📄 drawcircle.java
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class drawcircle extends Frame implements WindowListener,ActionListener,MouseListener
{
MenuBar mb;
Menu me;
static Color col=Color.black,cc=Color.red;
Choice c1,c2,c3;
Label prompt1,prompt2,prompt3,lab;
String input;
static int x0=0,x1=0,y0=0,y1=0,width,height,p=11,q=0,x_0,x_1,y_0,y_1,x_2,y_2,a;
int linestyle=0xffff,x00=0,x01=0,y00=0,y01=0;
int point=1,flag=0,r=0;
Dialog dialog;
Dimension D1;
Toolkit TK;
boolean isXOR=false;//用于确定写模式,true为异或模式,false为写模式
static Graphics g;
Image buffer;
int first=0;
TextField txt;
Button b;
public void display()
{ //获取屏幕的宽度和高度
TK=Toolkit.getDefaultToolkit();
D1=TK.getScreenSize();
//设置标题栏和窗口的大小
setTitle("等距参数法画圆");
setSize(D1.width,D1.height);
width=D1.width;
height=D1.height;
//为窗口添加事件监听器
addWindowListener(this);
addMouseListener(this);
//菜单
mb=new MenuBar();
setMenuBar(mb);
//mf=new Menu("文件");
me=new Menu("画圆");
me.add(new MenuItem("参数法画圆"));
me.addActionListener(this);
mb.add(me);
setVisible(true);
g=this.getGraphics();//画笔初始化
buffer=this.createImage(width,height);//创建缓冲区的大小
}//display
//实现接口ActionListener的所有方法,用于处理发生在菜单上的事件
public void actionPerformed(ActionEvent e)
{
isXOR=false;
if(e.getActionCommand()=="参数法画圆"){
p=11;
}
} //actionPerformed
//实现接口WindowListener的所有方法,用于处理发生在窗口上的事件
public void windowClosing(WindowEvent e)
{
System.exit(0);
}//windowClosing
public void windowOpened(WindowEvent e){ }
public void windowDeactivated(WindowEvent e){}
public void windowClosed(WindowEvent e){}
public void windowIconified(WindowEvent e){}
public void windowDeiconified(WindowEvent e){}
public void windowActivated(WindowEvent e){}
//实现接口MouseListener的所有方法(5个),用于处理鼠标事件
public void mousePressed(MouseEvent e)
{
x0=e.getX();
y0=e.getY();
x_0=x0;//起始点坐标
y_0=y0;
}//mousePressed
public void mouseReleased(MouseEvent e)
{
int m,n,r=0;
isXOR=false;
x1=e.getX();
y1=e.getY();
x_1=x1;//终止点坐标
y_1=y1;
x01=0;
y01=0;
x00=0;
y00=0;
flag=0;
if(p==11)//画圆
{
r=(int)(Math.sqrt(Math.abs(x1-x0)*Math.abs(x1-x0)+Math.abs(y1-y0)*Math.abs(y1-y0))+0.5);
//(new DrawLine()).DDALine(this.g,this.buffer,col,x0,y0,x1,y1,isXOR);
(new DrawLine()).circle(this.g,this.buffer,Color.red,x0,y0,r,isXOR);
}
}//mouseReleased
public void mouseClicked(MouseEvent e){ }
public void mouseEntered(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }
public void mouseMoved(MouseEvent e){ }
//用于显示
public void paint(Graphics g)
{
if(first==0) //初始化缓冲区
{
buffer=this.createImage(width,height);
first=1;
}
g.drawImage(buffer,0,0,this);
}
/****main*/
public static void main(String args[])
{
drawcircle f= new drawcircle();
f.display();
}
}
class DrawLine
{
public void DDALine( Graphics g,Image buffer,Color c,int linestyle,int xu0,int yu0,int xu1,int yu1,boolean isXOR) //DDA算法
{
float increx,increy,x,y,length;
int i;int n=0;
int mark=0x8000;
if(Math.abs(xu1-xu0)>Math.abs(yu1-yu0))
length=Math.abs(xu1-xu0);
else length=Math.abs(yu1-yu0);
increx=(xu1-xu0)/length;
increy=(yu1-yu0)/length;
x=xu0;
y=yu0;
/*在屏幕上画*/
if(isXOR) g.setXORMode(c);
else
{
g.setPaintMode();
g.setColor(c);
}
for(i=1;i<=length;i++)
{
n++;
if(n%16>1) mark=0x8000>>(n%16-1);
if(n%16==1) mark=0x8000;
if(n%16==0) mark=0x0001;
if((linestyle & mark)!=0)
if(length==Math.abs(xu1-xu0))
g.fillOval((int)Math.round(x+0.5),(int)Math.round(y+0.5),1,1);
else
g.fillOval((int)Math.round(x+0.5),(int)Math.round(y+0.5),1,1);
x=x+increx;
y=y+increy;
}
}
public void circle(Graphics g,Image buffer,Color c,int x0,int y0,int r,boolean isXOR){
int x,y,x_2,y_2;
float t=0;
g.setPaintMode();
g.setColor(c);
x=r;y=0;
g.fillOval(x0,y0,3,3);
g.fillOval(x+x0,y+y0,1,1);
while(t>=0&&t<=1){
x_2=x;
y_2=y;
t=t+1.00f/100;
x=(int)Math.round(r*(1-t*t)/(1+t*t)+0.5);
y=(int)Math.round(r*(2*t)/(1+t*t)+0.5);
(new DrawLine()).DDALine(g,buffer,c,0xffff,x_2+x0,y_2+y0,x+x0,y+y0,isXOR);
(new DrawLine()).DDALine(g,buffer,c,0xffff,-x_2+x0,y_2+y0,-x+x0,y+y0,isXOR);
(new DrawLine()).DDALine(g,buffer,c,0xffff,-x_2+x0,-y_2+y0,-x+x0,-y+y0,isXOR);
(new DrawLine()).DDALine(g,buffer,c,0xffff,x_2+x0,-y_2+y0,x+x0,-y+y0,isXOR);
}//while
}
//将数字字符串转换为数字
public long ToData(String s)
{
int k=s.length();
long data=s.charAt(0)-48;//charAt:返回指定位置的字符
for(int i=1;i<=k-1;i++)
{
data=10*data+s.charAt(i)-48;
}
return data;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -