yspede yspede - 2 months ago 13
Java Question

How to paint on JFrame

I have created this class which create a JFrame with a background picture. I am trying to paint a circle on that picture. But I can only show the picture or the figure, the circle will not show on the picture. I call the class from my main.

Sorry if this i a newbie question :)

package worldofzuul;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.*;

/**
*
* @author JesperJørgensen
*/
public class GraphicsFrame extends JFrame {

private JPanel man = new JPanel();

void setupframe() {
// Here we create the Frame
JFrame frame = new JFrame(); // create the frame
frame.setLayout(new BorderLayout());
frame.setResizable(false);

frame.setTitle("Zuul the ultimate fridaybar game"); // sets title in top bar of frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // what will happens when the frame close (exit)

//Here we set the background image (the map which we walk in)
ImageIcon icon = new ImageIcon("src/Image/Kort.png");
frame.add(new JLabel(icon));

frame.setContentPane(new DrawPane());

frame.pack(); // sets the size of the frame to fit all objects inside.
frame.setVisible(true); // show the frame

}

class DrawPane extends JPanel {

@Override
protected void paintComponent(Graphics g) {
g.setColor(Color.red);
g.fillRect(20, 20, 100, 200);

}
}

}

Answer

setContentPane removes ImageIcon that so only visible element will be DrawPane