當前位置:首頁 » 編程語言 » java橢圓

java橢圓

發布時間: 2023-09-24 05:59:32

『壹』 java 橢圓演算法

以下代碼,將輸出一個橢圓,再有問題,我可遠程助你。如下:

/**
*(300,100)(400,100)
*
*/
importjava.awt.*;
importjavax.swing.*;
importjava.awt.event.*;
publicclassLipse
{
publicstaticvoidmain(String[]args)
{
newMainFrame();
}
}

{
JPanelpane=newJPanel();
JTextFieldT_a,T_b;
JButtonDraw,Show;
JLabelL_a,L_b;
inta,b;
MainFrame()
{
super("DrawLipseWindow");
Containercon=this.getContentPane();
con.setLayout(null);

pane.setBounds(20,20,850,550);
pane.setBackground(newColor(100,156,200));
con.add(pane);

L_a=newJLabel("請輸入長半徑:a");
L_a.setBounds(180,580,100,20);
con.add(L_a);

L_b=newJLabel("請輸入短半徑:b");
L_b.setBounds(180,630,100,20);
con.add(L_b);


T_a=newJTextField();
T_a.setBounds(300,580,50,20);
con.add(T_a);

T_b=newJTextField();
T_b.setBounds(300,630,50,20);
con.add(T_b);

Draw=newJButton("畫橢圓");
Draw.setBounds(550,580,90,30);
Draw.addActionListener(this);
con.add(Draw);

Show=newJButton("顯示坐標");
Show.setBounds(550,620,90,30);
Show.addActionListener(this);
con.add(Show);

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

}/*MainFrame()*/
publicvoidactionPerformed(ActionEvente)
{
if(e.getSource()==Draw)
{
a=Integer.parseInt(T_a.getText().trim());
b=Integer.parseInt(T_b.getText().trim());
Lineline=newLine(this);
line.drawLipse(a,b);
}
if(e.getSource()==Show)
{
Graphicsg1=this.pane.getGraphics();
g1.setColor(Color.PINK);
g1.drawLine(0,300,920,300);//----x---
g1.drawLine(410,0,410,720);//----y---
g1.dispose();
}

}/*methodactionPerformed*/
}
classLine
{
MainFramejb;
Line(MainFramejb)
{
this.jb=jb;
}
publicvoiddrawLipse(inta,intb)
{
intx,y;
doubled1,d2;
x=0;y=b;
d1=b*b+a*a*(-b+0.25);
Graphicsg=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);
Thread.sleep(30);
}//topofwhile
}catch(Exceptione){}

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);
Thread.sleep(30);
}/*bottomofwhile*/

}catch(Exceptione){}

}/*DrawLipse*/

}

{
publicvoidwindowClosing(WindowEvente)
{
System.exit(0);
}
}
熱點內容
預編譯的作用是什麼 發布:2025-02-01 17:44:52 瀏覽:738
蘋果電腦打不開壓縮文件 發布:2025-02-01 17:39:47 瀏覽:967
無人機存儲櫃子 發布:2025-02-01 17:39:45 瀏覽:199
shell腳本編寫刪除修改 發布:2025-02-01 17:38:05 瀏覽:118
wifi連接伺服器678怎麼回事 發布:2025-02-01 17:29:10 瀏覽:261
自動編譯小說軟體 發布:2025-02-01 17:23:19 瀏覽:186
滑鼠側鍵編程 發布:2025-02-01 17:14:51 瀏覽:68
bs源碼 發布:2025-02-01 16:51:52 瀏覽:568
百度雲zip怎麼解壓 發布:2025-02-01 16:31:59 瀏覽:567
pmc密碼是什麼意思 發布:2025-02-01 16:28:49 瀏覽:784