Auf Thema antworten

Denn hier mal der ganze code ;)


[code=Java]

public class DragableTile extends Tile {


    // layer

    public int originalLayer;

   

    // point fo Jlabel (x|y)

    public Point offsetPoint = null;

   

    // rectangle to chck collision

    public Rectangle rectangle = null;

   

    public int imageWidth =  0;

    public int imageHeight = 0;

   

    // test Vars

    public static int startPoint = 0;

   

    // list of all available Dragable Tiles

    public static ArrayList<DragableTile> dragableTileList = new ArrayList<DragableTile>();


    public enum TouchLocation {

        RIGHT, LEFT, TOP, BOTTOM, NONE;

    }


    public DragableTile(int tileNumber,int tileValue, int tileColor){

        super(tileNumber,tileValue,tileColor);

       

        this.imageWidth     = this.getImage().getIconWidth();

        this.imageHeight    = this.getImage().getIconHeight();

       

        // every new component shoul'd get a new location

        DragableTile.startPoint += 100;

       

        // size and rectangle

        setBounds(0,0, 400,400);

        setLocation(0,0);

        rectangle = new Rectangle(this.getX(), this.getY(), this.imageWidth, this.imageHeight);

        setPreferredSize(new Dimension(this.imageWidth, this.imageHeight));

   

        // activate events (different way e.g. this.addMouseListener(this))

        enableEvents(MouseEvent.MOUSE_EVENT_MASK |

                     MouseEvent.MOUSE_MOTION_EVENT_MASK);

    }

   

    /**

     * Mouse Motion

     */

    protected void processMouseMotionEvent(MouseEvent e){

       

        // check offset != null - tile is moving

        if(this.offsetPoint != null){

           

            rectangle = new Rectangle(

                    this.getLocation().x,

                    this.getLocation().y,this.imageWidth, this.imageHeight);

            System.out.println(rectangle);

           

            // default 100,100

            this.setLocation (

                    getX () + (e.getX () - offsetPoint.x),

                    getY () + (e.getY () - offsetPoint.y));

           

            //this.setBounds((int)this.getLocation().getX(), (int)this.getLocation().getY(), this.imageWidth, this.imageHeight);

           

            revalidate();

            System.out.println("X- K : " + getX () + (e.getX () - offsetPoint.x));

            System.out.println("Y- K : " + getY () + (e.getY () - offsetPoint.y));

           

            System.out.println("X- Koordinate des Bildes : " + this.getLocation().getX());

            System.out.println("Y- Koordinate des Bildes : " + this.getLocation().getY());

           

        }

    }


    protected void processMouseEvent (MouseEvent e) {

   

        // click on Tile

        if(e.getID() == MouseEvent.MOUSE_PRESSED) {

           

            if(this.getParent() instanceof JLayeredPane) {

                // get original layer

                originalLayer = JLayeredPane.getLayer(this);


                // drag tile layer

                ((JLayeredPane) getParent ()).setLayer (

                        this, JLayeredPane.DRAG_LAYER.intValue ()

                );

               

                // determine offset - mouse point depends on pic

                offsetPoint = new Point(e.getPoint ().x,

                                        e.getPoint ().y);

            }

           

        // release tile

        }else if(e.getID () == MouseEvent.MOUSE_RELEASED) {


            if(getParent () instanceof JLayeredPane) {

            // back to originLayer

            ((JLayeredPane) getParent()).setLayer(this, originalLayer);


            // check collision with another Tile

            final TouchLocation touchLoacation = checkCollision();


                switch(touchLoacation){

                    case NONE :

                        break;

                    case TOP : //DragAndDropPanelsDemo.showOPtionDialog();

                        break;

                    case BOTTOM:

                        break;

                    case RIGHT:

                        break;

                    case LEFT:

                        break;

                }

               

            // no dragging anymore

            offsetPoint = null;

            }

        }

    }

   

    public void paintComponent(Graphics g) {

        super.paintComponent(g);

        Graphics2D g2 = (Graphics2D) g;

        // zeichnen

        g2.drawString("x-Kooridnate: " + this.rectangle.x,10,10);

        g2.drawString("y-Kooridnate: " + this.rectangle.y,10,25);

        g2.drawRect(this.rectangle.x,this.rectangle.y, this.rectangle.width, this.rectangle.height);

}

}

[/code]



Hier wo ich das teste:


[code=Java]

public class DragAndDropPanelsDemo extends JFrame {

    static DragableTile e = null;

    static DragableTile f = null;

    static DragableTile g  = null;

    Bootstrap b = new Bootstrap();

    static JLayeredPane layeredPane;

   

   

    static ArrayList<DragableLabel> list = new ArrayList<DragableLabel>();

   

    ImageManager a = ImageManager.getInstance();

 public DragAndDropPanelsDemo () {


   JPanel zweitesPanel = new JPanel ();

   zweitesPanel.setLayout(null);


  

  

  

  

  

  

  

  

  

  

   layeredPane = new JLayeredPane();

   layeredPane.setLayout(null);

   //layeredPane.setPreferredSize(new Dimension(300,300));

// public DragableLabel(String name, Color farbe)


   e = new DragableTile(1, 1, 0);

   layeredPane.add(e,JLayeredPane.DEFAULT_LAYER);

  

   //e.setBounds(0,0,e.imageWidth,e.imageHeight);

  

  

   layeredPane.setBounds(0,0,500,500);

  

  

  

  

   f = new DragableTile(1, 1, 0);

  

  

  

   layeredPane.add(f,

           new Integer (

             JLayeredPane.DEFAULT_LAYER.intValue()

             +1));

  

   //f.setBounds(0,0,e.imageWidth,e.imageHeight);

  

  

  

   g = new DragableTile(1, 1, 0);

  

   layeredPane.add(g,

           new Integer (

             JLayeredPane.DEFAULT_LAYER.intValue()

             +1));

  

  // g.setBounds(0,0,e.imageWidth,e.imageHeight);

  

   DragableTile.dragableTileList.add(e);

   DragableTile.dragableTileList.add(f);

   DragableTile.dragableTileList.add(g);

   zweitesPanel.add(layeredPane);

 

//   g = new DragableLabel(a.getImageIcon("a.jpg"), Color.BLUE);

//   layeredPane.add(g,

//                new Integer (

//                  JLayeredPane.DEFAULT_LAYER.intValue()

//                  +2));



//    System.out.println(e.rectangle.toString());

//    System.out.println(f.rectangle.toString());

//    //System.out.println(g.rectangle.toString());




   getContentPane().add(zweitesPanel);

   setVisible(true);

 }


 public static void main (String[] argv) throws Exception {

      System.out.println("Starting Drag And Drop Example ...");

      DragAndDropPanelsDemo mainFrame = new DragAndDropPanelsDemo();

      mainFrame.setSize(400, 400);

      mainFrame.setLocation(100,100);

      mainFrame.setTitle("Drag And Drop Example");

      mainFrame.setVisible(true);

 }

 

[/code]


Es ist kein KSKB... nur ein Ausschnitt, kann gerne aber ein besipiel anhängen.


Aber hoffe das ihr mein fehler seht.

Habe viel ausprobiert ...


danke vielmals



Oben