站内搜索: 请输入搜索关键词

当前页面: 开发资料首页Java 专题一个绘图程序

一个绘图程序

摘要: 一个绘图程序

</td> </tr> <tr> <td width="532" height="35" valign="top" class="ArticleTeitle">

//一个作画程序。 运行图如下:


import java.awt.*;
import javax.swing.*;
import java.awt.event.*;


public class ChenWin29 extends JFrame implements ActionListener{
JPanel p;
CanvasPanel cp;
JLabel l1,l2;
JTextField t1,t2;
JButton ok;
int r=50, R=80;

public ChenWin29(){
cp=new CanvasPanel();
cp.setBorder(BorderFactory.createLoweredBevelBorder());
p=new JPanel();
l1=new JLabel("r");
l2=new JLabel("R");
t1=new JTextField("50",4);
t1.addActionListener(this);
t2=new JTextField("80",4);
t2.addActionListener(this);
ok=new JButton("OK");
ok.addActionListener(this);
p.add(l1);
p.add(t1);
p.add(l2);
p.add(t2);
p.add(ok);
p.setBorder(BorderFactory.createTitledBorder("Control"));
getContentPane().add("South",p);
getContentPane().add(cp,BorderLayout.CENTER);

}

class CanvasPanel extends JPanel{

public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.blue);

for(double a=0;a<=2*Math.PI;a+=Math.PI/18)
{
int x=(int)(r*Math.cos(a))+250;
int y=(int)(r*Math.sin(a))+150;
g.drawOval(x-R,y-R,2*R,2*R);
}
}

}

public void actionPerformed(ActionEvent e){
Object temp=e.getSource();
if(temp instanceof JButton){
if(temp==ok){
try{
r=Integer.valueOf(t1.getText()).intValue();
R=Integer.valueOf(t2.getText()).intValue();
System.out.println(r);
}catch(NumberFormatException ee){}
cp.repaint();
}
}
if(temp instanceof JTextField){
if(temp==t1){

try{
r=Integer.valueOf(t1.getText()).intValue();
R=Integer.valueOf(t2.getText()).intValue();
}catch(NumberFormatException ee){}
cp.repaint();
}

if(temp==t2){

try{
r=Integer.valueOf(t1.getText()).intValue();
R=Integer.valueOf(t2.getText()).intValue();
}catch(NumberFormatException ee){}
cp.repaint();
}
}
}
public static void main(String args[]){
ChenWin29 aa=new ChenWin29();
aa.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});

aa.setSize(500,400);
aa.show();
}
}

</td> <td width="176" valign="top" class="ArticleTeitle">
</td> </tr> <tr> <td height="25" colspan="2" valign="top" class="ArticleTeitle">


↑返回目录
前一篇: 按钮的光标设置
后一篇: 重定向标准流