我基本上是一个刚开始使用swing的新手。所以请容忍我在这里。我可以使用 swing 库中的预制组件来做简单的 GUI 工作。然而,现在我正试图弄清楚如何在 JPanel 上绘制基本形状。在这种情况下,它是我递归收集的 Square 对象的集合,并且应该围绕彼此同心显示。
几周前,我们做了一个涉及绘制形状的小项目,只不过这些形状是直接绘制到 JFrame 上的。现在我试图在 JPanel 或扩展 JComponent 的类中执行此操作,我遇到了太多的绊脚石。此时,JPanel 上没有显示任何内容。
这是我到目前为止的课程。
方班。这只是创建了一个简单的 Square
public class Square
{
private int x, y, width, height;
private Color theColor;
public Square(int xS, int yS, int widthS, int heightS, Color squareColor)
{
x = xS;
y = yS;
width = widthS;
height = heightS;
theColor = squareColor;
}
public void draw(Graphics2D g2)
{
g2.setColor(theColor);
Rectangle rectDraw = new Rectangle(x,y,width,height);
g2.draw(rectDraw);
}
}
图形用户界面类
public class SquareGUI extends JFrame
{
private JComboBox colorChoices, shapeChoices;
private JTextArea numberOfTimes;
private SquarePanel thisPanel;
public SquareGUI()
{
thisPanel = new SquarePanel();
JPanel northPanel = new JPanel(new FlowLayout());
setSize(640, 480);
setLayout(new BorderLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ActionListener listener = new CommandListener();
colorChoices = new JComboBox();
shapeChoices = new JComboBox();
numberOfTimes = new JTextArea(1,3);
colorChoices.addItem("Black");
colorChoices.addItem("Blue");
colorChoices.addItem("Red");
colorChoices.addItem("Green");
shapeChoices.addItem("Square");
shapeChoices.addItem("Circle");
colorChoices.addActionListener(listener);
shapeChoices.addActionListener(listener);
northPanel.add(colorChoices);
northPanel.add(shapeChoices);
northPanel.add(new JLabel("Number of Shapes:"));
northPanel.add(numberOfTimes);
add(northPanel, BorderLayout.NORTH);
add(thisPanel, BorderLayout.CENTER);
setVisible(true);
}
public void addShapesRecursively(int x, int y, int width, int height, int times)
{
if (times == 0) { return; }
Color colorChoice = null;
switch (colorChoices.getSelectedIndex())
{
case 0: colorChoice = Color.BLACK; break;
case 1: colorChoice = Color.BLUE; break;
case 2: colorChoice = Color.RED; break;
case 3: colorChoice = Color.GREEN; break;
}
if (shapeChoices.getSelectedIndex() == 0)
thisPanel.add(new Square(x, y, width, height, colorChoice));
else
System.out.println("todo");
addShapesRecursively(x-15, y-15, width + 15, height + 15, times - 1);
}
class CommandListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent arg0)
{
addShapesRecursively(getWidth()/2,getHeight()/2,20,20,Integer.parseInt(numberOfTimes.getText()));
}
}
public static void main(String[]args)
{
new SquareGUI();
}
}
还有我的 JPanel 类,它应该显示正方形。
public class SquarePanel extends JPanel
{
private ArrayList<Square> squareList;
public SquarePanel()
{
setBackground(Color.WHITE);
squareList = new ArrayList<Square>();
}
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
for (int i = 0; i < squareList.size(); i++)
{
Square tempSquare = squareList.get(i);
tempSquare.draw(g2);
}
}
public void add(Square addSquare)
{
squareList.add(addSquare);
}
}
我很抱歉,目前还没有评论等。只是把我的头发拉出来试图让它发挥作用。我知道递归位有效,因为 ArrayList 运行后有 x 个 Square 对象。这只是在 JPanel 上绘制这些方块的问题。
我首先尝试了一个扩展 JComponent 的单独类,但永远无法让被覆盖的paintComponent 在其中触发。所以我环顾四周,发现你也可以在 JPanel 中覆盖paintComponent。所以它按预期触发,但 JPanel 本身没有出现任何内容。
我的总体问题是,如何让方块正确显示?