Moving image with Java mouse

Ok, I'm pretty new to Java and I'm having problems with the project that was assigned to me. I am having trouble wrapping a CTetrimino object over a JFrame Here is my CTetrimino class.

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;

public class CTetriMino {

public CTetriMino(int type, int x, int y, int w, int h, Color c) 
{
    CMino m = new CMino();
    d = m.getDiameter();
    Type=type;
    X=x;
    Y=y;
    Width=w;
    Height=h;
    FillColor=c;
}

public CTetriMino(CTetriMino src) { // copy constructor
    Type=src.Type;
    X=src.X;
    Y=src.Y;
    Width=src.Width;
    Height=src.Height;
    FillColor=src.FillColor;
}

public int getX() { return X; }
public void setX(int x) { X=x; }
public int getY() { return Y; }
public void setY(int y) { Y=y; }

public void draw(Graphics g) {
    g.setColor(FillColor);
    switch (Type) {
    case 0:
        //g.fillOval(X, Y, Width, Height);
        //break;
        g.fillOval(X, Y, d, d);
        g.fillOval(X+d,Y, d, d);
        g.fillOval(X+d,Y-d, d, d);
        g.fillOval(X+d+d,Y-d, d, d);
        break;
    case 1:
        g.fillRect(X, Y, Width, Height);
        //g.fillOval(X, Y, d, d);
        //g.fillOval(X+d,Y, d, d);
        //g.fillOval(X+d,Y+d, d, d);
        //g.fillOval(X+d+d,Y+d, d, d);
        break;
    case 2:
        g.fillOval(X, Y, d, d);
        g.fillOval(X+d,Y, d, d);
        g.fillOval(X,Y-d, d, d);
        g.fillOval(X+d,Y-d, d, d);
        break;
    case 3:
        g.fillOval(X, Y, d, d);
        g.fillOval(X,Y+d, d, d);
        g.fillOval(X,Y+d+d, d, d);
        g.fillOval(X,Y-d, d, d);
        break;
    case 4:
        g.fillOval(X, Y, d, d);
        g.fillOval(X,Y+d, d, d);
        g.fillOval(X,Y-d, d, d);
        g.fillOval(X+d,Y-d, d, d);
        break;
    case 5:
        g.fillOval(X, Y, d, d);
        g.fillOval(X,Y+d, d, d);
        g.fillOval(X,Y-d, d, d);
        g.fillOval(X-d,Y-d, d, d);
        break;
    case 6:
        g.fillOval(X, Y, d, d);
        g.fillOval(X+d,Y, d, d);
        g.fillOval(X,Y-d, d, d);
        g.fillOval(X-d,Y, d, d);
        break;
        }
    }

public boolean containPoint(int x, int y) {
        switch (Type) {
        case 0:
            {
                double a=Width/2.0;
                double b=Height/2.0;
                double xc=X+a;
                double yc=Y+b;
                System.out.println(a);
                System.out.println(b);
                System.out.println(xc);
                System.out.println(yc);
                System.out.println(((x-xc)*(x-xc)/(a*a)+(y-yc)*(y-yc)/(b*b)<=1.0));
                return ((x-xc)*(x-xc)/(a*a)+(y-yc)*(y-yc)/(b*b)<=1.0);

            }
        case 1:
            return (x>=X && y>=Y && x<X+Width && y<Y+Height);

        }

        return false;
    }





private int Type;   
private int X;
private int Y;
private int Width;
private int Height;
private Color FillColor;
private int d;
}

      

Here is my Panel class, which I add to the Jframe in the TestNewTetris class.

import java.awt.Color;
import java.awt.Image;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.*;

import java.util.*;



public class Panel extends JPanel 
implements MouseListener, MouseMotionListener
{


private ArrayList<CTetriMino> originals;  
private ArrayList<CTetriMino> duplicates;

private CTetriMino BlockToBeMoved;
private int m_nOffsetX; // difference between cursor and top-left corner
private int m_nOffsetY;

// double buffering
private Image backBuffer;
private Graphics gBackBuffer;

boolean isInitialized;

// init and register mouse event handler
public Panel()
{
    isInitialized=false;
    // handle mouse and mouse motion events
    this.addMouseListener(this);
    this.addMouseMotionListener(this);
}

// Set up the initial state after the panel is created
void init()
{
    // Initial state
    duplicates = new ArrayList<CTetriMino>();
    originals = new ArrayList<CTetriMino>();
    Color[] colors = {Color.red, Color.green, Color.blue, Color.magenta, Color.cyan, Color.yellow, Color.orange};
    int count=colors.length;
    int dx=10;
    int dy=30;
    int gap=20;
    int length=(getSize().height-2*dy-(count-1)*gap)/count;
    for (int i=0; i<count; i++) { 
        //originals.add(new CTetriMino((i<count/2)?0:1, dx, dy+i*(length+gap), length, length, colors[i]));
        System.out.println(length);
        originals.add(new CTetriMino(i, dx, dy, length, length, colors[i])); 
        dx = dx+110;

    }
    BlockToBeMoved=null; // no shape selected

    // create the back buffer
    backBuffer = createImage(getSize().width, getSize().height);
    gBackBuffer = backBuffer.getGraphics();
}

// State Presentation
public void paintComponent( Graphics g )
{
    // super.paintComponent( g ); // clears drawing area

    if (!isInitialized) {
        isInitialized=true;
        init();
    }
    // State Presentation, using double buffers
    // First, clear the back buffer
    gBackBuffer.setColor(Color.white);
    gBackBuffer.clearRect(0, 0, getSize().width, getSize().height);
    // draw the originals to back buffer
    for (int i=0; i<originals.size(); i++) {
        originals.get(i).draw(gBackBuffer);
    }
    // draw the duplicates to back buffer
    for (int i=0; i<duplicates.size(); i++) {
        duplicates.get(i).draw(gBackBuffer);
    }
    // copy from back buffer to front
    g.drawImage(backBuffer, 0, 0, null);
    g.fillRect(0, 100, 800, 4);
    g.fillRect(0, 400, 800, 4);
} // end method paintComponent

// MouseListener event handlers
public void mouseClicked( MouseEvent e )
{
    if (e.isMetaDown()) {   // right button
        for (int i=duplicates.size()-1; i>=0; i--) {
            if (duplicates.get(i).containPoint(e.getX(), e.getY())) {
                duplicates.remove(i);
                repaint();
                break;
            }
        }
    }
}

public void mousePressed( MouseEvent e )
{
    if (e.isMetaDown()) return; // ignore right button

    // First, check the originals, from top down (i.e. back to front)
    for (int i=duplicates.size()-1; i>=0; i--) {
        CTetriMino p=duplicates.get(i);
        if (p.containPoint(e.getX(), e.getY())) {
            duplicates.remove(i);
            duplicates.add(p);  // move to the end, i.e. the top
            BlockToBeMoved=p;
            m_nOffsetX=e.getX()-BlockToBeMoved.getX();
            m_nOffsetY=e.getY()-BlockToBeMoved.getY();
            repaint();
            return;
        }
    }
    // Second, check the orginals 
    for (int i=originals.size()-1; i>=0; i--) {
        CTetriMino p=originals.get(i);
        System.out.println(p.containPoint(e.getX(), e.getY()));
        if (p.containPoint(e.getX(), e.getY())) {
            CTetriMino p2=new CTetriMino(p); // make a copy of p
            duplicates.add(p2); // add to the end
            BlockToBeMoved=p2;  // p2 is selected, to be moved
            m_nOffsetX=e.getX()-BlockToBeMoved.getX();
            m_nOffsetY=e.getY()-BlockToBeMoved.getY();
            repaint();
            return;
        }
    }
}

public void mouseReleased( MouseEvent e )
{
    BlockToBeMoved=null; // no shape selected
}

public void mouseEntered( MouseEvent e )
{
}

public void mouseExited( MouseEvent e )
{
}

public void mouseMoved( MouseEvent e )
{
}

public void mouseDragged( MouseEvent e )
{
    if (e.isMetaDown()) return; // ignore right button
    System.out.println(BlockToBeMoved);
    if (BlockToBeMoved!=null) {
        BlockToBeMoved.setX(e.getX()-m_nOffsetX);
        BlockToBeMoved.setY(e.getY()-m_nOffsetY);
        repaint();
    }

} // end method mouseDragged






}

      

I would show you what my program does when I run it, but I don't have a high enough reputation yet.

The bottom Jframe is what is compiled if I change this code:

case 0:
g.fillOval(X, Y, d, d);
g.fillOval(X+d,Y, d, d);
g.fillOval(X+d,Y-d, d, d);
g.fillOval(X+d+d,Y-d, d, d);
break;

      

:

case 0:
g.fillOval(X, Y, Width, Height);
break;

      

In both options, I can drag the green square. In the second option, I can drag the red circle. I CAN'T, however, move my red tetrimino. I just wonder why I can't move my red tetrimino. Eventually I want to be able to move all tetriminos and drag them under the second black line to remove them. I also want to be able to rotate the tetriminos by pressing righr click. Any code or advice is appreciated. If you need more information, I have provided it as soon as possible. Thank.

+3


source to share


1 answer


If I understand correctly, the solution should be as follows: Please create an image package as shown below. To do this, you need to execute two mouse events with label captions by clicking the mouse button to do this. Add an icon to the label, this should be an image of your image.

enter image description here

enter image description here

critical import



    import java.awt.Point;

      

events

   private void jLabel1MousePressed(java.awt.event.MouseEvent evt) {                                     
          initialClick = evt.getPoint();
    }  





     private void jLabel1MouseDragged(java.awt.event.MouseEvent evt) {                                     

        int thisX = jLabel1.getLocation().x;
        int thisY = jLabel1.getLocation().y;

        // Determine how much the mouse moved since the initial click
        int xMoved = (thisX + evt.getX()) - (thisX + initialClick.x);
        int yMoved = (thisY + evt.getY()) - (thisY + initialClick.y);

        // Move picture to this position
        int X = thisX + xMoved;
        int Y = thisY + yMoved;

        jLabel1.setLocation(X, Y);
        jLabel1.repaint();


    }                                    

      

+3


source







All Articles