[英]paintComponent() not being called
Here is a little program that should (in theory) draw an image of a ball on screen.这是一个应该(理论上)在屏幕上绘制球图像的小程序。 The problem is that paintComponent seems to not get called.问题是paintComponent 似乎没有被调用。 The program consists of two classes.该计划由两个班级组成。
import java.awt.*;
import java.io.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.swing.*;
public class ScreenSaver extends JPanel {
private static final long serialVersionUID = 001;
public static void main(String[] args) {
new ScreenSaver();
}
public ScreenSaver() {
new Window(1600, 900, "ScreenSaver", this);
}
//----------------------------------------------------------------------------------
private static BufferedImage ball;
public static BufferedImage getBallSprite() {
try {
File pathToBall = new File("ball.png");
ball = ImageIO.read(pathToBall);
} catch (IOException ex) {
ex.printStackTrace();
}
return ball;
}
}
import java.awt.*;
import java.io.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.swing.*;
public class Window extends Canvas {
private static final long serialVersionUID = 002;
public Window(int width, int height, String title, ScreenSaver ScreenSaver) {
JFrame frame = new JFrame(title);
frame.setPreferredSize(new Dimension(width, height));
frame.setMaximumSize(new Dimension(width, height));
frame.setMinimumSize(new Dimension(width, height));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
repaint();
}
public void paintComponent(Graphics g) {
System.out.println("Painting...");
BufferedImage ball = ScreenSaver.getBallSprite();
g.drawImage(ball, 0, 0, 100, 100, this);
}
}
As you can see, I tested if paintComponent was called using a console message.如您所见,我测试了是否使用控制台消息调用了paintComponent。 Sadly this was not the case.可悲的是,情况并非如此。 Can someone explain?有人可以解释吗?
java.awt.Canvas
does not inherit from JComponent
so paintComponent
won't be called automatically java.awt.Canvas
不继承自JComponent
所以paintComponent
不会被自动调用
public class MyWindow extends JComponent {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
...
}
It's no wonder that paintComponent
is not called, because Canvas
has no implementation of paintComponent
which you can override.难怪不调用paintComponent
,因为Canvas
没有您可以覆盖的paintComponent
实现。 With a canvas
you have to overwrite paint
for your purposes.使用canvas
,您必须为您的目的覆盖paint
。 In your code you use both a JPanel
and a Canvas
, which is not necessary at all.在您的代码中,您同时使用JPanel
和Canvas
,这根本没有必要。 Use either of the two.使用两者中的任何一个。
The following is an example with a Canvas
:以下是Canvas
的示例:
import java.awt.*;
import javax.swing.*;
public class ScreenSaver extends Canvas{
public static void main(String[] args) {
JFrame window = new JFrame("Screensaver");
window.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
window.setResizable(false);
ScreenSaver canvas = new ScreenSaver();
canvas.setPreferredSize(new Dimension(1600, 900));
canvas.setBackground(Color.BLACK);
window.add(canvas);
window.pack();
window.setLocationRelativeTo(null);
window.setVisible(true);
}
@Override
public void paint(Graphics g){
g.setColor(Color.GREEN);
g.fillOval(100, 100, 100, 100);
}
}
The annotation Override
above the method to be overwritten ensures that the compiler can issue a warning message if the overwritten method does not exist or there is a typo.要覆盖的方法上方的注释Override
确保编译器可以在被覆盖的方法不存在或存在拼写错误的情况下发出警告消息。 I hope this helps you further.我希望这可以进一步帮助您。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.