2011年3月26日
<Java> JVA309-多彩圖形繪板
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.geom.*;
public class JVA309 extends JFrame implements ActionListener{
String figure="";
Shape shape;
ButtonGroup bg;
JRadioButton rbn1,rbn2,rbn3;
double x,y,r;
JPanel p=new JPanel();
public static void main(String args[]){
JVA309 pm=new JVA309();
}
public JVA309(){
super("多彩圖形繪板");
setBackground(Color.yellow);
Container c=getContentPane();
rbn1=new JRadioButton("圓形");
rbn2=new JRadioButton("扇形");
rbn3=new JRadioButton("方形");
rbn1.setOpaque(false);
rbn2.setOpaque(false);
rbn3.setOpaque(false);
bg=new ButtonGroup();
bg.add(rbn1);
bg.add(rbn2);
bg.add(rbn3);
rbn1.addActionListener(this);
rbn2.addActionListener(this);
rbn3.addActionListener(this);
//請在此撰寫JPanel設定
p.setLayout(new FlowLayout(FlowLayout.CENTER));
p.setBackground(Color.yellow);
//p.add(new JLabel("選定圖形後在空白處拖曳滑鼠:"));
p.add(rbn1);
p.add(rbn2);
p.add(rbn3);
c.add(p,"North");
addMouseMotionListener(new MouseMotionAdapter(){
//請在此撰寫滑鼠拖曳程式
public void mouseDragged(MouseEvent me){
x = me.getX();
y = me.getY();
repaint();
}
});
setSize(500,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void actionPerformed(ActionEvent ae){
String cm=ae.getActionCommand();
//請在此撰寫圖形判斷程式碼
figure = (String)cm;
//請在此撰寫圖形判斷程式碼
}
public void update(Graphics g){
setBackground(Color.yellow);
paint(g);
}
public void paint(Graphics g){
Graphics2D g2d=(Graphics2D) g;
//請在此撰寫圖形相關程式碼
r = Math.random()*50.0;
if(figure.equals("圓形")){
shape = new Ellipse2D.Double(x,y,r,r);
}else if(figure.equals("扇形")){
shape = new Arc2D.Double(x,y,r,r,30D,120D,2);
}else if(figure.equals("方形")){
shape = new Rectangle2D.Double(x,y,r,r);
}else{
shape = new Ellipse2D.Double();
}
//請在此撰寫程式碼
g2d.setColor(new Color( (int)(Math.random()*255),
(int)(Math.random()*255),
(int)(Math.random()*255)));
g2d.fill(shape); //請下面撰寫程式
rbn1.repaint();
rbn2.repaint();
rbn3.repaint();
}
}
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言