Board index » jbuilder » Disappearing graphics.

Disappearing graphics.


2004-09-18 09:37:34 AM
jbuilder4
Hi. I am trying to write a program for my Java class in school. I have
sucessfully drawn objects on a jFrame, as required, but they disappear as
soon as they are drawn. I can only see them for a very brief instant, then
they are gone, leaving an empty jFrame. Can anyone explain this? I tried
asking my teacher, but for some reason it doesn't happen on his computer.
While this is good for grading purposes, it doesn't help me solve the
problem. I am using the newest version of JBuilder X Foundation, just
downloaded and installed (and reinstalled, to see if that helped). I have
run it with java 1.4.2 and the 1.5RC. Neither seem to work.
Please help. This is really annoying!
To help anyone understand the project, the description is:
2. Implement a superclass Vehicle and subclasses Car and Truck. A
vehicle has a position on the screen, utilizing the class java.awt.Point.
Implement a method draw in the class RandomDrawing. This draw method can
take in different types of vehicles, and paint car and truck as follows:
(graphics removed - they aren't really important to this problem)
In RandomDrawing class, generate four trucks and six cars with randomly
generated positions for drawing. The position for each vehicle has the range
100~700 for x-axis and 100~500 for y-axis. You must take advantage of
inheritance, polymorphism, and genericity.
My code follows. The first is the class that extends jFrame, which is where
I suspect the problem is. The other classes follow that one.
package randomdrawing;
----------------------------------------------------------------------------
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class RandomDrawing extends JFrame {
JPanel contentPane;
BorderLayout borderLayout1 = new BorderLayout();
JPanel jPanel1 = new JPanel();
// need to declare 6 cars - an array seems logical
Car[] cars = new Car[6];
// do the same for 4 trucks
Truck[] trucks = new Truck[4];
//Construct the frame
public RandomDrawing() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(borderLayout1);
this.setSize(new Dimension(800, 600));
this.setTitle("Random Drawing");
this.addFocusListener(new RandomDrawing_this_focusAdapter(this));
this.addWindowListener(new RandomDrawing_this_windowAdapter(this));
jPanel1.addFocusListener(new RandomDrawing_jPanel1_focusAdapter(this));
contentPane.add(jPanel1, BorderLayout.CENTER);
}
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
public void update(Graphics g){
drawVehicles();
}
// maybe try contentPane instead of jPanel1
public void draw(Vehicle v){
v.paint(jPanel1.getGraphics());
}
public void drawVehicles(){
int i;
Point p = new Point();
Random r = new Random();
// generate Car positions, and draw each
for(i=0; i<6; i++){
p.setLocation(r.nextInt(601)+100, r.nextInt(401)+100);
cars[i] = new Car(p);
draw(cars[i]);
}
// generate positions for trucks, and draw each
for(i=0; i<4; i++){
p.setLocation(r.nextInt(601)+100, r.nextInt(401)+100);
trucks[i] = new Truck(p);
draw(trucks[i]);
}
}
void this_windowOpened(WindowEvent e) {
drawVehicles();
}
void this_focusGained(FocusEvent e) {
}
}
class RandomDrawing_jPanel1_focusAdapter extends java.awt.event.FocusAdapter
{
RandomDrawing adaptee;
RandomDrawing_jPanel1_focusAdapter(RandomDrawing adaptee) {
this.adaptee = adaptee;
}
}
class RandomDrawing_this_windowAdapter extends java.awt.event.WindowAdapter
{
RandomDrawing adaptee;
RandomDrawing_this_windowAdapter(RandomDrawing adaptee) {
this.adaptee = adaptee;
}
public void windowOpened(WindowEvent e) {
adaptee.this_windowOpened(e);
}
}
class RandomDrawing_this_focusAdapter extends java.awt.event.FocusAdapter {
RandomDrawing adaptee;
RandomDrawing_this_focusAdapter(RandomDrawing adaptee) {
this.adaptee = adaptee;
}
public void focusGained(FocusEvent e) {
adaptee.this_focusGained(e);
}
}
-------------------------------------------------------------------
package randomdrawing;
import javax.swing.UIManager;
import java.awt.*;
public class RandomDrawingApp {
boolean packFrame = false;
//Construct the application
public RandomDrawingApp() {
RandomDrawing frame = new RandomDrawing();
//Validate frames that have preset sizes
//Pack frames that have useful preferred size info, e.g. from their
layout
if (packFrame) {
frame.pack();
}
else {
frame.validate();
}
//Center the window
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.height>screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width>screenSize.width) {
frameSize.width = screenSize.width;
}
frame.setLocation((screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2);
frame.setVisible(true);
}
//Main method
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(Exception e) {
e.printStackTrace();
}
new RandomDrawingApp();
}
}
----------------------------------------------------------------------------
-----
package randomdrawing;
import java.awt.*;
abstract public class Vehicle {
// the position of the Vehicle on the screen
public Point pos;
// constructors
public Vehicle() {
}
public Vehicle(Point p){
pos = p;
}
abstract public void paint(Graphics g);
}
----------------------------------------------------------------------------
------
package randomdrawing;
import java.awt.*;
public class Car extends Vehicle {
public Car() {
}
public Car(Point p){
super(p);
}
public void paint(Graphics g) {
g.drawOval(pos.x, pos.y, 10, 10);
g.drawOval(pos.x+40, pos.y, 10, 10);
g.drawRect(pos.x-10, pos.y-20, 70, 20);
g.drawRect(pos.x+10, pos.y-30, 30, 10);
}
}
-------------------------------------------------------------------
package randomdrawing;
import java.awt.*;
public class Truck extends Vehicle {
public Truck() {
}
public Truck(Point p){
super(p);
}
public void paint(Graphics g) {
g.drawOval(pos.x, pos.y, 10, 10);
g.drawOval(pos.x+30, pos.y, 10, 10);
g.drawOval(pos.x+40, pos.y, 10, 10);
g.drawOval(pos.x+120, pos.y, 10, 10);
g.drawOval(pos.x+130, pos.y, 10, 10);
g.drawRect(pos.x, pos.y-30, 20, 30);
g.drawRect(pos.x+23, pos.y-40, 120, 40);
}
}
------------------------------------------------------------------
 
 

Re:Disappearing graphics.

What happens is that the JPanel is painted over your manually painted
vehicles.
Instead of drawing directly to the JFrame, you should subclass JPanel
and use that subclass. Then paint the vehicles in the subclass'
paintComponent() method.
 

Re:Disappearing graphics.

Tor Iver Wilhelmsen < XXXX@XXXXX.COM >wrote:
Quote
What happens is that the JPanel is painted over your manually painted
vehicles.

Instead of drawing directly to the JFrame, you should subclass JPanel
and use that subclass. Then paint the vehicles in the subclass'
paintComponent() method.
Ok. First, thank you, that makes perfect sence. I'm glad I at least have a clue now. However, I still can't get it to work. I created a subclass of jPanel, and added the code into it to draw the vehicles. I then tried to call this from the main application program when it opens the frame. I get the same result. Being new at this stinks!
If you can help me understand, here is the new subclass of jPanel (MyDrawing) and the changed RandomDrawing and RandomDrawingApp classes. Thank you for your help!
--------------------------------------------------------------
import java.awt.*;
import java.util.*;
public class MyDrawing extends JPanel {
Car[] cars = new Car[6];
Truck[] trucks = new Truck[4];
public MyDrawing() {
}
public void paintComponent(Graphics g) {
super.paintComponent(g); // paints background
genVehicles();
}
public void draw(Vehicle v){
v.paint(this.getGraphics());
}
public void genVehicles(){
int i;
Point p = new Point();
Random r = new Random();
// generate Car positions, and draw each
for(i=0; i<6; i++){
p.setLocation(r.nextInt(601)+100, r.nextInt(401)+100);
cars[i] = new Car(p);
draw(cars[i]);
}
// generate positions for trucks, and draw each
for(i=0; i<4; i++){
p.setLocation(r.nextInt(601)+100, r.nextInt(401)+100);
trucks[i] = new Truck(p);
draw(trucks[i]);
}
}
}
------------------------------------------------------------
package randomdrawing;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class RandomDrawing
extends JFrame {
JPanel contentPane;
BorderLayout borderLayout1 = new BorderLayout();
MyDrawing jPanel1 = new MyDrawing();
//Construct the frame
public RandomDrawing() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch (Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
contentPane = (JPanel)this.getContentPane();
contentPane.setLayout(borderLayout1);
this.setSize(new Dimension(800, 600));
this.setTitle("Random Drawing");
this.addFocusListener(new RandomDrawing_this_focusAdapter(this));
this.addWindowListener(new RandomDrawing_this_windowAdapter(this));
jPanel1.addFocusListener(new RandomDrawing_jPanel1_focusAdapter(this));
contentPane.add(jPanel1, BorderLayout.CENTER);
}
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
public void draw(Vehicle v) {
v.paint(jPanel1.getGraphics());
}
}
class RandomDrawing_jPanel1_focusAdapter
extends java.awt.event.FocusAdapter {
RandomDrawing adaptee;
RandomDrawing_jPanel1_focusAdapter(RandomDrawing adaptee) {
this.adaptee = adaptee;
}
}
class RandomDrawing_this_windowAdapter
extends java.awt.event.WindowAdapter {
RandomDrawing adaptee;
RandomDrawing_this_windowAdapter(RandomDrawing adaptee) {
this.adaptee = adaptee;
}
}
class RandomDrawing_this_focusAdapter
extends java.awt.event.FocusAdapter {
RandomDrawing adaptee;
RandomDrawing_this_focusAdapter(RandomDrawing adaptee) {
this.adaptee = adaptee;
}
}
--------------------------------------------------------------
package randomdrawing;
import javax.swing.UIManager;
import java.awt.*;
public class RandomDrawingApp {
boolean packFrame = false;
//Construct the application
public RandomDrawingApp() {
RandomDrawing frame = new RandomDrawing();
//Validate frames that have preset sizes
//Pack frames that have useful preferred size info, e.g. from their layout
if (packFrame) {
frame.pack();
}
else {
frame.validate();
}
//Center the window
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.height>screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width>screenSize.width) {
frameSize.width = screenSize.width;
}
frame.setLocation( (screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2);
frame.setVisible(true);
frame.jPanel1.paintComponent(frame.jPanel1.getGraphics());
}
//Main method
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception e) {
e.printStackTrace();
}
new RandomDrawingApp();
}
}
--------------------------------------------------------------
 

{smallsort}

Re:Disappearing graphics.

On 9/18/2004 at 8:11:56 PM, Steve wrote:
Quote
public void paintComponent(Graphics g) {
super.paintComponent(g); // paints background
genVehicles();
}
The paintComponent() method should normally just paint the component and
do nothing else. In any case, it should not change the material state of
the component.
Quote
public void draw(Vehicle v){
v.paint(this.getGraphics());
}
As a general rule, you should not be invoking the painting code yourself.
Simply override the paintComponent() method and the Swing paint subsystem
will call it when appropriate. If the component changes its state in such
a way that it needs to be repainted, it should call the repaint() method.
That should eventually result in the paintComponent() method being called
by the paint subsystem.
You might want to check out the "Swing Tutorial". It has a section
specifically devoted to custom painting, but it would be a good idea to
read the rest of the tutorial as well.
java.sun.com/docs/books/tutorial/uiswing/index.html
--
Regards,
John McGrath [TeamB]
---------------------------------------------------
Before sending me e-mail, please read:
www.JPMcGrath.net/newsgroups/e-mail.html