谁能给我一个关于如何使用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