0

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



    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
RayG
  • 1
  • 2
    I recommend avoiding null layouts - choose the [Layout Manager](https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html) that best fits your needs, and remember that you can nest layouts within child components – copeg Jun 28 '16 at 21:02
  • Thanks copeg. I changed to Box Layout as a test. I can now see the string. Cant believe it. Was at it for at least 4 hours. – RayG Jun 28 '16 at 21:06
  • @copeg arr, you beat me to it, I was struggling with the debugger for a while – Jezor Jun 28 '16 at 21:13

1 Answers1

3

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.

Community
  • 1
  • 1
Jezor
  • 3,253
  • 2
  • 19
  • 43