【Java开源代码栏目提醒】:网学会员Java开源代码为您提供RadioButton2DTest.java参考,解决您在RadioButton2DTest.java学习中工作中的难题,参考学习。
import java.applet.*;
import java.awt.*;
public class RadioButton2DTest extends Applet implements Runnable
{
// 动画线程
private Thread animation;
private BufferedGraphics offscreen;
// 是否禁用超级能量列表的单选按钮
private RadioButton2D singleRB;
// 跟踪超级能量是否可用
private boolean powersEnabled;
// 放置超级能量
列表的按钮组
private RadioButtonGroup rbGroup;
// 可用的超级能量的字符串描述
private final String[] POWERS = { "Fireball", "Super Kick", "Acid Storm", "Razor Talons", "Electroshock" };
private final int NUM_BUTTONS = POWERS.length;
// 描述选择状态的标签
private Label2D status;
public void init()
{
// 为按钮创建一个按钮组
ButtonImageGroup group = new ButtonImageGroup(2, "radio.gif");
group.init(this);
Label2D label;
Font font = new Font("Helvetica", Font.PLAIN, 18);
// 创建切换超级能量是否可用的单选按钮
label = new Label2D(font, "Enable Superpowers", Color.WHITE);
singleRB = new RadioButton2D(label, group, null, new Vector2D.Double(50, 50));
label.centerOn(singleRB.getBounds(), (Graphics2D) getGraphics());
label.setX(singleRB.getBounds().getX() + singleRB.getBounds().getWidth() + 5);
singleRB.setSelected(true);
powersEnabled = singleRB.isSelected();
addMouseListener(singleRB);
// 创建容纳不同能量的单选按钮组
RadioButton2D rb;
rbGroup = new RadioButtonGroup();
for(int i = 0; i < NUM_BUTTONS; i++)
{
label = new Label2D(font, POWERS[i], Color.WHITE);
rb = new RadioButton2D(label, group, rbGroup, new Vector2D.Double(100, 100+(i*35)));
label.centerOn(rb.getBounds(), (Graphics2D) getGraphics());
label.setX(rb.getBounds().getX() + rb.getBounds().getWidth() + 5);
addMouseListener(rb);
// 默认情况下,第0个按钮应该被选中
if(i == 0) rb.setSelected(true);
}
// 用初始的空白字符串创建状态标签
status = new Label2D(font, "", new Color(0, 255, 255));
status.setPos(new Vector2D.Double(50, 325));
offscreen = new BufferedGraphics(this);
AnimationStrip.observer = this;
} // init
public void start()
{
// 启动动画线程
animation = new Thread(this);
animation.start();
}
public void stop()
{
animation = null;
}
public void run()
{
Thread t = Thread.currentThread();
while (t == animation)
{
repaint();
try
{
Thread.sleep(10);
}
catch(InterruptedException e)
{
break;
}
}
} // run
public void update(Graphics g)
{
// 如果独立的那个选项改变,则切换能量选项组的状态
if(powersEnabled != singleRB.isSelected())
{
powersEnabled = singleRB.isSelected();
rbGroup.setVisible(powersEnabled);
}
// 更新标签的描述
if(powersEnabled == true && rbGroup.getSelection() != null)
{
status.setText("Selected superpower: " + rbGroup.getSelection().getText());
}
else
{
status.setText("Superpowers disabled");
}
paint(g);
}
public void paint(Graphics g)
{
Graphics2D bg = (Graphics2D)offscreen.getValidGraphics();
bg.setPaint(Color.BLACK);
bg.fillRect(0, 0, getSize().width, getSize().height);
// 绘制单独的那个按钮
singleRB.paint(bg);
// 绘制按钮组
rbGroup.paint(bg);
// 绘制状态标签
status.paint(bg);
g.drawImage(offscreen.getBuffer(), 0, 0, this);
} // paint
} // RadioButton2DTest