Results 1 to 1 of 1
  1. #1
    Controloler is offline Member
    Join Date
    Dec 2010
    Posts
    2
    Rep Power
    0

    Default Strategy Game: MouseListener problem

    Hey guys, I joined this forum so I could ask you about this problem I'm stumped over. So far, the coding is simple enough. I want to select a rectangle, and move it. I have created a new class called Unit, extending the class Rectangle, and another class ColorPanel, which does most of the work.

    When I run my main program(simply making the GUI and adding a panel, I'll post it too at the bottom), the rectangles show, but I cannot move them.

    Here are the two codes.

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.Random;

    public class ColorPanel extends JPanel{

    private Unit database[];
    private Unit selectedUnit;
    private int x, y;

    public ColorPanel(Color backColor){
    setBackground(backColor);
    database = new Unit[10];
    for (int i = 0; i < database.length; i++){
    Unit c = new Unit(i * 25, 25, 20, 25, Color.red);
    database[i] = c;
    }
    selectedUnit = null;
    addMouseListener(new PanelListener());
    }

    public void paintComponent(Graphics g){
    super.paintComponent(g);
    for (Unit c: database)
    c.fill(g);
    }

    private class PanelListener extends MouseAdapter{

    public void mouseClicked(MouseEvent e){
    if(selectedUnit != null){
    int newX = e.getX();
    int newY = e.getY();
    int dx = newX - x;
    int dy = newY - y;
    selectedUnit.moveUnit(dx, dy);
    }
    x = e.getX();
    y = e.getY();
    repaint();
    for(int i = database.length - 1; i >= 0; i--){
    if (database[i].contains(x, y)){
    selectedUnit = database[i];
    break;
    }
    else
    selectedUnit = null;
    }
    }

    }
    }

    //And the Unit class:

    import java.awt.*;

    public class Unit extends Rectangle{

    private int x;
    private int y;
    private int height;
    private int width;
    private Color color;

    public Unit (int x1, int y1, int h, int w, Color c){
    x = x1;
    y = y1;
    height = h;
    width = w;
    color = c;
    }

    public void draw(Graphics g){
    Color oldColor = g.getColor();
    g.setColor(color);
    g.drawRect(x, y, height, width);
    g.setColor(oldColor);
    }

    public void fill(Graphics g){
    Color oldColor = g.getColor();
    g.setColor(color);
    g.fillRect(x, y, height, width);
    g.setColor(oldColor);
    }

    public void moveUnit(int xAmount, int yAmount){
    x = x + xAmount;
    y = y + yAmount;
    }
    }

    Can you see what is wrong?

    Main program:

    import javax.swing.*;
    import java.awt.*;

    public class GameGUI{

    public static void main(String[] args){
    JFrame GUI = new JFrame();
    GUI.setTitle("Map test");
    GUI.setSize(1100, 880);
    GUI.setResizable(false);
    GUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ;
    JPanel bkround = new JPanel();
    Color pale = new Color(255, 255, 200);
    ColorPanel map = new ColorPanel(pale);
    Container pane = GUI.getContentPane();
    pane.add(map);
    GUI.setVisible(true);
    }
    }

    EDIT: Thanks for all of your help guys, it was wonderful. I'm glad I'm a part of this forum, really.

    I figured it out, eventually. This thread can be closed.
    Last edited by Controloler; 12-24-2010 at 08:24 PM.

Similar Threads

  1. Developing an Hex-board strategy game
    By abegade in forum Java 2D
    Replies: 8
    Last Post: 05-03-2011, 12:16 PM
  2. Problem with mouseListener
    By js91723 in forum AWT / Swing
    Replies: 12
    Last Post: 05-31-2010, 08:49 PM
  3. Problem with mouseListener (REPOST)
    By js91723 in forum AWT / Swing
    Replies: 1
    Last Post: 05-31-2010, 06:15 PM
  4. 2D strategy game or 2D war game
    By led1433 in forum Java 2D
    Replies: 5
    Last Post: 02-10-2009, 07:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •