问答题

综合应用题 下面是一个Applet程序,实现实心或者空心矩形的绘制。要求鼠标在Applet窗口中拖动实现矩形的绘制,可以选择填充或者空心,有按钮用来清空窗口中的图像。请改正程序中的错误(有下划线的语句),使程序能输出正确的结果。 注意:不改动程序的结构,不得增行或删行。 程序运行结果如下:
import java.awt.*; import java.applet.*; /* */ public class ex13_3 extends Applet{ private Button btnClear; private CheckboxGroup cbg; private Checkbox chk1, chk2; private int upX, upY; private int downX, downY; private boolean bDraw, bClear, bStyle; public void init(){ bDraw = false; bClear = false; bStyle = false; setLayout(null); cbg = new CheckboxGroup(); chk1 = new Checkbox("Hollow", cbg, true); chk2 = new Checkbox("Filled", cbg, false); chk1.reshape(80,getSize().height-80,80,25); chk2.reshape(160,getSize().height-80,80,25); add(chk1); add(chk2); btnClear = new Button("clear"); btnClear.reshape(240,getSize().height-80,80,25); add(btnClear); } public void paint(Graphics g){ if(bDraw){ if(upX > downX && upY > downY){ if(bStyle) g.fillRect(downX, downY, upX - downX, upY - downY); else g.drawRect(downX, downY, upX - downX, upY - downY); } else if(upX > downX && upY < downY){ if(bStyle) g.fillRect(downX, upY, upX - downX, downY - upY); else g.drawRect(downX, upY, upX - downX, downY - upY); } else if(upX < downX && upY > downY){ if(bStyle) g.fillRect(upX, downY, downX - upX, upY - downY); else g.drawRect(upX, downY, downX - upX, upY - downY); } else if(upX < downX && upY < downY){ if(bStyle) g.fillRect(upX, upY, downX - upX, downY - upY); else g.drawRect(upX, upY, downX - upX, downY - upY); } bDraw = false; } if(bClear){ g.clearRect(0,0, getSize().height, getSize().width); bClear = false; } } public void update(Graphics g){ paint(g); } public boolean mouseUp(Event e, int x, int y){ upX = x; upY = y; bDraw = true; repaint(); return true; } public boolean mouseDown(Event e, int x, int y){ downX = x; downY = y; return true; } public boolean action(Event e, Object o){ if(e.target == btnClear){ bClear = true; repaint(); } if(chk1.getState() == true){ bStyle = true; } if(chk2.getState() == true){ bStyle = false; } return true; } } ex13_3.html ex13_3

【参考答案】

g.clearRect(0,0,getSize().width,getSize().height)bStyle = fa......

(↓↓↓ 点击下方‘点击查看答案’看完整答案、解析 ↓↓↓)
热门 试题

多项选择题
简单应用题请完成下列Java程序:制作一个图形用户界面,上方包含一个TextField和Button构件,实现输入字符串,点击Button获取文本区的字符;中间显示Label的内容;下方是4个按钮,分别实现控制Label在最左边,在中间,在右边和退出程序的功能。注意:请勿改动main( )主方法和其他已有语句内容,仅在下划线处填入适当的语句。程序运行结果如下:import java.awt.*;import java.awt.event.*;public class ex15_2 extends Frame implements ActionListener {private Label l;private TextField tf;public static void main(String[] arg) {ex15_2 obj15_2 = new ex15_2();}public ex15_2() {setBackground(Color.gray);l = new Label( Welcom to the NCR Examination! );Font font = new Font( TimesRoman ,Font.BOLD,20);l.setFont(font);add( Center ,l);Panel p = new Panel();Button b = new Button( Left );b.addActionListener(this);p.add(b);b = new Button( Center );b.addActionListener(this);p.add(b);b = new Button( Right );b.addActionListener(this);p.add(b);b = new Button( Exit );b.addActionListener(this);p.add(b);;p = new Panel();tf = new TextField(40);p.add(tf);b = new Button( Set );b.addActionListener(this);p.add(b);add( North ,p);setSize(500,300);show();}public void actionPerformed(ActionEvent ae) {if(ae.getActionCommand().equals( Exit ))System.exit(0);else if(ae.getActionCommand().equals( Left ));else if(ae.getActionCommand().equals( Center ))l.setAlignment(Label.CENTER);else if(ae.getActionCommand().equals( Right ))l.setAlignment(Label.RIGHT);else if(ae.getActionCommand().equals( Set ))l.setText(tf.getText());}}