Paint Event does not fire until teh frame has been resized


I

Iain

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
...........
...........

}
 
Ad

Advertisements

I

Iain

Hi Sabine

Thanks for the feedback. I added the call the pack as below

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.pack();

frm.setAlwaysOnTop(true);

and it fails.

It shrinks the frame down so I can only see the window bar with the
Text Title.
I still have to resize the object manually.
 
I

Iain

Hi Sabine
Hm. Switch the setVisible and setSize around. Read [1]. I'm a bit
stumped.

I have switched the lines about. No difference.


Apart from the panel there are no other objects on the panel.
The paint function uses only

g2.fillOval
g2.fillRect

to display various shapes. Am I right (in my newbie mind) that these
are not physical objects so the pack() would not see them ?
 
M

Mayeul

Iain said:
Hi Sabine
Hm. Switch the setVisible and setSize around. Read [1]. I'm a bit
stumped.

I have switched the lines about. No difference.


Apart from the panel there are no other objects on the panel.
The paint function uses only

g2.fillOval
g2.fillRect

to display various shapes. Am I right (in my newbie mind) that these
are not physical objects so the pack() would not see them ?

The pack() doesn't see what is painted where, it only deals with
components. No idea what would qualify as a physical object.

So, indeed, pack() doesn't see g2.fillOval() and such.
 
K

Knute Johnson

Iain said:
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.
 
Ad

Advertisements

D

Daniel Pitts

Iain said:
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
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,
KeyListener
You're doing too much with this one class. Use anonymous inner classes
where appropriate.
{
// 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");
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frm.setSize(F_WIDTH, F_HEIGHT);
frm.setVisible(true);
pnl = (JPanel)frm.getContentPane();
pnl.add(this);
Never allow the "this" reference escape your constructor. Use a
factory-method instead if you need to ensure initialization.
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)
Don't extend Canvas and paint(), extend JComponent (or JPanel) and
paintComponent() instead.
{
// 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
...........
...........

}

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.
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top