Hello everyone. I'm still trying to make a game. XD Right now I'm trying to make a square so that you select it and then click a place and the square will move to that place. I have a program where one square can do that but I need to make it where more than one square can move. Can you help me?
Here is the program where one square will move:
So how would I make this so I can make mulitple squares selectable and moveable? My original idea was that I would have an array of squares and they each would implement runnable so that they would constantly check if they are selected and where to go. Thank you to anyone who replys!public class GameMove extends JFrame { //Alot of global vairables i took out to decrease size of post public static void main(String[] args) { new GameMove(); } public GameMove() { Runnable h = new AnimateThread(this); Thread t = new Thread(h); t.start(); this.setSize(WIDTH,HEIGHT); this.setTitle("Game"); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.add(new PaintScreen(), BorderLayout.CENTER); this.setVisible(true); } private class PaintScreen extends JComponent { public PaintScreen() { this.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { mouseX = e.getX(); mouseY = e.getY(); } public void mouseClicked(MouseEvent e) { //Sees if mouse is in the square if(s.contains(e.getPoint())) { if(isIn) { if(isClicked == true) { isClicked = false; } else if(isClicked == false) { isClicked = true; } } else { isIn = true; } } else { isIn = false; } } }); } public void paint(Graphics g) { Graphics2D g2 = (Graphics2D) g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON); s = new Rectangle2D.Float(x,y,20,20); s2 = new Rectangle2D.Float(x2,y2,20,20); if (isClicked == true) { g2.setColor(Color.GREEN); g2.fill(s); g2.fill(s2); } else { g2.setColor(Color.BLACK); g2.fill(s); g2.fill(s2); } if(isClicked && isIn == false) { if(mouseX != x) { if(mouseX > x) { x+= 1; } if(mouseX < x) { x -= 1; } } if (mouseY != y) { if (mouseY > y) { y += 1; } if (mouseY < y) { y -= 1; } } } g2.draw(s); } } private class AnimateThread implements Runnable { JFrame c; public AnimateThread(JFrame c) { this.c = c; } public void run() { while(true) { c.repaint(); } } } } class Unit implements Runnable { public boolean isClicked = false; public boolean isIn = false; public Rectangle2D s; int x; int y; ArrayList<Unit> unitList; public Unit(int x,int y,int w, int h) { s = new Rectangle2D.Float(x,y,w,h); this.x = x; this.y = y; this.unitList = unitList; } public void run() { while(true) { try { Thread.sleep(100); } catch(InterruptedException e) { e.printStackTrace(); } detect(); } } public void mousePressed(MouseEvent e) { GameMove.mouseX = e.getX(); GameMove.mouseY = e.getY(); } public void mouseClicked(MouseEvent e) { //Sees if mouse is in the square if(s.contains(e.getPoint())) { if(isIn) { if(isClicked == true) { isClicked = false; } else if(isClicked == false) { isClicked = true; } } else { isIn = true; } } else { isIn = false; } } public void detect() { // collision for(Unit u: unitList) { if(u != this && u.getBox().intersects(s)) { if(x > u.getX()) { x += 3; } if (x < u.getX()) { x -= 3; } if(y > u.getY()) { y += 3; } if (y < u.getY()) { y -= 3; } } } } public int getX() { return x; } public int getY() { return y; } public Rectangle2D getBox() { return s; } }
lil_misfit