0

hope this is not a duplicated post but I really can not find the solution.

So, I want to put an image taking all the panel

This is my Code:

public class MyFrame extends JFrame{

private BufferedImage backgroundImage;

public MyFrame() throws IOException {
    setSize(650, 450);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setTitle("Dragon Game");
    setResizable(false);

    getContentPane().setFocusTraversalPolicyProvider(true);
    getContentPane().setBounds(new Rectangle(0, 0, 650, 450));
    getContentPane().setSize(650,450);
    getContentPane().setLayout(null);


    JPanel gamePanel = new JPanel();
    gamePanel.setBounds(0, 25, 650, 375);
    getContentPane().add(gamePanel);
    gamePanel.setLayout(null);
    try{
        backgroundImage = ImageIO.read( new File("imgs/dragon.png"));       
    } catch (IOException e) {
        e.printStackTrace();
    }
}

protected void paintComponent(Graphics g){
    requestFocus(true);
    setFocusable(true);

    g.drawImage(backgroundImage, 0, 0, this.getWidth(), this.getHeight(), Color.WHITE, null);
}

}

Any Idea?

Luís Melo
  • 191
  • 1
  • 12
  • 3
    JFrame has no paintComponent method. Please use @Override annotations, please read the many many similar questions on this site. Draw in a JPanel's paintComponent method, not in the JFrame as all the other question's answers will tell you. – Hovercraft Full Of Eels Mar 25 '16 at 02:27
  • 1
    Or draw it in a JLabel as an ImageIcon -- but if you do this, be careful to set up the JLabel's layout manager. Also paintComponent should never have `requestFocus` or `setFocusable` within it **ever**. This method should be for painting and painting only. – Hovercraft Full Of Eels Mar 25 '16 at 02:28
  • 2
    [link to 18,000 duplicate questions](https://www.google.com/webhp?sourceid=chrome-instant&rlz=1C1LEND_enUS445US445&ion=1&espv=2&ie=UTF-8#q=drawing+an+image+in+a+jpanel+site:http:%2F%2Fstackoverflow.com%2F). – Hovercraft Full Of Eels Mar 25 '16 at 02:28
  • Also, try hard coding variables. This is a short program, but it would help if you had a final int to prevent any errors. – A. Takami Mar 25 '16 at 02:56
  • @HovercraftFullOfEels: sorry... but much thanks... that help a lot. If I may, i want to change the image when i click a button (who change a boolean variable), just put if/else statement?. – Luís Melo Mar 25 '16 at 03:25
  • @A.Takami: I don't understand. Where can I put the final int? – Luís Melo Mar 25 '16 at 03:27
  • @LuísMelo: an if/else would work fine, or simply changing the BufferedImage that the backgroundImage variable refers to, and then calling `repaint()`. – Hovercraft Full Of Eels Mar 25 '16 at 03:48
  • @LuísMelo for example, you keep calling the `setSize()` method on values 650 and 450, along with plenty of other calls where you are using the same values multiple times, but simply just typing them in. to prevent errors further down the road, you can hard code these variables by allocating them as, for example, `public static final int WIDTH = 650` which will prevent the ints from being altered down the line, and prevent possible errors. the `final` simply prevents the int from being accidentally altered. – A. Takami Mar 28 '16 at 07:39

0 Answers0