![](/img/trans.png)
[英]JPanel drawing - why do I have to override paintComponent method?
[英]Do I need to extend JPanel for paintComponent?
谁能给我一个有关如何使用paintComponent的文档的链接(我几乎在所有地方都看过),我只是在显示一个.png,我可以更改x,y坐标(通过键盘输入)。 任何帮助表示赞赏。
编辑:源
这不起作用,我的paintComponent没有正常运行。
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
class graphicsprogram extends JPanel {
public void paintComponent(final Graphics g) {
super.paintComponent(g);
Image img = new Image("img.png");
final Dimension d = getSize();
g.drawImage(img);
}
public static void main(String[] args) {
final Point point = new Point();
final JFrame frame = new JFrame("Grid");
JLabel label = new JLabel("Drag Me!!!", JLabel.CENTER);
JButton close = new JButton(new ImageIcon("close.png"));
// Button to close the window because the window is undecorated.
close.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
// Listen to the mouse activity for dragging the window
frame.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
point.x = e.getX();
point.y = e.getY();
}
});
// Listener for moving the window
frame.addMouseMotionListener(new MouseMotionAdapter (){
public void mouseDragged(MouseEvent e) {
Point p = frame.getLocation();
frame.setLocation(p.x + e.getX() - point.x, p.y + e.getY() - point.y);
}
});
close.setPreferredSize(new Dimension(50, 50));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setUndecorated(true);
frame.setMinimumSize(new Dimension(800, 600));
frame.setLocation(200, 200);
frame.setLayout(new BorderLayout());
frame.getContentPane().add(close, BorderLayout.NORTH);
frame.getContentPane().add(label, BorderLayout.CENTER);
frame.getContentPane().setBackground(Color.PINK);
frame.setVisible(true);
}
}
错误:
javac graphicsprogram.java
graphicsprogram.java:8: error: cannot find symbol
Image img = Image("img.png");
^
symbol: method Image(String)
location: class graphicsprogram
graphicsprogram.java:10: error: no suitable method found for drawImage(Image)
g.drawImage(img);
^
method Graphics.drawImage(Image,int,int,int,int,int,int,int,int,Color,ImageObserver) is not applicable
(actual and formal argument lists differ in length)
method Graphics.drawImage(Image,int,int,int,int,int,int,int,int,ImageObserver) is not applicable
(actual and formal argument lists differ in length)
method Graphics.drawImage(Image,int,int,int,int,Color,ImageObserver) is not applicable
(actual and formal argument lists differ in length)
method Graphics.drawImage(Image,int,int,Color,ImageObserver) is not applicable
(actual and formal argument lists differ in length)
method Graphics.drawImage(Image,int,int,int,int,ImageObserver) is not applicable
(actual and formal argument lists differ in length)
method Graphics.drawImage(Image,int,int,ImageObserver) is not applicable
(actual and formal argument lists differ in length)
2 errors
第二次编辑
好,所以我将paintComponent类更新为此
super.paintComponent(g);
final Dimension d = getSize();
g.drawImage(ImageIO.read("img.png"));
现在我得到了这个错误。
javac graphicsprogram.java
graphicsprogram.java:9: error: cannot find symbol
g.drawImage(ImageIO.read("img.png"));
^
symbol: variable ImageIO
location: class graphicsprogram
1 error
javac graphicsprogram.java
graphicsprogram.java:8: error: cannot find symbol
Image img = Image("img.png");
^
图像是一个接口,因此根本没有构造函数。 API的图片部分会告诉您这一点,当您遇到类似错误时,它应该是您的第一个地方。 解决方案是使用ImageIO.read(....)
将Image作为文件或资源读取。
symbol: method Image(String)
location: class graphicsprogram
graphicsprogram.java:10: error: no suitable method found for drawImage(Image)
g.drawImage(img);
^
Graphics类没有将单个Image对象作为其参数的方法。 组合方法绝不是一个好主意,并希望它们会起作用,并且API会再次告诉您可以在Graphics上使用哪些方法来绘制图像。
另外,您将希望一次读取图像,并且可能要在类的构造函数中读取图像,而不是尝试在paintComponent方法中读取图像,因为您不想降低此方法的速度。 这将使您的程序响应速度变慢。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.