Note that there are some explanatory texts on larger screens.

plurals
  1. PODo I need to extend JPanel for paintComponent?
    primarykey
    data
    text
    <p>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.</p> <p><strong>EDIT: Source</strong></p> <p>This is not working, I haven't got paintComponent running properly.</p> <pre><code>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); } } </code></pre> <p>Error:</p> <pre><code>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 </code></pre> <p><strong>2nd Edit</strong></p> <p>OK so I updated the paintComponent class to this</p> <pre><code> super.paintComponent(g); final Dimension d = getSize(); g.drawImage(ImageIO.read("img.png")); </code></pre> <p>And now I get this error.</p> <pre><code>javac graphicsprogram.java graphicsprogram.java:9: error: cannot find symbol g.drawImage(ImageIO.read("img.png")); ^ symbol: variable ImageIO location: class graphicsprogram 1 error </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload