第一次java实验:"*号画圆"

类别:Java 点击:0 评论:0 推荐:

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
class MyCanvas extends Canvas

 int x,y,r,n;
 int x0,y0;
 MyCanvas()
 {
  setSize(600,600);
  setBackground(Color.red);
 }
 public void setX(int x)
 {
  this.x=x;
 }
 public void setY(int y)
 {
  this.y=y;
 }
 public void setR(int r)
 {
  this.r=r;
 }
 public void setN(int n)
 {
  this.n=n;
 }
 public void paint(Graphics g1)
 {
   for(int i=0;i<=360;i=i+360/n)
   {
   x0 = (int)(x+r*Math.cos(i));
   y0 = (int)(y+r*Math.sin(i));
   g1.drawString("*",x0,y0);}
 }
}
public class e1 extends Applet implements ActionListener
{
 MyCanvas canvas;
 TextField inputR,inputX,inputY,inputN;
 Label label1,label2,label3;
 Button b1,b2;
 public void init()
 { 
  canvas = new MyCanvas();
  inputR = new TextField(6);
  inputX = new TextField(6);
  inputY = new TextField(6);
  inputN = new TextField(6);
  b1 = new Button("确定");
  b1.addActionListener(this);
  label1 = new Label("输入位置坐标:");
  label2 = new Label("输入半径:");
  label3 = new Label("输入要打印的*数:");
  add(label1);
  add(inputX);
  add(inputY);
  add(label2);
  add(inputR);
  add(label3);
  add(inputN);
  add(b1);
  add(canvas);
 }
 public void actionPerformed(ActionEvent e)
 {
   int x=0,y=0,n=0,r=0;
   try
   {
    x=Integer.valueOf(inputX.getText()).intValue();
    y=Integer.valueOf(inputY.getText()).intValue();
    n=Integer.valueOf(inputN.getText()).intValue();
    r=Integer.valueOf(inputR.getText()).intValue();
    canvas.setX(x);
    canvas.setY(y);
    canvas.setR(r);
    canvas.setN(n);
    canvas.repaint();
   }
   catch(NumberFormatException ee)
   {
    x = 0;
    y = 0;
    r = 0;
    n = 0;
   }
  
 }
}

本文地址:http://com.8s8s.com/it/it10094.htm