Java - 尝试一次绘制多个形状,有些缺失

时间:2017-10-14 03:57:57

标签: java swing paintcomponent jinternalframe

我试图在JInternalFrame的contentPane中绘制多个形状。单击按钮时,程序应确定选择了哪些复选框,并添加适当的形状。如果选择了多个形状,则应添加所有形状。

目前该程序可以绘制任何形状。问题是,点击时,当我选中多个复选框时,只绘制一个形状,我无法弄清楚原因。 (示例:选择黄色和蓝色JCheckbox,它只绘制黄色形状,同时绘制两者)请帮助我让程序在点击时绘制多个形状!

以下是一些代码:

用于处理形状添加的ActionListener:

class addListener implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        if (redBox.isSelected())
        {
            x = createRNG(RNG_MAX,RNG_MIN);//so the shapes starting location is random
            y = createRNG(RNG_MAX,RNG_MIN);
            rShape = new redShape(x,y);
            contentPane.add(rShape);//,BorderLayout.CENTER);
        }
        if (blueBox.isSelected())
        {
            x = createRNG(RNG_MAX,RNG_MIN);
            y = createRNG(RNG_MAX,RNG_MIN);
            bShape = new bShape(x,y);
            //internalFrame.getContentPane().add(bShape);
            contentPane.add(bShape);//BorderLayout.CENTER);
        }
        if (yellowBox.isSelected())
        {
            x = createRNG(RNG_MAX,RNG_MIN);
            y = createRNG(RNG_MAX,RNG_MIN);
            yShape = new yShape(x,y);
            //internalFrame.getContentPane().add(yShape);
            contentPane.add(yShape);//,BorderLayout.CENTER);

        }
        internalFrame.repaint();
        internalFrame.revalidate();
    }
}

其中一个形状类的代码。这些类的代码基本相同,只是绘制了不同的形状。

import javax.swing.*;
import java.awt.*;

public class redShape extends coloredShapes
{
private static final int RECTANGLE_WIDTH = 80;
private static final int RECTANGLE_HEIGHT = 20;

private int xLeft;
private int yTop;

public redShape(int x, int y)
{
    xLeft = x;
    yTop = y;
}

public void paintComponent(Graphics g)
{
    //draws 1 large rectangle
    g.setColor(Color.red);
    g.fillRect(xLeft,yTop,RECTANGLE_WIDTH,RECTANGLE_HEIGHT);

    //Divides the rectangle into parts
    g.setColor(Color.black);
    g.drawLine(xLeft, yTop,xLeft+80,yTop);
    g.drawLine(xLeft,yTop,xLeft,yTop+20);
    g.drawLine(xLeft,yTop+20,xLeft+80,yTop+20);
    g.drawLine(xLeft+80,yTop+20,xLeft+80,yTop);
    g.drawLine(xLeft+20,yTop,xLeft+20,yTop+20);
    g.drawLine(xLeft+40,yTop,xLeft+40,yTop+20);
    g.drawLine(xLeft+60,yTop,xLeft+60,yTop+20);
}

public void moveComponentBy(int dx, int dy)
{
    if (yTop < 450)
    {
        xLeft += dx;
        yTop += dy;
        if (xLeft < 0)
        {
            xLeft = 0;
        }
        if (xLeft > 500)
        {
            xLeft = 500;
        }
        repaint();
    }
}

public int getYTop()
{
    return yTop;
}

public int getXLeft()
{
    return xLeft;
}
}

这是我认为相关的代码,如果需要进一步的代码来确定问题,请询问,我会提供它!

编辑: coloredShapes抽象类,根据要求。

import java.util.*;
import javax.swing.*;
abstract public class coloredShapes extends JComponent
{     
private static final int RECTANGLE_WIDTH = 10;
private static final int RECTANGLE_HEIGHT = 10;

private int xLeft;
private int yTop;

public coloredShapes()
{
    xLeft = 0;
    yTop = 0;
}

public abstract void moveComponentBy(int dx, int dy);

public abstract int getYTop();

public abstract int getXLeft();
}

1 个答案:

答案 0 :(得分:1)

我假设您Borderlayout使用JInternalFrame 从我看到的情况来看,我假设您将所有形状添加到JInternalFrame中:contentPane.add(bShape);bShape添加到Borderlayout中心。

Borderlayout中心可以容纳一个组件。添加第二个会覆盖第一个。
对于测试,您可以setLayout(null);JInternalFrame(并设置添加的组件的边界)。有关更强大的解决方案,请参阅here
还要确保您绘制的背景是透明的。有关详细信息,请参阅here