简体   繁体   English

我是否需要为paintComponent扩展JPanel?

[英]Do I need to extend JPanel for paintComponent?

Can anyone give me a link (I have looked, almost, everywhere) to the documentation about how to use paintComponent, I am just looking at displaying a .png that I can change the x, y co-ords (from keyboard input). 谁能给我一个有关如何使用paintComponent的文档的链接(我几乎在所有地方都看过),我只是在显示一个.png,我可以更改x,y坐标(通过键盘输入)。 Any help is appreciated. 任何帮助表示赞赏。

EDIT: Source 编辑:源

This is not working, I haven't got paintComponent running properly. 这不起作用,我的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);
    }
}

Error: 错误:

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

2nd Edit 第二次编辑

OK so I updated the paintComponent class to this 好,所以我将paintComponent类更新为此

    super.paintComponent(g);
    final Dimension d = getSize();
    g.drawImage(ImageIO.read("img.png"));

And now I get this error. 现在我得到了这个错误。

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");
                    ^

Image is an interface and thus has no constructor at all. 图像是一个接口,因此根本没有构造函数。 The Image part of the API will tell you this, and should be the first place you go when you have similar errors. API的图片部分会告诉您这一点,当您遇到类似错误时,它应该是您的第一个地方。 The solution is to use ImageIO.read(....) to read in the Image as a File or as a resource. 解决方案是使用ImageIO.read(....)将Image作为文件或资源读取。

  symbol:   method Image(String)
  location: class graphicsprogram
graphicsprogram.java:10: error: no suitable method found for drawImage(Image)
        g.drawImage(img);
         ^

The Graphics class has no method that takes a single Image object as its parameter. Graphics类没有将单个Image对象作为其参数的方法。 It's never a good idea to make up methods and hope that they might work and again the API will tell you what methods you can use on Graphics to draw the image. 组合方法绝不是一个好主意,并希望它们会起作用,并且API会再次告诉您可以在Graphics上使用哪些方法来绘制图像。

Also, you'll want to read in the image once and probably in your class's constructor rather than trying to read it in the paintComponent method since you don't want to slow this method down. 另外,您将希望一次读取图像,并且可能要在类的构造函数中读取图像,而不是尝试在paintComponent方法中读取图像,因为您不想降低此方法的速度。 It will make your program slow to respond. 这将使您的程序响应速度变慢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM