Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I put graphics on a JPanel?

I am having a problem adding graphics to a JPanel. If I change the line from panel.add(new graphics()); to frame.add(new graphics()); and do not add the JPanel to the JFrame, the black rectangle appears on the JFrame. I just cannot get the black rectangle to appear on the JPannel and was wondering if someone could help me with this.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

 public class Catch{

public class graphics extends JComponent{
    public void paintComponent(Graphics g){
    super.paintComponents(g);
    g.fillRect(200, 62, 30, 10);
    }
}

 public void createGUI(){
    final JFrame frame = new JFrame();
    JPanel panel = new JPanel();
    frame.setSize(500,500);
    frame.addMouseListener(new MouseAdapter(){
        public void mouseClicked(MouseEvent e) {
            System.out.println(e.getPoint().getX());
            System.out.println(e.getPoint().getY());
        }
     });
    panel.add(new graphics());
    frame.add(panel);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(frame.DISPOSE_ON_CLOSE); 
}

public static void main(String[] args){
    Catch GUI= new Catch();
    GUI.createGUI();
   }
}
like image 988
dannyn382 Avatar asked May 07 '12 19:05

dannyn382


People also ask

Can you add image to JPanel?

You need to add the MapLabel to the top JPanel, and make sure to size them all to the full size of the image (by overriding the GetPreferredSize()). Save this answer.

How do I make a transparent JPanel?

You can simply create your jPanel using drag and drop, as you always do and then for changing the panel's color and making it transparent or semi-transparent you can use this code: panel. setBackground(new Color(0.0f, 0.0f, 0.0f, 0.5f));

Which method do you use to add a component to a JPanel?

Use add() to place components in a panel. FlowLayout is the default layout manager for JPanel . Use setLayout() only if you want a different layout manager.


2 Answers

The custom component was 0x0 px.

import java.awt.*;
import javax.swing.*;

public class Catch {

    public class MyGraphics extends JComponent {

        private static final long serialVersionUID = 1L;

        MyGraphics() {
            setPreferredSize(new Dimension(500, 100));
        }

        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.fillRect(200, 62, 30, 10);
        }
    }

    public void createGUI() {
        final JFrame frame = new JFrame();
        JPanel panel = new JPanel();
        panel.add(new MyGraphics());
        frame.add(panel);
        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                Catch GUI = new Catch();
                GUI.createGUI();
            }
        });
    }
}
like image 131
Andrew Thompson Avatar answered Oct 22 '22 09:10

Andrew Thompson


Here is something you can look at:

import javax.swing.*;
import java.awt.*;
public class GraphicsOnJPanel extends JFrame
{
    public GraphicsOnJPanel ()
    {
        setSize (Toolkit.getDefaultToolkit ().getScreenSize ());
        setResizable (false);
        setContentPane (new JPanel ()
        {
            public void paint (Graphics g)
            {
                g.setColor (Color.RED);
                g.fillRect (100, 100, 100, 100);
             }
         }
    );
          setVisible (true);
}


    public static void main (String args[])
    {
        new GraphicsOnJPanel ();
    }
}

Maybe this is useful?

like image 36
user2213307 Avatar answered Oct 22 '22 09:10

user2213307