2

I am a beginner Java programmer and I have a question for putting image in my java game. In my code, I am putting f but I want to change it with an image of a flag.

How can I use drawImage?

    protected void paintComponent(Graphics graphics) {

        removeAll();

        super.paintComponent(graphics);

        if (caseModele.contientFlag()) {

            setBackground(Color.WHITE);

            graphics.setColor(Color.blue);
            graphics.drawString("f", getWidth() / 2, getHeight() / 2);//here

        } 
}
  • One thing to be aware of: drawString is bottom left anchored, whereas drawImage is top left anchored: Your string will print about the y component to the drawString argument, whereas your Image will be painted below your y argument – ControlAltDel Apr 23 '15 at 11:28

1 Answers1

1

You can't use drawString(); to draw an image, you should use drawImage(); instead.

Here you have a link that explains the use of drawImage(); you should be able to make your way from there. https://docs.oracle.com/javase/tutorial/2d/images/drawimage.html

Bowsska
  • 126
  • 8