konze
konze

Reputation: 893

arrange non rectangular JPanels in Java Swing

I've defined a new class LShapePanel which extends JPanel and which looks like an L.

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

public class LShapePanel extends JPanel{

    public Color color;

    public LShapePanel(Color color) {
        this.color = color;
    }

    @Override
    public void paintComponent(Graphics g) {

        super.paintComponent(g);

        Graphics2D g2d = (Graphics2D) g;

        g2d.setColor(color);

        /* coordinates for polygon  */
        int[] xPoints = {0,100,100,20,20,0};
        int[] yPoints = {0,0,20,20,100,100};

        /* draw polygon */
        g2d.fillPolygon(xPoints, yPoints, 6);
    }
}

I'd like to arrange two of these LShapePanels like this:

enter image description here

But I don't know how? Here is my code for arranging two LShapePanels in a row.

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

import java.awt.Color;
import java.awt.Dimension;

public class DifferentShapes extends JFrame {

    public DifferentShapes() {

        setTitle("different shapes");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocation(500, 300);

        JPanel panel = new JPanel();

        /* create and add first L in red */
        LShapePanel lsp1 = new LShapePanel(new Color(255,0,0));
        lsp1.setPreferredSize(new Dimension(100,100));
        panel.add(lsp1);

        /* create and add second L in green*/
        LShapePanel lsp2 = new LShapePanel(new Color(0,255,0));
        lsp2.setPreferredSize(new Dimension(100,100));
        panel.add(lsp2);

        add(panel);

        pack();

    }

    public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                DifferentShapes df = new DifferentShapes();
                df.setVisible(true);
            }
        });
    }
}

And the result:

enter image description here

Upvotes: 0

Views: 380

Answers (2)

GLaDOS
GLaDOS

Reputation: 76

You need to use layout manager to arrange the components in the JFrame. According to this turorial, the content pane, which actually contains the components you put into JFrame, uses Borderlayout by default. In an "L" shape as the LShapePanel looks, it's actually a rectangle(every component in swing is a rectangle, as a matter of fact) with part of it transplant. So if you want to arrange the panels in the way you want, they will have to overlap with each other. Different kinds of layout managers use different layout strategies, and Borderlayout won't allow components to overlap, so you have to change to another layout manager. Sorry that I don't know any layout manager that allows components to overlap, but you can use JLayeredPane to achieve you goal. Add a JLayeredPane to the JFrame and then add the LShapePanels to the JLayeredPane.

Upvotes: 1

tbodt
tbodt

Reputation: 16987

Sorry, layout manager enthusiasts, but I can't think of any way other than using setLocation and a null layout manager. Here's a demonstration:

setLayout(null);
LShapePanel lsp1 = new LShapePanel(new Color(255,0,0));
lsp1.setPreferredSize(new Dimension(100,100));
lsp1.setLocation(0,0);
add(lsp1);

LShapePanel lsp2 = new LShapePanel(new Color(0,255,0));
lsp2.setPreferredSize(new Dimension(100,100));
lsp2.setLocation(30,30);
add(lsp2);

Upvotes: 0

Related Questions