该楼层疑似违规已被系统折叠隐藏此楼查看此楼
利用java画出椭圆。也就是鼠标一边移动一边显示出椭圆,如何做到请大神指教
这是我写的(没有达到我自己的要求):
import java.awt.*;
import java.awt.Graphics;
import java.awt.event.*;
import javax.swing.*;
import ponent;
public class Draw2 implements MouseMotionListener,MouseListener{
JFrame f;
int x1,x2,y1,y2;
mypane pane;
public static void main(String args[]){
Draw2 d=new Draw2();
d.go();
}
public void go(){
f=new JFrame("绘图");
pane=new mypane();
pane.addMouseListener(this);
pane.addMouseMotionListener(this);
f.getContentPane().add(pane,"Center");
f.setSize(300,300);
f.setVisible(true);
}
public void mousePressed(MouseEvent e){
pane.repaint();
x1=e.getX();
y1=e.getY();
}
public void mouseDragged(MouseEvent e){
x2=e.getX()-x1;
y2=e.getY()-y1;
}
public void mouseMoved(MouseEvent e){
}
public void mouseReleased(MouseEvent e){
x2=e.getX()-x1;
y2=e.getY()-y1;
}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
class mypane extends JPanel{
protected void paintComponent(Graphics g){
super.paintComponent(g);
g.drawOval(x1, y1, x2, y2);
}
}
}
该如何改???