Re: Paint Event does not fire until teh frame has been resized
I have a problem with a small app I am creating.
I have attached some of the Class code below.
When the app starts the objects are not visible in the frame.
To make the objects visible I have to manually resize the
window then all objects become visible.
I have placed an println statement in the Paint function and there
is no output until I resize which indicates that the Paint function
is not firing the first time.
It does then function correctly all the time and the window is
repainted every time an object is moved.
Does Anybody have any idea what I am doing wrong ?
There are many problems with the code you are showing, but the real
problem is in the code you're not showing. I'll give pointers on what
needs to be fixed in this code snippet, but please provide an SSCCE that
demonstrates the problem.
public class TheGUIView extends Canvas implements Observer,
You're doing too much with this one class. Use anonymous inner classes
// Global Variables required in this module
private TheModel model;
private TheController controller;
Neither the view nor the model should know or care about the controller
in proper MVC design.
private JFrame frm;
private JPanel pnl;
private static int F_HEIGHT = 400;
private static int F_WIDTH = 400;
public TheSheepGUIView(TheModel model, TheController controller)
this.model = model;
this.controller = controller;
frm = new JFrame("Move The Object Using The Curson Keys");
pnl = (JPanel)frm.getContentPane();
Never allow the "this" reference escape your constructor. Use a
factory-method instead if you need to ensure initialization.
// Paint the game on the screen using the state (the model)
public void paint(Graphics g)
Don't extend Canvas and paint(), extend JComponent (or JPanel) and
// There is a problem as this Paint function is not being fired at
Graphics2D g2 = (Graphics2D)g;
g2.setColor(new Color(255, 0, 0));
// Render the Object - A Square
I'm also guessing that you don't use EventQueue.invokeLater(...) to
instantiate a TheGUIView object. This can lead to *many* problems, some
that are more apparent than others.
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>