3

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();
    }

  }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
dynamicruss
  • 141
  • 2
  • [tag:javascript] tag removed. Your question has absolutely nothing to do with Javascript, and you'll want to avoid using misleading tags to avoid misleading folks who want to help you. – Hovercraft Full Of Eels Oct 22 '14 at 18:09
  • 1
    You state, `"without using jpanel, jframe, etc"` -- if this were my project, I would in fact use what you're avoiding: a JPanel, JFrame and use JLabels for your pieces. I'd then drag them in the JFrame's glasspane. – Hovercraft Full Of Eels Oct 22 '14 at 18:11
  • 1
    The last time I run this code, I couldn't replicate the problem as described... – MadProgrammer Oct 22 '14 at 20:33
  • BTW `bg = getImage(new URL("file:/C:/Backdroptest.png"));` that won't work on any computer but yours. The image should be stored in a place relative to the code base or document base. The URL should then be formed by the URL constructor that takes a base and relative path. – Andrew Thompson Oct 23 '14 at 23:50
  • 1) Why code an applet? If it is due to the teacher specifying it, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT using components in favor of Swing. – Andrew Thompson Oct 23 '14 at 23:51
  • 1
    @AndrewThompson Unrelated to this post, but I think it's time for a "Why teachers should teach students how to write MCVEs". – Paul Samsotha Oct 24 '14 at 01:32
  • 1
    @peeskillet No surer way to make it happen than to write it. :) My one experience with the blog software (especially in connection with code formatting) was so horrible that I won't attempt it again. – Andrew Thompson Oct 24 '14 at 01:38

0 Answers0