Auf Thema antworten

Moin,

so nachdem ich verzweifelt danach gesucht und nichts brauchbares gefunden habe, brauche ich porfessionelle Hilfe :D


Es geht darum, ein (.gif) Bild, das in einem BufferedImage Array ist, zu verschieben. Das Bild wurde aber zuvor auf das Panel gezeichnet. Ist es also möglich, mithilfe eines mouse Events, das Bild zu verschieben, das gerade angeklickt/anwisiert wurde?

So schaut das aus. Dass das Spielfeld voller Autos ist, tut nichts zur Sache denn das diente dem Bugfixing.

Code derzeit:

[code=Java]public rushPanel() {

        initComponents();

        this.setPreferredSize(new Dimension(w, h));


        try {

            img2 = ImageIO.read(new File("src/pics/absperrung.png"));

            main_car = ImageIO.read(new File("src/pics/main.gif"));

            for (int i = 0; i < car.length; i++) {

                car[i] = ImageIO.read(new File("src/pics/car" + i + ".gif"));

                car_rot[i] = ImageIO.read(new File("src/pics_rot/car_rot" + i + ".gif"));

            }


            for (int i = 0; i < truck.length; i++) {

                truck[i] = ImageIO.read(new File("src/pics/truck" + i + ".gif"));

                truck_rot[i] = ImageIO.read(new File("src/pics_rot/truck_rot" + i + ".gif"));

            }

        } catch (Exception ex) {

            Logger.getLogger(rushPanel.class.getName()).log(Level.SEVERE, null, ex);

        }


    }


    @Override

    public void paintComponent(Graphics g) {


        super.paintComponent(g);

        Graphics2D g2 = (Graphics2D) g;


        final int GAME_BORDER = (int) Math.sqrt(Attributes.UMRANDUNG.length);


        for (int y = 0; y < GAME_BORDER; y++) {                            

            for (int x = 0; x < GAME_BORDER; x++) {                        


                if (Attributes.UMRANDUNG[y * GAME_BORDER + x] != 1) {

                    g2.drawRect((64 * x), (64 * y), 64, 64);

                    System.out.println("Stelle y: " + y + "\nStelle x: " + x + "\nGameboarder[i]" + Attributes.UMRANDUNG[y * GAME_BORDER + x] + "\n" + "Zahl in Klammer: " + y * GAME_BORDER + x + "\n____________");


                } else if (Attributes.UMRANDUNG[y * GAME_BORDER + x] == 1) {

                    g2.drawImage(img2, (64 * x), (64 * y), this);

                    System.out.println("FELD| " + "Stelle y: " + y + "\nStelle x: " + x + "\nGameboarder[i]" + Attributes.UMRANDUNG[y * GAME_BORDER + x] + "\n" + "Zahl in Klammer: " + y * GAME_BORDER + x + "\n____________");

                }

            }

        }


        for (int x = 1; x < Attributes.LAYER_1.length; x++) {                            

            for (int y = 1; y < Attributes.LAYER_1.length; y++) {

                Random r = new Random();



                // fügt nur Autos ein (2 Stellig)


                if (Attributes.LAYER_1[x][y] == 4) {                                   // maincar

                    g2.drawImage(main_car, 64 * y, 64 * x, this);

                    break;


                }


                if (Attributes.LAYER_1[x][y] == 2) {

                    if (Attributes.LAYER_1[x][y] == Attributes.LAYER_1[x][y + 1]) {     // zuerst alle spalten in zeile eins dann

                                                                                        // alle spalten in zeile zwei usw ...

                        g2.drawImage(car[r.nextInt(10)], 64 * y, 64 * x, this);

                        y = y + 1;

                    }

                }


                // trucks

                if (Attributes.LAYER_1[x][y] == 3) {

                    if (Attributes.LAYER_1[x][y] == Attributes.LAYER_1[x][y + 1] && Attributes.LAYER_1[x][y + 1] == Attributes.LAYER_1[x][y + 2]) {

                        g2.drawImage(truck[r.nextInt(4)], 64 * y, 64 * x, this);

                        y = y + 2;

                    }


                }

                //                   g2.drawImage(rotateImage(img2, 180.0), null, 20, 20);


            }

        }


        Random r = new Random();

        for (int x = 1; x < Attributes.LAYER_1.length; x++) {

            for (int y = 1; y < Attributes.LAYER_1.length; y++) {


                if (Attributes.LAYER_1[y][x] == 5) {

                    if (Attributes.LAYER_1[y][x] == Attributes.LAYER_1[y + 1][x]) {     // [y][x] zuerst alle zeilen in spalte eins dann

                                                                                        // alle zeilen in spalte zwei usw...

                        g2.drawImage(car_rot[r.nextInt(10)], 64 * x, 64 * y, this);

                        y = y + 1;

                    }

                }

                if (Attributes.LAYER_1[y][x] == 6) {

                    if (Attributes.LAYER_1[y][x] == Attributes.LAYER_1[y + 1][x] && Attributes.LAYER_1[y + 1][x] == Attributes.LAYER_1[y + 2][x]) {

                        g2.drawImage(truck_rot[r.nextInt(4)], 64 * x, 64 * y, this);

                        y = y + 2;

                    }

                }


            }

        }

    }[/code]


Auszug aus den Attributes:

[code=Java]public class Attributes {



    // 2 ... Car

    // 3 ... Truck

    // 4 ... Maincar

    // 5 ... rot_Cars

    // 6 ... rot_Trucks


    public static final int[][] LAYER_1 = {

        {1, 1, 1, 1, 1, 1, 1, 1},

        {1, 2, 2, 2, 2, 6, 6, 1},

        {1, 0, 0, 6, 5, 6, 6, 1},

        {1, 4, 4, 6, 5, 6, 6, 1},

        {1, 2, 2, 6, 5, 5, 6, 1},

        {1, 3, 3, 3, 5, 5, 6, 1},

        {1, 3, 3, 3, 0, 5, 6, 1},

        {1, 1, 1, 1, 1, 1, 1, 1},

    };



    public static final int[] UMRANDUNG = {

        1, 1, 1, 1, 1, 1, 1, 1,

        1, 0, 0, 0, 0, 0, 0, 1,

        1, 0, 0, 0, 0, 0, 0, 1,

        1, 0, 0, 0, 0, 0, 0, 0,

        1, 0, 0, 0, 0, 0, 0, 1,

        1, 0, 0, 0, 0, 0, 0, 1,

        1, 0, 0, 0, 0, 0, 0, 1,

        1, 1, 1, 1, 1, 1, 1, 1,};

}[/code]



Oben