Win a copy of Zero to AI - A non-technical, hype-free guide to prospering in the AI era this week in the Artificial Intelligence and Machine Learning forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Overriding paintComponent

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
oh, and I tried putting
super.paintComponents(g);
in as well...
 
reply
    Bookmark Topic Watch Topic
  • New Topic