1

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
ToXic73
  • 332
  • 6
  • 15
  • two good links: [first](http://docs.oracle.com/javase/tutorial/uiswing/painting/index.html) and [second](http://www.oracle.com/technetwork/java/painting-140037.html) – Hovercraft Full Of Eels Jun 09 '13 at 12:14
  • Add some [additional notes](http://stackoverflow.com/questions/17006929/move-bufferedimage-with-keys/17007028#17007028). And no, you don't need to extend `JPanel`, you could extend `JComponent` instead... – MadProgrammer Jun 09 '13 at 12:19
  • Can you provide more info? Because maybe you just need to use jlabel instead of rewriting the method. – docDevil Jun 09 '13 at 12:23
  • Your "example" class does not extend JPanel, JComponent or anything similar, and so it makes sense that paintComponent won't work. Did you read the links that I've given you in my first comment? Please do so without delay. – Hovercraft Full Of Eels Jun 09 '13 at 13:18
  • @HovercraftFullOfEels, done, but it still has the same errors – ToXic73 Jun 09 '13 at 13:36
  • @ToXic73: you will want to show the latest code as an edit to your question, as well as show any error/exception text as well. – Hovercraft Full Of Eels Jun 09 '13 at 13:51
  • @ToXic73 : Either use a [BufferedImage](http://docs.oracle.com/javase/7/docs/api/java/awt/image/BufferedImage.html) or [ImageIcon](http://docs.oracle.com/javase/7/docs/api/javax/swing/ImageIcon.html) for storing `image`. This [example](http://stackoverflow.com/a/13795187/1057230) might can shed more light on the topic. THere is one wonderful post by HovercraftFullOfEels related to one Chess Game with BufferedImage, I am trying to find that post, but its still ellusive. I hope he be able to do that soon. (Green blocks with Gray and a pawn is drawn in that post) – nIcE cOw Jun 09 '13 at 14:10

1 Answers1

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

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373