"金刚石"作图程序 站点:爱心种子小博士 关键字:"金刚石"作图程序
|
"金刚石"作图程序 程序运行图:
//金刚石作图程序。
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class ChenWin2 extends JApplet implements ActionListener
{
int x0=200;
int y0=150;
int r=80;
int n=15;
double t=6.28318/n;
int x[]=new int[50];
int y[]=new int[50];
JPanel p2=new JPanel();
JButton bu=new JButton("好!");
JButton ex=new JButton("结束");
MyCanvas ca=new MyCanvas();
MyPanel pa=new MyPanel(5,25,15,5,1,50,100,80,5,1);
public void init(){
Container con=getContentPane();
bu.addActionListener(this);
ex.addActionListener(this);
p2.add(bu);
p2.add(ex);
con.add("North",pa);
con.add("Center",ca);
con.add("South",p2);
}
class MyCanvas extends Canvas{
public void paint(Graphics g)
{
something s;
s=new something(105);
g.drawString("This is ChenWin2!! Value"+s,20,50);
for(int i=0;i<n;i++)
{
x[i]=(int)(r*Math.cos(i*t)+x0);
y[i]=(int)(r*Math.sin(i*t)+y0);
}
g.setColor(Color.red);
for(int i=0;i<=n-2;i++)
{
for(int j=i+1;j<=n-1;j++)
g.drawLine(x[i],y[i],x[j],y[j]);
}
}
}
public void actionPerformed(ActionEvent evt)
{
JButton jb=(JButton)evt.getSource();
if(jb==bu){
n=pa.v1;
r=pa.v2;
t=6.28318/n;
for(int i=0;i<n;i++){
x[i]=(int)(r*Math.cos(i*t)+x0);
y[i]=(int)(r*Math.sin(i*t)+y0);
}
ca.repaint();
}
if(jb==ex){
System.out.println("OK!!!OK!!!!");
System.exit(0);
}
}
public static void main(String args[])
{
Framec bb;
bb=new Framec(new ChenWin2());
bb.setTitle("This is ChenWin2");
}
}
class something
{
private int value;
public something(int value)
{
this.value=value;
}
public String toString()
{
String s;
s="<"+value+">";
return s;
}
}
class Framec extends JFrame
{
public Framec(JApplet applet)
{
setSize(600,450);
addWindowListener(new MywindowListener());
getContentPane().add("Center",applet);
applet.init();
show();
}
class MywindowListener extends WindowAdapter
{
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
}
class MyPanel extends JPanel implements ChangeListener{
JSlider sli1,sli2;
int v1,v2;
public MyPanel(int min,int max,int val,int mjr,int mnr,
int min1,int max1,int val1,int mjr1,int mnr1){
sli1=setSlider(min,max,val,mjr,mnr);
sli2=setSlider(min1,max1,val1,mjr1,mnr1);
v1=val;
v2=val1;
add(sli1);
add(sli2);
}
public JSlider setSlider(int min,int max,int val,int mjr,int mnr){
JSlider sli=new JSlider(JSlider.HORIZONTAL,min,max,val);
sli.setPaintTicks(true);
sli.setMajorTickSpacing(mjr);
sli.setMinorTickSpacing(mnr);
sli.setPaintLabels(true);
sli.addChangeListener(this);
return sli;
}
public void stateChanged(ChangeEvent e){
JSlider sli=(JSlider)e.getSource();
if(sli==sli1){
v1=sli1.getValue();
}
else if(sli.equals(sli2)){
v2=sli2.getValue();
}
}
}
|
|
|
|