1. 程式人生 > >繪製指定屬性圖形(以圓為例)2.0

繪製指定屬性圖形(以圓為例)2.0

package javaPractice;

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

public class Point {
	public static void main(String[] args) {
		new MyFrame();
	}
}

class MyFrame extends JFrame{
	MyJPanel mj = new MyJPanel();
	JLabel j1,j2,j3;
	JTextArea txt1,txt2,txt3;
	JTextField jt1,jt2,jt3;
	JButton btn;
	JPanel jp2;


	MyFrame(){
		this.setSize(500,400);
		this.setTitle("drawcircle");
		this.setLayout(null);
			
		j1=new JLabel("座標x");
		j2=new JLabel("座標y");
		j3=new JLabel("半徑r");
		btn=new JButton("draw");
		jp2=new JPanel();
		jt1=new JTextField();
		jt2=new JTextField();
		jt3=new JTextField();
				
		jt1.setPreferredSize(new Dimension(15,10));
		jt2.setPreferredSize(new Dimension(15,10));
		jt3.setPreferredSize(new Dimension(15,10));
		
		j1.setBounds(30, 330, 35, 20);	
		j2.setBounds(130, 330, 35, 20);
		j3.setBounds(220, 330, 35, 20);
		jt1.setBounds(70, 330, 50, 20);		
		jt2.setBounds(160, 330, 50, 20);
		jt3.setBounds(245, 330, 50, 20);	
		btn.setBounds(350, 330, 100, 20);

		
		this.add(btn);
		this.add(j1);
		this.add(j2);
		this.add(j3);
		this.add(jt1);
		this.add(jt2);
		this.add(jt3);
				
		this.setLocationRelativeTo(null);		
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);		
		this.setVisible(true);							
		   
		btn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				int a,b,c;
				a=Integer.parseInt(jt1.getText());
				b=Integer.parseInt(jt2.getText());
				c=Integer.parseInt(jt3.getText());
				mj.setX(a);
				mj.setY(b);
				mj.setR(c);
				mj.paint(getGraphics());
			}
		});

		this.setVisible(true);
	}
}

class Monitor extends MouseAdapter{
	public void mousePressed(MouseEvent e) {
		MyFrame mf = (MyFrame)e.getSource();
		mf.repaint();
	}
}

class MyJPanel extends JPanel{
	private int x;
	private int y;
	private int r;
	
	public void setX(int x) {
		this.x = x;
	}
	
	public void setY(int y) {
		this.y = y;
	}
	
	public void setR(int r) {
		this.r = r;
	}
	
	public void paint(Graphics g){
			super.paint(g);
			g.setColor(Color.pink);		
			g.fillOval(x, y, r, r);
	}	
}