我在移动一个矩形的时候,中间过程无法显示,请高手指教,谢谢。代码如下(在面板上拖动鼠标就会画矩形,鼠标滚轮调整大小,平移的时候看不见中间移动的过程,其他功能还没实现)import java.awt.*;import javax.swing.*;
import java.awt.event.*;
public class Test{
public static void main(String args[]){
TestFrame bp = new TestFrame();
bp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
bp.setVisible(true);
}
}
class TestFrame extends JFrame{
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
public TestFrame(){
setTitle("实验二");
setSize(d.width,d.height);
TestPanel1 can= new TestPanel1();
add(can);
}
}
class TestPanel1 extends JPanel implements MouseMotionListener,MouseWheelListener,ActionListener{
public int x,y,m,c;
public int width,height;
JButton pingyi=new JButton("平移");
JButton xuanzhuan=new JButton("旋转");
JButton duichen=new JButton("对称");
JButton cuoqie=new JButton("错切");
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();

    public TestPanel1(){
    
     pingyi.addActionListener(this);
     xuanzhuan.addActionListener(this);
     duichen.addActionListener(this);
     cuoqie.addActionListener(this);
     add(pingyi);
     add(xuanzhuan);
     add(duichen);
     add(cuoqie);
            
    validate();
addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
x=e.getX();
y=e.getY();
}
});
addMouseMotionListener(this);
addMouseWheelListener(this);
    }

public void mouseDragged(MouseEvent e){
m=e.getX();
width=m-x;
height=width;
repaint();
}
public void mouseMoved(MouseEvent e){}
public void mouseWheelMoved(MouseWheelEvent e){
if(e.getWheelRotation()>0){
x+=2;
y+=2;
width-=4;
height-=4;
}
if(e.getWheelRotation()<0){
x-=2;
y-=2;
width+=4;
height+=4;
}
repaint();
}

public void actionPerformed(ActionEvent event){
Object source= event.getSource();
if(source==pingyi){
pingyi();
}
if(source==xuanzhuan){

}
if(source==duichen){

}
if(source==cuoqie){

}
}
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.yellow);
g.fillRect(0, 0, this.getWidth(),this.getHeight());
g.setColor(Color.BLACK);
    g.drawRect(x,y,width,height);    
}

public void pingyi(){
while(x<600){
     x+=3;
    
     try
    {
 Thread.sleep(300);
    }
    catch(InterruptedException e)
    {
e.printStackTrace();
    }
    repaint();
}
}
}

解决方案 »

  1.   

    我给楼主提点建议,你画图的时候,把你画的图形放到一个集合里!
    然后在你重画的时候,把集合里的图形再重画一下就可以了!
    下面是我写的一个简单画椭圆的类子,中间过程可以显示!package shi.circle;import java.awt.*;
    import java.awt.geom.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.*;public class DrawCircleDemo extends JPanel
    {
    private Vector<Shape> vec=new Vector<Shape>();
    private Shape shape=null;
    private JPanel panel=null;
    private int x1,y1,x2,y2;
    private int start1,end1,start2,end2;

    public DrawCircleDemo()
    {
    MouseHandler handler=new MouseHandler();
    addMouseListener(handler);
    addMouseMotionListener(handler);
    setSize(300,300);
    setVisible(true);

    }
    public void paintComponent(Graphics g)
    {
    super.paintComponent(g);
    Graphics2D g2=(Graphics2D)g;
    for(int i=0;i<vec.size();i++)//把前面的图画出来
    {
    g2.draw(vec.get(i));
    }
    if(shape!=null)
    {
    g2.draw(shape);
    }


    }
    public void addShape(Shape shape)
    {
    vec.add(shape); }
    class MouseHandler extends MouseAdapter implements MouseMotionListener
    {

    public void mousePressed(MouseEvent e)
    {
    x1=e.getX();
    y1=e.getY();

    }

    public void mouseReleased(MouseEvent e)
    {
    addShape(shape);
    }

    public void mouseDragged(MouseEvent e)
    {
    x2=e.getX();
    y2=e.getY();
    validate();
    shape=new Ellipse2D.Double(start1,end1,start2-start1,end2-end1);
    repaint();
    }
    public void mouseMoved(MouseEvent e)//这个方法不用写了
    {

    }
    }

    public void validate()
    {
    if(x1<x2&&y1<y2)
    {
    start1=x1;
    end1=y1;
    start2=x2;
    end2=y2;
    }
    else if(x1<x2&&y1>y2)
    {
    start1=x1;
    end1=y2;
    start2=x2;
    end2=y1;
    }

    else if(x1>x2&&y1<y2)
    {
    start1=x2;
    end2=y1;
    start1=x1;
    end2=y2;
    }

    else if(x1>x2&&y1>y2)
    {
    start1=x2;
    end1=y2;
    start2=x1;
    end2=y1;
    }
    }
    public static void main(String args[])
    {
    DrawCircleDemo draw=new DrawCircleDemo();
    JFrame frame=new JFrame("java画椭圆");
    frame.add(draw);
    frame.setSize(400,300);
    frame.setLocation(300,300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    }

    }