• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Overriding paintComponent

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
I have a program where I have various Displays added to a JFrame. If I run this:
package tetris_1_1;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
public class Display extends JPanel
{
protected Dimension d;
protected int width, height;
public Display()
{
}
public Display(int width, int height, String title)
{
d = new Dimension(width, height);
setBackground(Color.black);
if(title != null)
{
this.setBorder(new TitledBorder(null, title, TitledBorder.RIGHT, TitledBorder.BOTTOM));
}
}
public void drawGrid(Graphics g)
{
g.setColor(Color.white);
for(int i=0; i<width; i+=20)
{
g.drawLine(i, 0, i, height);
}
for(int j=0; j<width; j+=20)
{
g.drawLine(0, j, width, j);
}
}
public Dimension getMinimumSize()
{
return getPreferredSize();
}
public Dimension getPreferredSize()
{
return new Dimension(d);
}
}
with a call from the JFrame like:
scoreboard = new Display(100, 50, "score");
.
.
c.add(scoreboard);//(Container c = getContentPane(); )
it works fine, but if I want to call drawGrid(); to draw a grid in the Display, it messes up. I override paintComponent(), but when it runs, the Displays disappear. This is my paintComponent():
paintComponent(Graphics g)
{
drawGrid(g);
}
I've tried modifying it thus:
paintComponent(Graphics g)
{
g.setColor(Color.black);
g.fillRect(0, 0, width, height);
drawGrid(g);
}
but that doesn't work either.
Any ideas folks?
Malc.
 
malcolm bailey
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oh, and I tried putting
super.paintComponents(g);
in as well...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic