Re: Paint Event does not fire until teh frame has been resized
Iain wrote:
Hi all
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 ?
Many thanks
public class TheGUIView extends Canvas implements Observer,
KeyListener
{
// Global Variables required in this module
private TheModel model;
private TheController controller;
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");
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frm.setSize(F_WIDTH, F_HEIGHT);
frm.setVisible(true);
pnl = (JPanel)frm.getContentPane();
pnl.add(this);
frm.setAlwaysOnTop(true);
addKeyListener(this);
((Observable) model).addObserver(this);
}
// Paint the game on the screen using the state (the model)
public void paint(Graphics g)
{
// There is a problem as this Paint function is not being fired at
the start
System.out.println("Painting");
Graphics2D g2 = (Graphics2D)g;
g2.setColor(new Color(255, 0, 0));
// Render the Object - A Square
...........
...........
}
You are mixing AWT and Swing components. I'm sure you are having
problems. Pick one or the other. If you use AWT, override paint() and
if using Swing override paintComponent().
import java.awt.*;
import java.awt.event.*;
public class test extends Canvas {
public test() {
}
public void paint(Graphics g) {
g.setColor(Color.BLUE);
g.fillRect(10,10,10,10);
}
public static void main(String[] args) {
final Frame f = new Frame();
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
f.dispose();
}
});
f.add(new test());
f.setSize(240,180);
f.setVisible(true);
}
}
Also, no modern compiler requires you to add components to the content
pane of Swing containers any more. The JFrame and JWindow add() methods
add the component to the content pane.
--
Knute Johnson
email s/nospam/knute2009/
--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access