你好,好吧,这是我的问题:我必须通过添加三种不同的类型来创建一个小程序的GUI组件,以便用户可以从以下选项中进行选择:
- 数字数量:1、2、4、8、16或这些数字的各种组合数字
- 图形类型:圆形、椭圆形、矩形或正方形
- 颜色:红色、蓝色、绿色、黄色、粉色、黑色、青色或品红色
我已经完成了设计,但问题是绘制用户选择的图像
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Ovals extends JApplet implements ItemListener
{
private JCheckBox circleCB,ovalCB,rectangleCB,squareCB;
private Color currentColor = Color.black;
private JRadioButton redRB, greenRB, blueRB, yellowRB, pinkRB, cyanRB, magentaRB, blackRB;
private ButtonGroup ColorSelectBGroup;
private JComboBox numFig;
int Figure;
int num;
public String[] figNum = {"1", "2", "4", "8", "16", "32", "64", "128"};
public void init()
{
Container c = getContentPane();
c.setLayout(null);
circleCB = new JCheckBox("Circle");
ovalCB = new JCheckBox("Oval");
rectangleCB = new JCheckBox("Rectangle");
squareCB = new JCheckBox("Square");
redRB = new JRadioButton("Red");
greenRB = new JRadioButton("Green");
blueRB = new JRadioButton("Blue");
yellowRB = new JRadioButton("Yellow");
pinkRB = new JRadioButton("Pink");
cyanRB = new JRadioButton("Cyan");
magentaRB = new JRadioButton("Magenta");
blackRB = new JRadioButton("Black");
numFig = new JComboBox(figNum);
numFig.setMaximumRowCount(8);
circleCB.setSize(80, 30);
ovalCB.setSize(80, 30);
rectangleCB.setSize(80, 30);
squareCB.setSize(80, 30);
redRB.setSize(80, 30);
greenRB.setSize(80, 30);
blueRB.setSize(80, 30);
yellowRB.setSize(80, 30);
pinkRB.setSize(80, 30);
cyanRB.setSize(80, 30);
magentaRB.setSize(80, 30);
blackRB.setSize(80, 30);
numFig.setSize(80, 30);
circleCB.setLocation(100, 70);
ovalCB.setLocation(100, 110);
rectangleCB.setLocation(100, 150);
squareCB.setLocation(100, 190);
redRB.setLocation(300, 70);
greenRB.setLocation(300, 110);
blueRB.setLocation(300, 150);
yellowRB.setLocation(300, 190);
pinkRB.setLocation(300, 230);
cyanRB.setLocation(300, 270);
magentaRB.setLocation(300, 310);
blackRB.setLocation(300, 350);
numFig.setLocation(200, 70);
circleCB.addItemListener(this);
ovalCB.addItemListener(this);
rectangleCB.addItemListener(this);
squareCB.addItemListener((ItemListener) this);
redRB.addItemListener(this);
greenRB.addItemListener(this);
blueRB.addItemListener(this);
yellowRB.addItemListener(this);
pinkRB.addItemListener(this);
cyanRB.addItemListener(this);
magentaRB.addItemListener(this);
blackRB.addItemListener(this);
numFig.addItemListener(this);
c.add(circleCB);
c.add(ovalCB);
c.add(rectangleCB);
c.add(squareCB);
c.add(redRB);
c.add(greenRB);
c.add(blueRB);
c.add(yellowRB);
c.add(pinkRB);
c.add(cyanRB);
c.add(magentaRB);
c.add(blackRB);
c.add(numFig);
ColorSelectBGroup = new ButtonGroup();
ColorSelectBGroup.add(redRB);
ColorSelectBGroup.add(greenRB);
ColorSelectBGroup.add(blueRB);
ColorSelectBGroup.add(yellowRB);
ColorSelectBGroup.add(pinkRB);
ColorSelectBGroup.add(cyanRB);
ColorSelectBGroup.add(magentaRB);
ColorSelectBGroup.add(blackRB);
}
public void paint (Graphics g)
{
super.paint(g);
g.setColor(Color.orange);
g.drawLine(183, 50, 183, 350);
g.drawLine(291, 50, 291, 350);
}
public void itemStateChanged(ItemEvent e)
{
for ( int i = 0; i < 10; i++ )
{
switch( Figure )
{
case e.getSource() == circleCB:
(e.getStateChange() == ItemEvent.SELECTED)
e.drawLine( 10, 10, 250, 10 + i * 10 );
break;
case e.getSource() == rectangleCB:
(e.getStateChange() == ItemEvent.SELECTED)
e.drawRect( 10 + i * 10, 10 + i * 10, 50 + i * 10, 50 + i * 10 );
break;
case e.getSource() == ovalCB:
(e.getStateChange() == ItemEvent.SELECTED)
e.drawOval( 10 + i * 10, 10 + i * 10, 50 + i * 10, 50 + i * 10 );
break;
case e.getSource() == squareCB:
(e.getStateChange() == ItemEvent.SELECTED)
e.drawRect( 10 + i * 10, 10 + i * 10, 50 + i * 10, 50 + i * 10 );
break;
default:
}
if (e.getSource() == redRB)
currentColor = Color.red;
else if (e.getSource() == greenRB)
currentColor = Color.green;
else if (e.getSource() == blueRB)
currentColor = Color.blue;
else if (e.getSource() == yellowRB)
currentColor = Color.yellow;
else if (e.getSource() == pinkRB)
currentColor = Color.pink;
else if (e.getSource() == cyanRB)
currentColor = Color.cyan;
else if (e.getSource() == magentaRB)
currentColor = Color.magenta;
else if (e.getSource() == blackRB)
currentColor = Color.black;
repaint();
}
}
}
我的下一步是什么我必须调整我的itemStateChanged和绘制方法,但我不确定如何进行,请帮助
建议:
- 有一个扩展JPanel的类,并在其
paintComponent
重写方法中进行所有绘图 - 不要忘记在重写中调用super方法,通常是第一件事
- 然后将这个JPanel加载到您的JApplet的contentPane的BorderLayout.CENTER位置
- 在paintComponent方法中,如果块检查类中变量的状态,例如Color变量、数字变量、形状的布尔值,并根据这些变量的状态绘制适当的形状
- 在监听器中,更改上述变量的状态,然后调用
repaint()
来重新绘制JPanel并显示新形状
查看自定义绘画方法。我建议您使用DrawOnComponent
示例,因为它允许您将要绘制的对象添加到ArrayList中。当前代码使用了一个"ColoredRectangle"类,因此您需要将其更改为使用"ColoredShape"类,以便绘制不同形状的对象。
然后,您可能需要查看"使用图形",它将向您展示如何使用Shape
类而不是使用特定的图形绘制方法进行绘制。这使您的代码更加灵活。
把这两个建议放在一起,你就有了可能的解决方案。