W
Wolfgang
I have the simplest program, first placing an image in a panel, then
painting a simple ball on top of it (just a red dot); see appended
code. Since the loading of the image takes unusually long (seconds),
I put in a static variable 'numberOfTimesBallWasDrawn' to see what
happens. If I just draw the ball, and no image, everything is normal,
the ball is drawn once. If I draw an image, and then the ball,
however, the ball gets drawn 640 times (sixhundredandfourty),
according to variable 'numberOfTimesBallWasDrawn'. Why is that? Can
anybody enlighten me about what's going on here?
Thanks for your help and advice.
Wolfgang
//==== simple code drawing image and ball======
import java.awt.*;
import java.util.*;
import javax.swing.*;
public class MainFrame extends JFrame {
static int numberOfTimesBallWasDrawn = 0;
public MainFrame() {
JPanelToDrawOn ap = new JPanelToDrawOn();
setSize(300, 300);
getContentPane().add(ap);
setVisible(true);
}
public static void main(String[] args) {
MainFrame ap = new MainFrame();
}
}
class JPanelToDrawOn extends JPanel {
Image image = Toolkit.getDefaultToolkit().getImage("c:/temp/deepfield.gif");
Ball ball = new Ball();
public void paint(Graphics g) {
// THE IMAGE IS THE CULPRIT causing 640 times drawing
g.drawImage(image, 0, 0, 300, 300, this);
ball.draw(g);
}
}
class Ball {
public void draw(Graphics g) {
System.err.println(MainFrame.numberOfTimesBallWasDrawn++);
g.setColor(Color.red);
g.fillOval(30, 30, 20, 20);
}
}
painting a simple ball on top of it (just a red dot); see appended
code. Since the loading of the image takes unusually long (seconds),
I put in a static variable 'numberOfTimesBallWasDrawn' to see what
happens. If I just draw the ball, and no image, everything is normal,
the ball is drawn once. If I draw an image, and then the ball,
however, the ball gets drawn 640 times (sixhundredandfourty),
according to variable 'numberOfTimesBallWasDrawn'. Why is that? Can
anybody enlighten me about what's going on here?
Thanks for your help and advice.
Wolfgang
//==== simple code drawing image and ball======
import java.awt.*;
import java.util.*;
import javax.swing.*;
public class MainFrame extends JFrame {
static int numberOfTimesBallWasDrawn = 0;
public MainFrame() {
JPanelToDrawOn ap = new JPanelToDrawOn();
setSize(300, 300);
getContentPane().add(ap);
setVisible(true);
}
public static void main(String[] args) {
MainFrame ap = new MainFrame();
}
}
class JPanelToDrawOn extends JPanel {
Image image = Toolkit.getDefaultToolkit().getImage("c:/temp/deepfield.gif");
Ball ball = new Ball();
public void paint(Graphics g) {
// THE IMAGE IS THE CULPRIT causing 640 times drawing
g.drawImage(image, 0, 0, 300, 300, this);
ball.draw(g);
}
}
class Ball {
public void draw(Graphics g) {
System.err.println(MainFrame.numberOfTimesBallWasDrawn++);
g.setColor(Color.red);
g.fillOval(30, 30, 20, 20);
}
}