I'm trying to get my JPanel (JFrame?) to resize itself to fit randomly drawn shapes. I have a shape superclass and right now I'm working with my line subclass. Everything is working fine except I can't figure out how to make the shapes fit the size of the JPanel.

Should I limit the size of the shapes or could someone suggest a way to dynamically set the size of the Jpanel based on the maximum size of the shapes? (I'll eventually want to do this with several shape types in the same panel). I'm thinking a for loop grabbing the maximum size of a shape and setting height and width to this might work, but I invoke the JFrame before the Panel randomly draws the shapes.

Thanks in advance

Recommended Answers

All 3 Replies

if you do set up a for loop grabbing the max x and y of your shape coords, then simply compare them to the current size of your JFrame and if it isnt big enough to display them set its size accordingly, if youd rather resize the JPanel you are drawing on you can set its preferred size with the big enough size and then call YourJframe.pack() to resize the Jframe to the sizes of its content.

Thanks, however if I pack() I end up with my Jframe being 0 by 0 and the size of my JPanel seems off.

I created a static method to restrict the co-ordinates to start but they're still going off the side of the window. Classes below.

Class to draw lines -

import java.awt.Color;
import java.awt.Graphics;

public class MyLine {
    private int x1;
    private int y1;
    private int x2;
    private int y2;
    private Color myColor;
    
    public MyLine (int x1, int y1, int x2, int y2, Color color) {
        this.x1 = DimensionCheck.coords(x1);
        this.y1 = DimensionCheck.coords(y1);
        this.x2 = DimensionCheck.coords(x2);
        this.y2 = DimensionCheck.coords(y2);
        myColor = color;
    }
    
    public void draw (Graphics g) {
        g.setColor(myColor);
        g.drawLine(x1,y1,x2,x2);
    }
}

Class to check co-ordinates

public class DimensionCheck 
{
    public static int coords (int i)
    {
        if (i >= 0 && i <= 300)
        {
            return i;
        }
        else if(i < 0)
        {
            return 0;
        }
        else
        {
            return 300;
        }
    }

class to draw panel with random MyLines (I've even set the random to generate max 300 here)

import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;
import javax.swing.JPanel;
import java.awt.*;

public class DrawPanel extends JPanel {

    private Random randomNumbers = new Random();
    private MyLine[] lines;

    public DrawPanel() {
        setBackground(Color.white);
        Dimension prefsize = new Dimension(300,300);
        setPreferredSize(prefsize);

        lines = new MyLine[5 + randomNumbers.nextInt(5)];

        for (int count = 0; count < lines.length; count++) {
            int x1 = randomNumbers.nextInt(300);
            int x2 = randomNumbers.nextInt(300);
            int y1 = randomNumbers.nextInt(300);
            int y2 = randomNumbers.nextInt(300);

            Color color = new Color(randomNumbers.nextInt(256),
                    randomNumbers.nextInt(256), randomNumbers.nextInt(256));

            lines[count] = new MyLine(x1, y1, x2, y2, color);
        } 
       
    }
    
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        for (MyLine line : lines) {
            line.draw(g);
        }
        
    }
}

Finally my Jpanel class

import javax.swing.JFrame;

public class TestDraw {
    public static void main (String[]args){
        DrawPanel panel = new DrawPanel();
        JFrame application = new JFrame();
        
        application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        application.add(panel);
        application.setSize(300,300);
        application.setVisible(true);
    }
}

This is one of those ones where you know it's something simple but I've been staring at it for too long I think :(

Well I'm not sure what changed but I deleted the application.setSize and put pack() in there instead and it seems to have worked. However if I take preferredsize out of the panel class it reverts to 0x0. Either way it's working now so I'm happy, I can break it later to see what exactly takes care of it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.