简体   繁体   中英

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). Any help is appreciated.

EDIT: Source

This is not working, I haven't got paintComponent running properly.

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

    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. The solution is to use ImageIO.read(....) to read in the Image as a File or as a resource.

  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. 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.

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. It will make your program slow to respond.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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