为什么我的照片不动?

时间:2017-05-28 11:51:53

标签: java swing animation jframe 2d

我想用我用鼠标点击的方向移动图片,但它只在循环结束时重新绘制。我在这里尝试了一些建议,但由于我是初学者,这对我来说非常困难。

这是我的代码:

package projekt;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class Bewegung3 extends JFrame implements Runnable{

private int framebreite = 900, framehöhe = 600;
private int c = 50;
private int d = 50;
private Image bild = new ImageIcon("punkt.png").getImage().getScaledInstance(c, d, Image.SCALE_SMOOTH);
private Thread thread;
private int a = 394;
private int b = 285;
private int zaehler = 0;
private int[][] koordinaten = new int[2][2];




public Bewegung3(){

    setBounds(500, 300, framebreite, framehöhe);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);


    getContentPane().addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            int mausx = e.getX();
            int mausy = e.getY();
            switch(zaehler){
            case 0:
                koordinaten[0][0] = a;
                koordinaten[0][1] = b;
                koordinaten[1][0] = mausx;
                koordinaten[1][1] = mausy;
                zaehler++; break;

            case 1:
                koordinaten[0][0] = koordinaten[1][0];
                koordinaten[0][1] = koordinaten[1][1];
                koordinaten[1][0] = mausx;
                koordinaten[1][1] = mausy;
                break;
            }


            run();


        }

        });



    thread = new Thread(this);
    thread.start();
}


public void paint(Graphics g){
    g.setColor(Color.BLACK);
    g.fillRect(0, 0, framebreite, framehöhe); 


    g.drawImage(bild,a,b,null);
}





public static void main(String[] args){
    new Bewegung3();

}





public void run(){


    System.out.println();
    System.out.println(koordinaten[0][0] + " "+ koordinaten[0][1]);
    System.out.println(koordinaten[1][0] + " "+ koordinaten[1][1]);
    System.out.println();


    //x right
    if(koordinaten[1][0] > koordinaten[0][0]){



        for(int i = 0;i<=koordinaten[1][0] - koordinaten[0][0];i++){    
            a=(koordinaten[0][0]+i) - c/2;

            System.out.println("x+ " + (koordinaten[0][0]+i));
            repaint();

            try{
                thread.sleep(10);

            }catch(InterruptedException e){
                System.out.println(e);
            }
        }
    }



    //x left
    if(koordinaten[0][0] > koordinaten[1][0]){
        for(int i = 0;i <= koordinaten[0][0] - koordinaten[1][0] ;i++){ 
            a=(koordinaten[0][0]-i) - c/2;

            System.out.println("x- " + (koordinaten[0][0]-i));  
            repaint();

            try{
                thread.sleep(10);

            }catch(InterruptedException e){
                System.out.println(e);
            }
        }

    }





    //y down
    if(koordinaten[1][1] > koordinaten[0][1]){

        for(int i = 0;i<=koordinaten[1][1] - koordinaten[0][1];i++){
            b=(koordinaten[0][1]+i)  - d/2;

        System.out.println("y- " + (koordinaten[0][1]+i));  
        repaint();

        try{
            thread.sleep(10);

        }catch(InterruptedException e){
            System.out.println(e);
        }
        }

    }
    //y up
    if(koordinaten[0][1] > koordinaten[1][1]){
        for(int i = 0;i <= koordinaten[0][1] - koordinaten[1][1] ;i++){
            b=(koordinaten[0][1]-i) - d/2 ;

            System.out.println("y+ " + (koordinaten[0][1]-i));      
            repaint();

            try{
                thread.sleep(10);

            }catch(InterruptedException e){
                System.out.println(e);
            }
        }
    }


    }

}

}

0 个答案:

没有答案