我是否需要为paintComponent扩展JPanel?

时间:2013-06-09 12:12:23

标签: java swing paintcomponent

任何人都可以给我一个关于如何使用paintComponent的文档链接(我已经看过,几乎无处不在),我只是看着显示一个.png我可以改变x,y co-ords(来自键盘)输入)。任何帮助表示赞赏。

编辑:来源

这不起作用,我没有让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

1 个答案:

答案 0 :(得分:2)

javac graphicsprogram.java
graphicsprogram.java:8: error: cannot find symbol
        Image img = Image("img.png");
                    ^

Image是一个接口,因此根本没有构造函数。 API的Image部分会告诉您这一点,并且应该是您遇到类似错误时的第一个地方。解决方案是使用ImageIO.read(....)将图像作为文件或资源读取。

  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方法中读取它,因为您不希望减慢此方法的速度。它会使你的程序响应缓慢。