JAVA面板的叠加,如何将2个Java swing元素彼此叠加?

so I have this project that I'm doing, which draws a circle and a square, and resizes them through buttons. Everything works great, however what I must do is put the circle inside the square, so that the circle is inscribed, and I can't seem to figure out how to put both swing elements on top of each other.

Here's a picture of how it currently is:

85FV8.jpg

And here's a photoshop version of how it should look like:

LMWej.jpg

Here's my Circle class:

import java.awt.Canvas;

import java.awt.Color;

import java.awt.Dimension;

import java.awt.Graphics;

import java.beans.PropertyChangeEvent;

import java.beans.PropertyChangeListener;

import java.beans.PropertyVetoException;

import java.beans.VetoableChangeListener;

public class Picture extends Canvas implements VetoableChangeListener, PropertyChangeListener {

private final int SIZE = 100;

private int radius = 1;

public Picture() {

setSize(SIZE,SIZE);

}

public void vetoableChange(PropertyChangeEvent pce) throws PropertyVetoException {

if ((pce.getPropertyName()).equals("value")) {

int v = (Integer)pce.getNewValue();

if ((v <=0)||(v > SIZE/2))

throw new PropertyVetoException ("Value out of bounds!", pce);

}

}

public void propertyChange(PropertyChangeEvent pce) {

if ((pce.getPropertyName()).equals("value")) {

setRadius((Integer)pce.getNewValue());

repaint();

}

}

public void setRadius(int radius) {

this.radius = radius;

}

public int getRadius() {

return this.radius;

}

public void paint (Graphics g) {

Dimension d = getSize();

g.setColor(Color.GREEN);

g.fillOval(d.width/2 - radius, d.height/2 - radius, radius*2, radius*2);

}

}

And here's my Square class, which is similar:

import java.awt.Canvas;

import java.awt.Color;

import java.awt.Dimension;

import java.awt.Graphics;

import java.beans.PropertyChangeEvent;

import java.beans.PropertyChangeListener;

import java.beans.PropertyVetoException;

import java.beans.VetoableChangeListener;

public class Square extends Canvas implements VetoableChangeListener, PropertyChangeListener {

private final int SIZE = 100;

private int side = 1;

public Square() {

setSize(SIZE,SIZE);

}

public void vetoableChange(PropertyChangeEvent pce) throws PropertyVetoException {

if ((pce.getPropertyName()).equals("value")) {

int v = (Integer)pce.getNewValue();

if ((v <=0)||(v > SIZE/2))

throw new PropertyVetoException ("Value out of bounds!", pce);

}

}

public void propertyChange(PropertyChangeEvent pce) {

if ((pce.getPropertyName()).equals("value")) {

setSide((Integer)pce.getNewValue());

repaint();

}

}

public void setSide(int side) {

this.side = side;

}

public int getSide() {

return this.side;

}

public void paint (Graphics g) {

Dimension d = getSize();

g.setColor(Color.BLUE);

g.drawRect(d.width/2 - side, d.height/2 - side, side*2, side*2);

}

}

I've added both of them to the palette. Would you please explain to me how to put those both elements on top of each other, I'm new to Swing and JavaBeans.

Thanks in advance!

解决方案

First of all you should not be extending Canvas, that is an AWT component. In Swing you can extend JComponent (or JPanel) and then you add custom painting to the paintComponent(...) method. Read the section from the Swing tutorial on Custom Painting for more information.

Also you should make each component transparent (by using setOaque(false) in the constructor of the component so that the background of one component does not cover the other.

I've added both of them to the palette.

And they both display on top of the palette because of the rules of the layout manager being used:

So you have a couple of options:

Take advantage of Swings parent/child relationship. That is add the square to the palette and the circle to the square. This means you will need to set a layout manager on the square so you can add the circle to it. A BorderLayout might be easy to use.

Create a panel using the OverlayLayout. This layout manager allows you to add two components on top of one another in the same panel. You would add the square to the panel and the circle to the panel (in that order since Swing paints the last component added to a panel first).

A completely different (and probably easier) option is to have a single component and paint both the square and circle in the paintComponent() method.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值