0

So I am new in java graphics and I am creating a program that will show a rectangle. But when I run my program it only show like a small box and not the rectangle. I don't really know why it is happening.

Here is my code:

import javax.swing.*;

public class GraphicsEditor{

    
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        JPanel panel = new JPanel();
        
        Rectangle rectangle = new Rectangle();
        
        frame.setSize(1280, 720);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        frame.setVisible(true);
        frame.add(panel);
        panel.add(rectangle);
        
    }
    
}

This is my rectangle class:

import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;

public class Rectangle extends JPanel implements Shape {
    
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2D = (Graphics2D) g;
        g2D.fillRect(0, 0, 200, 130);
    }

}

This is my shape interface:

import java.awt.*;

public interface Shape {
    void paintComponent(Graphics g);
}
camickr
  • 321,443
  • 19
  • 166
  • 288
akolangto
  • 33
  • 1
  • 6

2 Answers2

2

Here, try this

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;

public class GraphicsEditor {
    
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        Rectangle rectangle = new Rectangle();
        
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(rectangle);
        frame.pack();
        // center frame on screen
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        
    }
    
    
}
class Rectangle extends JPanel {
    
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2D = (Graphics2D) g;
        g2D.fillRect(0, 0, 200, 130);
    }
    
    @Override
    public Dimension getPreferredSize() {
        return new Dimension(500, 500);
    }
}

A couple of things.

  • you don't need the interface.
  • unlike components, just painting a picture doesn't affect the layout manager, so the panel will be reduced to it's default size with out regard to any painting.
  • so you need to override getPreferredSize() in your JPanel.
WJS
  • 36,363
  • 4
  • 24
  • 39
0

As the comments said, you should set the preferred size of both your panel and rectangle to your desired size, and then pack the frame, like:

panel.setPreferredSize(new Dimension(500,500));
rectangle.setPreferredSize(new Dimension(500,500));
frame.pack();

Otherwise your LayoutManager (when not specified it defaults to FlowLayout) will handle your rectangle the way it wants. So another way would be learning about Layout Managers, and using your desired one.

As a side note, I would like to make some suggestions to your code. Remember, Swing is not thread safe, so place your code inside an invokeLater() call, such as:

SwingUtilities.invokeLater(new Runnable() {
    @Override
    public void run() {
            JFrame frame = new JFrame();
            JPanel panel = new JPanel();

            Rectangle rectangle = new Rectangle();

            frame.setSize(1280, 720);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            panel.setPreferredSize(new Dimension(500,500));
            rectangle.setPreferredSize(new Dimension(500,500));

            frame.add(panel);
            panel.add(rectangle);
            frame.pack();
            frame.setVisible(true);
    }
});

Also, calling frame.setVisible(true) should be called after adding your components.

CLR 123
  • 161
  • 1
  • 8
  • The solution by WJS is preferable. Don't use setPreferredSize(). Each component should determine its own preferred size by overrring the getPreferredSize() method. – camickr Mar 22 '21 at 14:51
  • Yes, I think either will work out for OP, but WJS's solution should be preferred, I only noticed after reading his reply – CLR 123 Mar 22 '21 at 14:58