Bob
Bob

Reputation: 23

Gui Swing drawing objects are not showing up

I need help painting/drawing objects on a certain panel. I would like to draw stuff in the "drawPanel" JPanel. Can someone take a look at this and tell me what I am missing? I would appreciate it, I am sorry if I'm missing something way obvious.

When I run the program, nothing is drawn.

package redball;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;

public class drawTest extends JFrame {

    private JPanel contentPane;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    drawTest frame = new drawTest();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public drawTest() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);


        JPanel leftPanel = new JPanel();
        contentPane.add(leftPanel, BorderLayout.WEST);

        JLabel lblNewLabel = new JLabel("some stuff");
        leftPanel.add(lblNewLabel);

        JPanel drawPanel = new JPanel(); //this is where I want to draw stuff
        contentPane.add(drawPanel, BorderLayout.CENTER);


    }

    protected void paintComponent(Graphics g) {
        super.paintComponents(g); 

        g.setColor(Color.black);
        g.fillRect(25, 500, 100, 20);
    }

}

Upvotes: 1

Views: 271

Answers (1)

Andrew Thompson
Andrew Thompson

Reputation: 168825

General tips (the first two of which would help solve this):

  1. Use @Override notation on any altered method! (That code is trying to override a method that does not exist. Even if it was implemented in a panel, it is calling the wrong super method.)
  2. A custom painted component should suggest a preferred size.
  3. Please learn common Java nomenclature (naming conventions - e.g. EachWordUpperCaseClass, firstWordLowerCaseMethod(), firstWordLowerCaseAttribute unless it is an UPPER_CASE_CONSTANT) and use it consistently.

This is the result of implementing the above advice, with a few more tips mentioned in comments in the code.

enter image description here

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

public class DrawTest extends JFrame {

    private JPanel contentPane;

    public DrawTest() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // instead pack() once components are added
        // setBounds(100, 100, 450, 300); 
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);

        JPanel leftPanel = new JPanel();
        leftPanel.setBackground(Color.CYAN);
        contentPane.add(leftPanel, BorderLayout.LINE_START);

        JLabel lblNewLabel = new JLabel("some stuff");
        leftPanel.add(lblNewLabel);

        JPanel drawPanel = new JPanel() {

            Dimension preferredSize = new Dimension(300, 100);

            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);

                g.setColor(Color.black);
                // 500? That's much deeper than the panel shows on-screen!
                //g.fillRect(25, 500, 100, 20);
                g.fillRect(25, 50, 100, 20);
            }

            @Override
            public Dimension getPreferredSize() {
                Dimension d = super.getPreferredSize();
                if (d.width<50 || d.height<50) {
                    return preferredSize;
                } else {
                    System.out.println("d: " + d);
                    return d;
                }
            }
        };
        drawPanel.setBackground(Color.YELLOW);
        contentPane.add(drawPanel, BorderLayout.CENTER);

        pack();
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    DrawTest frame = new DrawTest();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
}

Upvotes: 1

Related Questions