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.