1个回答
展开全部
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
class MyCanvas extends Canvas
{
int x,y,r,n;
int x0,y0;
MyCanvas()
{
setSize(100,100);
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;
}
}
}
public void draw(Graphics2D g) {
g.setColor(color);//设置颜色
g.setStroke(stroke);//宽度
int x, y, w, h;
if (startX > endX) {//以下的startx 、endx都是由鼠标拖 动事件得到
x = endX;
w = startX - endX;
} else {
x = startX;
w = endX - startX;
}
if (startY > endY) {
y = endY;
h = startY - endY;
} else {
y = startY;
h = endY - startY;
}
g.drawOval(x, y, w, h);
}
import java.applet.*;
import java.awt.event.*;
class MyCanvas extends Canvas
{
int x,y,r,n;
int x0,y0;
MyCanvas()
{
setSize(100,100);
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;
}
}
}
public void draw(Graphics2D g) {
g.setColor(color);//设置颜色
g.setStroke(stroke);//宽度
int x, y, w, h;
if (startX > endX) {//以下的startx 、endx都是由鼠标拖 动事件得到
x = endX;
w = startX - endX;
} else {
x = startX;
w = endX - startX;
}
if (startY > endY) {
y = endY;
h = startY - endY;
} else {
y = startY;
h = endY - startY;
}
g.drawOval(x, y, w, h);
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询