1

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);

        }
    }

}

2 Answers2

0

setContentPane removes ImageIcon that so only visible element will be DrawPane

Antoniossss
  • 31,590
  • 6
  • 57
  • 99
  • okay, is there another way I can do it. That dont remove the drawing. –  Sep 29 '16 at 07:22
  • You can paint picture directly just like you do with the rectangle, and then paint on that picture. It is little bit more complex to explain in coupe iof sentences. Here you have how to "draw" a image http://stackoverflow.com/questions/17865465/how-do-i-draw-an-image-to-a-jpanel-or-jframe – Antoniossss Sep 29 '16 at 07:28
0

See comments:

//always post an MCVE
//see http://stackoverflow.com/help/mcve
//include imports 
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 *
 * @author JesperJørgensen
 */
public class GraphicsFrame extends JFrame {/*JFrame  subclassing is never used*/

    //This JPanel is never used
    private JPanel man = new JPanel();
    Image image;

    void setupframe() {
        // Here we create the Frame
        JFrame frame = new JFrame(); // create the frame
        frame.setSize(500,500);
        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)

        //initialize image
        image = new ImageIcon("src/Image/Kort.png").getImage();
        //frame.add(new JLabel(image));

        frame.setContentPane(new DrawPane());

        //if you don't use preferred sizes pack() will set frame to size 0.
        //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) {

            //add draw image to paint
            g.drawImage(image,0, 0, null);

            //this draws a rectangle. change to circle if desired
            g.setColor(Color.red);
            g.fillRect(20, 20, 100, 200);

        }
    }

    //include main to make it an MCVE
    public static void main(String args[]) {

        new GraphicsFrame().setupframe();
    }
}

To use the fact that this class is extending JFrame you may want to implement it like this:

public class GraphicsFrame extends JFrame {

    Image image;

    //introduce constructor
    public GraphicsFrame() {

        setupframe();
    }

    void setupframe() {

        // no need to create a frame. This class is a JFrame
        //JFrame frame = new JFrame(); // create the frame
        setSize(500,500);
        setLayout(new BorderLayout());
        setResizable(false);

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

        //initialize image
        image = new ImageIcon("src/Image/Kort.png").getImage();

        setContentPane(new DrawPane());

        setVisible(true); // show the frame
    }

    class DrawPane extends JPanel {

        @Override
        protected void paintComponent(Graphics g) {

            //add draw image to paint
            g.drawImage(image,0, 0, null);

            //this draws a circle
            g.setColor(Color.red);
            g.drawOval(100, 100, 40, 40);
        }
    }

    public static void main(String args[]) {

        new GraphicsFrame();
    }
}

Don't hesitate to ask for clarifications as needed.

c0der
  • 18,467
  • 6
  • 33
  • 65