我们正在玩mouseDragged和multipleObjects。
我们正在尝试创建一个棋盘游戏,其中有8个棋子可以单独点击并拖动到棋盘上各自的位置(不使用jpanel,jframe等)。截至目前,我们已经为棋盘游戏创建了8个单独的棋子,但是当你点击/拖动棋子'2'时,它会让所有的棋子在最上面的棋子下聚集在一起。
我们的初始解决方案(未显示)是使用按钮在玩家之间循环,但我们想尝试在不使用按钮的情况下执行此操作。欢迎任何建议。
板:
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 这部分表示8个单独的
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();
}
}
}