We are playing around with mouseDragged and multipleObjects.
We are trying to create a board game that has 8 pieces that can be individually clicked and dragged to their respective location on the board (without using jpanel, jframe, etc). As of now we have created 8 individual pieces for the board game, however when you go to click/drag piece '2', it causes all of the pieces to come together under the top piece.
Our initial solution (not shown) is to use a button to cycle between players pieces, but we would like to try to do this without the use of a button. Any suggestions are welcome.
Board:
import java.applet.Applet;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.net.MalformedURLException;
import java.net.URL;
public class GameBoard extends Applet implements ActionListener,
MouseListener, MouseMotionListener
{
GamePiece gp1;
GamePiece gp2;
GamePiece gp3;
GamePiece gp4;
GamePiece gp5;
GamePiece gp6;
GamePiece gp7;
GamePiece gp8;
Image bg;
int xposR;
int yposR;
@Override
public void init()
{
System.out.println(getCodeBase().getPath().toString());
MediaTracker tracker = new MediaTracker(this);
try
{
bg = getImage(new URL("file:/C:/Backdroptest.png"));
}
catch (MalformedURLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
tracker.addImage(bg, 0);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
this.setSize(d);
gp1 = new GamePiece(0, 0, "blue");
gp2 = new GamePiece(40, 0, "red");
gp3 = new GamePiece(80, 0, "yellow");
gp4 = new GamePiece(120, 0, "green");
gp5 = new GamePiece(160, 0, "orange");
gp6 = new GamePiece(200, 0, "cyan");
gp7 = new GamePiece(240, 0, "gray");
gp8 = new GamePiece(280, 0, "magenta");
addMouseListener(this);
addMouseMotionListener(this);
}
@Override
public void paint(Graphics g)
{
g.drawImage(bg, 0, 0, this);
gp1.display(g);
gp2.display(g);
gp3.display(g);
gp4.display(g);
gp5.display(g);
gp6.display(g);
gp7.display(g);
gp8.display(g);
}
@Override
public void actionPerformed(ActionEvent ev)
{
}
@Override
public void mousePressed(MouseEvent e)
{
}
@Override
public void mouseReleased(MouseEvent e)
{
}
@Override
public void mouseEntered(MouseEvent e)
{
}
@Override
public void mouseExited(MouseEvent e)
{
}
@Override
public void mouseMoved(MouseEvent e)
{
}
@Override
public void mouseClicked(MouseEvent e)
{
}
@Override
public void mouseDragged(MouseEvent e)
{
gp1.setLocation(xposR, yposR);
gp2.setLocation(xposR, yposR);
xposR = e.getX();
yposR = e.getY();
repaint();
}
}
GamePieces This part denotes the 8 individual pieces
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
public class GamePiece implements MouseListener, MouseMotionListener
{
int xpos;
int ypos;
int circleWidth = 30;
int circleHeight = 30;
Boolean mouseClick;
Color c;
public GamePiece(int x, int y, String cc)
{
xpos = x;
ypos = y;
mouseClick = true;
if (!mouseClick)
{
xpos = 50;
ypos = 50;
}
setColor(cc);
}
public void setLocation(int x, int y)
{
xpos = x;
ypos = y;
// mouseClick = true;
}
public void setColor(String s)
{
if (s.equals("blue"))
{
c = Color.blue;
}
else if (s.equals("red"))
{
c = Color.red;
}
else if (s.equals("yellow"))
{
c = Color.yellow;
}
else if (s.equals("green"))
{
c = Color.green;
}
else if (s.equals("orange"))
{
c = Color.orange;
}
else if (s.equals("cyan"))
{
c = Color.cyan;
}
else if (s.equals("gray"))
{
c = Color.gray;
}
else
c = Color.magenta;
}
public void display(Graphics g)
{
g.setColor(c);
g.fillOval(xpos, ypos, circleWidth, circleHeight);
}
@Override
public void mousePressed(MouseEvent e)
{
mouseClick = true;
}
@Override
public void mouseReleased(MouseEvent e)
{
}
@Override
public void mouseEntered(MouseEvent e)
{
}
@Override
public void mouseExited(MouseEvent e)
{
}
@Override
public void mouseMoved(MouseEvent e)
{
}
@Override
public void mouseClicked(MouseEvent e)
{
}
@Override
public void mouseDragged(MouseEvent e)
{
if (mouseClick)
{
xpos = e.getX();
ypos = e.getY();
}
}
}