int x=0,y=0; Mycanvas(){
setSize(400,400);
setBackground(Color.lightGray); addMouseListener(this); }
public void paint(Graphics g) { g.setColor(Color.red);
g.drawString(\
int diameter = Math.min(this.getWidth()/2, this.getHeight()/2); g.setColor(Color.yellow);
g.fillOval(x,y,diameter,diameter); g.setColor(this.getBackground()); g.fillOval(x-20,y-20,diameter,diameter); }
public void mousePressed(MouseEvent e){ x=e.getX(); y=e.getY(); repaint(); }
public void mouseReleased(MouseEvent e){} public void mouseEntered(MouseEvent e){} public void mouseExited(MouseEvent e){} public void mouseClicked(MouseEvent e){ x=e.getX(); y=e.getY(); repaint(); }
public void update(Graphics g){ paint(g); super.update(g); } }
public class MoonJFrame{
public static void main(String args[]){ Frame f=new Frame();
f.setBounds(500,300,400,400); f.setVisible(true);
f.add(new Mycanvas(),\ f.validate(); } }
(实验二程序清单续4)
5.根据阿基米德螺线的极坐标方程:r=aθ画出相应图形。
import java.awt.*; import java.awt.event.*; import javax.swing.*;
public class ArchimedesJFrame extends JFrame implements ActionListener{ private ArchimedesCanvas archimedes; private JRadioButton radiob_color[]; public ArchimedesJFrame(){
super(\阿基米德螺线\);
Dimension dim=getToolkit().getScreenSize();
this.setBounds(dim.width/4,dim.height/4,dim.width/2,dim.height/2); this.setDefaultCloseOperation(EXIT_ON_CLOSE); JToolBar toolbar=new JToolBar(); this.getCntentPane().add(toolbar,\); String colorstr[]={\红\,\绿\,\蓝\};
ButtonGroup bgroup_color=new ButtonGroup(); radiob_color=new JRadioButton[colorstr.length]; for(int i=0;i radiob_color[i]=new JRadioButton(colorstr[i]); radiob_color[i].addActionListener(this); bgroup_color.add(radiob_color[i]); toolbar.add(radiob_color[i]); } radiob_color[0].setSelected(true); archimedes=new ArchimedesCanvas(Color.red); this.getContentPane().add(archimedes,\); this.setVisible(true); } public void actionPerformed(ActionEvent e){ Color c=null; if(e.getSource()==radiob_color[0]) c=new Color(255,0,0); if(e.getSource()==radiob_color[1]){ c=new Color(0,255,0); } if(e.getSource()==radiob_color[2]) c=new Color(0,0,255); archimedes.setColor(c); archimedes.repaint(); } public static void main(String[] args){ new ArchimedesJFrame(); } } class ArchimedesCanvas extends Canvas implements ComponentListener,FocusListener,ActionListener{ private Color color; private Timer mTimer; public ArchimedesCanvas(Color color){ this.setBackground(Color.WHITE); this.setColor(color); this.addComponentListener(this); this.addFocusListener(this); } public void setColor(Color color){ this.color = color; } public void paint(Graphics g){ int x0 = this.getWidth()/2; int y0 = this.getHeight()/2; g.setColor(color); g.drawLine(x0,0,x0,y0*2); g.drawLine(0,y0,x0*2,y0); for (int i=0; i<4000; i++){ double angle = i*Math.PI/512; double radius = 0.5*angle; int x=(int)Math.round(radius*angle*Math.cos(angle)); int y=(int)Math.round(radius*angle*Math.sin(angle)); mTimer=new Timer(1000,this); mTimer.start(); g.drawOval(x0+x,y0+y,1,1); } } public void componentMoved(ComponentEvent e) {} public void componentHidden(ComponentEvent e) {} public void componentResized(ComponentEvent e) { this.repaint(); } public void componentShown(ComponentEvent e) {} public void focusGained(FocusEvent arg0){ mTimer.stop(); } public void focusLost(FocusEvent arg0){ mTimer.restart(); } public void actionPerformed(FocusEvent arg0){} public void actionPerformed(ActionEvent e) {} } 四、实验结果与分析 (分析每题采用的布局管理器、事件处理类和主要功能实现方法。) 结果: (1)输出结果: (2)输出结果: (3)输出结果: 点击“平均分”按钮后: 正常显示结果如下: (4)输出结果: 点击鼠标后:

