RayG
RayG

Reputation: 1

Java Cannot See Graphics on JPanel

I am using Java Eclipse with Swing Designer and I am trying to display some graphics on a JPanel. I am a pretty amateurish programmer and new to graphics coding.

Basically, There is a JFrame with 2 JPanels( Stage and Setttings). There is a method paintTest inside the class Surface. This method should draw the string 'This is a test' on the Stage JPanel. When I execute the code there are no errors. Any help would be appreciated.

Please see the below code.

import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;

import javax.swing.JFrame; 
import javax.swing.JPanel;
import javax.swing.border.BevelBorder;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Window;



public class TestSimulator extends JFrame{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private JFrame frmTestSimulation;
    private JPanel stagePanel = new JPanel();
    private JLabel stageLabel = new JLabel("Stage");
    private JPanel settingsPanel = new JPanel();
    private JLabel settingsLabel = new JLabel("Settings");


    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    TestSimulator window = new TestSimulator();
                    window.frmTestSimulation.setVisible(true);

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public TestSimulator() {
        initialize();
    }


    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frmTestSimulation = new JFrame();
        frmTestSimulation.setTitle("WAPP Simulation");
        frmTestSimulation.setBounds(100, 100, 727, 500);
        frmTestSimulation.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frmTestSimulation.getContentPane().setLayout(null);

        //JPanel stagePanel = new JPanel();
        stagePanel.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null, null));
        stagePanel.setBounds(262, 11, 439, 439);
        frmTestSimulation.getContentPane().add(stagePanel);
        stagePanel.setLayout(null);
        stagePanel.add(new Surface());  //Show the string 'This is a test'

        //JLabel stageLabel = new JLabel("Stage");
        stageLabel.setFont(new Font("Tahoma", Font.BOLD, 14));
        stageLabel.setBounds(183, 11, 53, 34);
        stagePanel.add(stageLabel);

        //JPanel settingsPanel = new JPanel();
        settingsPanel.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null, null));
        settingsPanel.setBounds(10, 11, 242, 439);

        frmTestSimulation.getContentPane().add(settingsPanel);
        settingsPanel.setLayout(null);

        //JLabel settingsLabel = new JLabel("Settings");
        settingsLabel.setFont(new Font("Tahoma", Font.BOLD, 14));
        settingsLabel.setBounds(78, 11, 71, 22);
        settingsPanel.add(settingsLabel);

    }

    class Surface extends JPanel{

        public void paintTest(Graphics g)
        {
            Graphics2D g2d = (Graphics2D) g;
            g2d.drawString("This is a test", 50, 50);

        }

        @Override
        public void paintComponent(Graphics g) {

            super.paintComponent(g);
            paintTest(g);
        }



    }
}

Upvotes: 0

Views: 315

Answers (1)

Jezor
Jezor

Reputation: 3426

Your stagePanel is lacking a layout, so only one component inside of it will be printed. Change

stagePanel.setLayout(null);

to

stagePanel.setLayout(new GridLayout(1, 2));

And it should display something like

image of the resulting window

Refer to this website for more information about layouts in Swing and to this answer to learn more about displaying custom stuff in JPanels.

Upvotes: 3

Related Questions