-2

I'm trying to make a simple app that visualised sorting algorithms, but have gotten stuck trying to draw a rectangle (these will later represent each number in the array). I have tried alot of tutorials, and my code compiles, but it just makes an empty screen when ran. Have I missed something obvious?

import javax.swing.*;  
import java.util.ArrayList;
import java.util.Random;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Color;
import java.awt.Rectangle;
import java.awt.geom.Rectangle2D;

class DrawRectangle extends JComponent{
    public void paintComponent(Graphics g){
        Graphics2D g2=(Graphics2D) g;
        g2.setPaint(Color.PINK);
        Rectangle2D rect=new Rectangle2D.Double(50,50,200,200);
        g2.draw(rect);
        g2.fill(rect);
    }
}

public class Sorter {  
    static int numElements = 20; 
    static int width = 800;
    static int height = 500;

    public void newList(){

    }
    public static void main(String[] args) {  
        ArrayList<Integer> nums = new ArrayList<Integer>();
        Random rand = new Random();
        
        for(int i = 0; i <= numElements; i++){
            int randomNum = rand.nextInt(100);
            nums.add(randomNum);
        }

        // Create J Frame
        JFrame f=new JFrame(); 
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            
        int arrWidth = width - 200;
        int eachCol = arrWidth / nums.size();
        for(int i = 0; i <= numElements; i++){

        }
        
        f.setSize(width,height);
        f.setLayout(null);//using no layout managers  
        f.setVisible(true);//making the frame visible  
        
        DrawRectangle rec= new DrawRectangle();
        f.add(rec);
        
        f.add(new DrawRectangle());
        f.repaint();
    }  
}
Abra
  • 19,142
  • 7
  • 29
  • 41
tomjedi9
  • 19
  • 4
  • Take a look at the README for this [Bubble Sort Animation](https://github.com/ggleblanc2/bubble-sort-animation). A Swing application that animates sorting algorithms is definitely not simple. – Gilbert Le Blanc Sep 24 '21 at 15:19
  • 1) Don't use a null layout. Swing was designed to be used with layout managers 2) *i''m trying to make a simple app that visualised sorting algorithms,* - check out [Bubble Sort Animation](https://stackoverflow.com/questions/64196198/bubble-sort-animation) for a different approach to animation for the next step of your project. – camickr Sep 24 '21 at 15:33

2 Answers2

3

Usually, when you want to draw shapes, you define a class that extends javax.swing.JPanel (and not JComponent) and you override method paintComponent (as you have done).

The first line in the overridden paintComponent method should almost always be a call to the superclass method, i.e.

super.paintComponent(g);

As stated in Performing Custom Painting

in Swing, your GUI creation code should be placed on the Event Dispatch Thread (EDT)

There is no need to set the layout to null in your JFrame. There is also no need to explicitly call method repaint.

You should add all the components to the JFrame before calling method setVisible.

The following code displays a pink rectangle in a JFrame. Note that I removed the irrelevant code concerning generating random numbers since that does not affect your desired result of displaying a rectangle.

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;

import javax.swing.JFrame;
import javax.swing.JPanel;

class DrawRectangle extends JPanel {
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        Graphics2D g2=(Graphics2D) g;
        g2.setPaint(Color.PINK);
        Rectangle2D rect=new Rectangle2D.Double(50,50,200,200);
        g2.draw(rect);
        g2.fill(rect);
    }
}

public class Sorter {
    static int width = 800;
    static int height = 500;

    private void createAndDisplayGui() {
        JFrame f=new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(new DrawRectangle());
        f.setSize(width,height);
        f.setLocationByPlatform(true);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        final Sorter sorter = new Sorter();
        EventQueue.invokeLater(() -> sorter.createAndDisplayGui());
    }  
}

Note that in the tutorial, Performing Custom Painting, they call method invokeLater of class SwingUtilities. That method merely invokes the method in class EventQueue so I just call the EventQueue method directly.

Here is a screen capture.

screen capture

Abra
  • 19,142
  • 7
  • 29
  • 41
1

Your problem is that your DrawRectangle is never given any size (height & width). You could add

public Dimension getPreferredSize() {
  return new Dimension(200,200);
}

to DrawRectangle and turn the layoutmanger back on (preferred solution). Or you could manually setSize/setBounds of both the DrawRectangle and the Frame.

ControlAltDel
  • 33,923
  • 10
  • 53
  • 80
  • You can look at https://sourceforge.net/p/tus/code/HEAD/tree/tjacobs/ui/shape/ for a complete MVC solution for drawing and managing shapes in swing. Start by looking at AreaManager – ControlAltDel Sep 24 '21 at 14:54