这是我写的一个画圈的程序。但不知道为什么。只能在右边画。
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.MouseMotionListener;
public class HuaTu extends JFrame implements ActionListener,MouseMotionListener,MouseListener{
JButton jb1,jb2,jb3;
int x = 0,y = 0,m = 0,n =0;
// JPanel jpSouth,jpCenter;
String shape = "";
public HuaTu(){
ImageIcon Save=new ImageIcon("New.gif");
// JButton button=new JBuTTon(zap);
jb1=new JButton("正方形");
jb1.setIcon(new ImageIcon("D://hand.jpg"));
jb2=new JButton("圆");
jb2.setIcon(new ImageIcon("D://hand.jpg"));
jb3=new JButton("画笔");
jb3.setIcon(new ImageIcon("D://hand.jpg"));
this.setBounds(0,0,800,600);
setResizable(false);
setLayout(null);
jb1.setBounds(100,500,160,60);
jb1.setSize(100,30);
add(jb1);
jb2.setBounds(300,500,160,60);
jb2.setSize(100,30);
add(jb2);
jb3.setBounds(500,500,160,60);
jb3.setSize(100,30);
add(jb3);
addMouseMotionListener(this);
addMouseListener(this);
jb1.addActionListener(this);
jb2.addActionListener(this);
jb3.addActionListener(this);
addMouseMotionListener(this);
addMouseListener(this);
addMouseMotionListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
/*public void update(Graphics g){
paint(g);
}*/
public void paint(Graphics g) {
if(shape.equals("正方形"))
g.drawRect(x,y,m-x,n-y);
if(shape.equals("圆"))
g.drawOval(x,y,m-x,n-y);
if(shape.equals("画笔"))
g.drawLine(x,y,x,y);
}
public static void main(String args[]){
new HuaTu();}
public void actionPerformed(ActionEvent e) {
// if(e.getSource() == jb1){}
shape = e.getActionCommand();
/*if(e.getSource() == jb2){
shape = e.getActionCommand();}
else if(e.getSource() == jb3){
shape = e.getActionCommand();}*/
//repaint();
}
public void mouseDragged(MouseEvent e) {
if(shape.equals("画笔")){
x=(int)e.getX();
y=(int)e.getY();
repaint();
}
}
public void mousePressed(MouseEvent e) {
x=m;
y=n;
x=e.getX();
y=e.getY();
}
public void mouseReleased(MouseEvent e) //获取所绘图形的终点
{ m=e.getX();
x=n=e.getY();
repaint();
}
public void mouseClicked(MouseEvent e){ }
public void mouseEntered(MouseEvent e){ }
public void mouseExited(MouseEvent e){ }
public void mouseMoved(MouseEvent e) {}
}
|
|