Author Topic: JPanel not displaying graphics  (Read 505 times)

So I've been creating this GUI for my program in Java, and its become a bit of a hassle.  Creating swing GUIs is pretty simple, although when it comes to integrating the Graphics class and swing, I run into some trouble.  I have three classes

  • Main (obviously)
  • DisplayManagement
  • DrawGraphics

Here's all of the classes

Main

Code: [Select]
import javax.swing.SwingUtilities;

public class Main {
   
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
            DisplayManagement manage = new DisplayManagement();
            manage.setTitle("Game");
            manage.setVisible(true);
            }
        });
    }
}

DrawGraphics

Code: [Select]
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;

public class DrawGraphics extends JPanel{
private void draw(Graphics g){
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.blue);
g2d.drawOval(10, 10, 100, 100);
}

@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
draw(g);
}
}

DisplayManagement

Code: [Select]
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

public class DisplayManagement extends JFrame implements ActionListener{
private JMenuBar mainMenuBar;
DrawGraphics drawG = new DrawGraphics();

    public DisplayManagement() {
        initUI();
        paint();
    }

    public final void initUI() {
        JMenu fileMenu = new JMenu ("File");
        JMenuItem printItem = new JMenuItem ("Print");
        fileMenu.add (printItem);
        JMenuItem exitItem = new JMenuItem ("Exit");
        fileMenu.add (exitItem);
        JMenu networkingMenu = new JMenu ("Networking");
        JMenuItem disconnectItem = new JMenuItem ("Disconnect");
        networkingMenu.add (disconnectItem);
        JMenuItem connectItem = new JMenuItem ("Connect");
        networkingMenu.add (connectItem);
        JMenu helpMenu = new JMenu ("Help");
        JMenuItem contentsItem = new JMenuItem ("Contents");
        helpMenu.add (contentsItem);
        JMenuItem aboutItem = new JMenuItem ("About");
        helpMenu.add (aboutItem);

        mainMenuBar = new JMenuBar();
        mainMenuBar.add (fileMenu);
        mainMenuBar.add (networkingMenu);
        mainMenuBar.add (helpMenu);

        setSize(510, 398);
        setLayout (null);

        add (mainMenuBar);
       
        mainMenuBar.setBounds (0, 0, 510, 25);

        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public final void paint(){
    add(drawG);
   
    setSize(360, 300);
    setTitle("Rectangles");
    setLocationRelativeTo(null);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
   
public void actionPerformed(ActionEvent e){
//Add actions later..
}
}

The GUI worked totally fine before I added stuff to the initUI method.. now the only thing that displays is the JMenu.. 

Anyone have any ideas?

http://pastebin.com/
http://en.irc2go.com/webchat/?net=QuakeNet&room=java

Armed with these you should get some help much faster. Worked for me.

Edit: If you comment out the setLayout (null); line in the initUI method it draws a circle. This might help: http://stackoverflow.com/questions/14982781/using-a-jpanel-with-a-null-layout
« Last Edit: December 10, 2013, 02:39:08 AM by Demian »