# 在java中实现图形学中的椭圆算法

有同学向我要在java中实现图形学中的椭圆算法，画直线的算法的程序，在此就献丑了，把上大学时用java写的源代码给先学者做个参考，下面是实现椭圆的算法

/*author computer np
*test date
*(300,100)(400,100)
*
*/
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class Lipse
{
public static void main(String[] args)
{
new MainFrame();
}
}
class MainFrame extends JFrame implements ActionListener
{
JPanel pane=new JPanel();
JTextField T_a,T_b;
JButton Draw,Show;
JLabel L_a,L_b;
int a,b;
MainFrame()
{
super("DrawLipse Window");
Container con=this.getContentPane();
con.setLayout(null);

pane.setBounds(20,20,850,550);
pane.setBackground(new Color(100,156,200));

L_a=new JLabel("请输入长半径:a");
L_a.setBounds(180,580,100,20);

L_b=new JLabel("请输入短半径:b");
L_b.setBounds(180,630,100,20);

T_a=new JTextField();
T_a.setBounds(300,580,50,20);

T_b=new JTextField();
T_b.setBounds(300,630,50,20);

Draw=new JButton("画椭圆");
Draw.setBounds(550,580,90,30);

Show=new JButton("显示坐标");
Show.setBounds(550,620,90,30);

this.setBounds(20,20,900,700);
this.setVisible(true);
this.setResizable(false);

}/*MainFrame()*/
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==Draw)
{
a=Integer.parseInt(T_a.getText().trim());
b=Integer.parseInt(T_b.getText().trim());
Line line=new Line(this);
line.drawLipse(a,b);
}
if(e.getSource()==Show)
{
Graphics g1=this.pane.getGraphics();
g1.setColor(Color.PINK);
g1.drawLine(0,300,920,300);//----x---
g1.drawLine(410,0,410,720);//----y---
g1.dispose();
}

}/*method actionPerformed*/
}
class Line
{
MainFrame jb;
Line(MainFrame jb)
{
this.jb=jb;
}
public void drawLipse(int a,int b)
{
int x,y;
double d1,d2;
x=0;  y=b;
d1=b*b+a*a*(-b+0.25);
Graphics g=jb.pane.getGraphics();
g.setColor(Color.red);
g.drawLine(x+410,y+300,x+410,y+300);
g.drawLine(-x+410,-y+300,-x+410,-y+300);
g.drawLine(-x+410,y+300,x+410,-y+300);
g.drawLine(x+410,-y+300,x+410,-y+300);
try
{
while(b*b*(x+1)<a*a*(y-0.5))
{
if(d1<=0)
{
d1+=b*b*(2*x+3);
x++;
}
else
{
d1+=(b*b*(2*x+3)+a*a*(-2*y+2));
x++;
y--;
}
g.drawLine(x+410,y+300,x+410,y+300);
g.drawLine(-x+410,-y+300,-x+410,-y+300);
g.drawLine(-x+410,y+300,x+410,-y+300);
g.drawLine(x+410,-y+300,x+410,-y+300);
}// top of while
}catch(Exception e){}

d2=b*b*(x+0.5)*(x+0.5)+a*a*(y-1)*(y-1)-a*a*b*b;
try
{
while(y>0)
{
if(d2<=0)
{
d2+=b*b*(2*x+2)+a*a*(-2*y+3);
x++;
y--;
}
else
{
d2+=a*a*(-2*y+3);
y--;
}
g.drawLine(x+410,y+300,x+410,y+300);
g.drawLine(-x+410,-y+300,-x+410,-y+300);
g.drawLine(-x+410,y+300,x+410,-y+300);
g.drawLine(x+410,-y+300,x+410,-y+300);
}/* bottom of while*/

}catch(Exception e){}

} /*DrawLipse*/

}
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120