1

Possible Duplicate:
Drawing multiple pixels/rectangles

In my code i wrote a method that creates a rectangle at mouseX, mouseY. but all it does is update the position of that rectangle so it follows the mouse, i want it to create a new one at the mouse every time the method runs, can someone please help?

this is my method

public void drawParticle(float x, float y){
    g.drawRect(x, y, 4, 4);
}

The main class Control call the drawParticle method;

import java.awt.Point;
import java.awt.geom.Point2D;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

public class Control extends BasicGameState {
    public static final int ID = 1;

    public Methods m = new Methods();
    public Graphics g = new Graphics();

    int mouseX;
    int mouseY;


    public void init(GameContainer container, StateBasedGame game) throws SlickException{
    }

    public void render(GameContainer container, StateBasedGame game, Graphics g) throws SlickException {
        m.drawParticle(mouseX, mouseY);
    }

    public void update(GameContainer container, StateBasedGame game, int delta) {
    }

    public void mousePressed(int button, int x, int y) {
        mouseX = x;
        mouseY = y;
    }

    public int getID() {
        return ID;
    }

}

Thanks - Shamus

Community
  • 1
  • 1
user1610541
  • 111
  • 1
  • 11
  • We need some more of your code to help you. E.g.: How is `drawParticle` called? – Stefan Neubert Oct 08 '12 at 02:29
  • okay, added class that calls the method – user1610541 Oct 08 '12 at 02:36
  • @user1610541 do you have any other draw / paint methods? – Serhiy Oct 08 '12 at 02:47
  • Thats exactly what I've been looking into! any idea how i could accomplish that? – user1610541 Oct 08 '12 at 02:48
  • 1
    I have had a loot into Slick2D documentation, it seems that its redrawing everything when method render is called. So what I suggest you to try is adding some list, after each click store the point there and when you do render, iterate through the list and draw every rectangle. – Serhiy Oct 08 '12 at 02:53
  • okay, makes sense! but now, how would i store the mouse clicks in a list? arrays? i am very new to them and don't understand one bit :L – user1610541 Oct 08 '12 at 02:57
  • 2
    In `mousePressed()` method store the values of x and y to `Point` class for example (by calling `Point point = new Point(x,y)`). Store the point into some ArrayList (or any other data structure). Just make sure you initialize your ArrayList only once. In `mousePressed()` add the value of point to ArrayList and finally in render make some cycle to get all the points and draw them. Hopes this helps. – Serhiy Oct 08 '12 at 03:02

2 Answers2

3

The long and short of it is, you need to maintain a list of the objects you want to paint on each paint cycle.

enter image description here

public class ColorMeRectangles {

    public static void main(String[] args) {
        new ColorMeRectangles();
    }

    public ColorMeRectangles() {

        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException ex) {
                } catch (InstantiationException ex) {
                } catch (IllegalAccessException ex) {
                } catch (UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setSize(400, 400);
                frame.setLocationRelativeTo(null);
                frame.setLayout(new BorderLayout());
                frame.add(new RectanglePane());
                frame.setVisible(true);

            }
        });

    }

    public class RectanglePane extends JPanel {

        private Point mousePoint;
        private List<Partical> particals;
        private Timer generator;
        private int min = -4;
        private int max = 4;

        public RectanglePane() {

            setBackground(Color.BLACK);

            particals = new ArrayList<Partical>(25);

            addMouseMotionListener(new MouseAdapter() {
                @Override
                public void mouseMoved(MouseEvent e) {
                    mousePoint = e.getPoint();
                    repaint();
                }
            });

            generator = new Timer(125, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {

                    if (mousePoint != null) {

                        int x = mousePoint.x + (min + (int) (Math.random() * ((max - min) + 1)));
                        int y = mousePoint.y + (min + (int) (Math.random() * ((max - min) + 1)));

                        Color color = new Color(
                                (int) (Math.random() * 255),
                                (int) (Math.random() * 255),
                                (int) (Math.random() * 255));

                        particals.add(new Partical(new Point(x, y), color));

                        repaint();

                    }

                }
            });
            generator.setRepeats(true);
            generator.setCoalesce(true);
            generator.start();

        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);

            Graphics2D g2d = (Graphics2D) g.create();
            for (Partical partical : particals) {

                partical.paint(g2d);

            }

            if (mousePoint != null) {

                g2d.setColor(Color.WHITE);
                g2d.drawRect(mousePoint.x - 2, mousePoint.y - 2, 4, 4);

            }

            g2d.dispose();

        }
    }

    public class Partical {

        private Point location;
        private Color color;

        public Partical(Point location, Color color) {
            this.location = location;
            this.color = color;
        }

        public Point getLocation() {
            return location;
        }

        public Color getColor() {
            return color;
        }

        public void paint(Graphics2D g2d) {
            g2d.setColor(color);
            g2d.drawRect(location.x - 4, location.y - 4, 8, 8);
        }
    }
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
1

One way to do this is to create a List as a member variable and add a new Rectangle each time the user clicks the mouse. Then in your render() method, iterate through the List of Rectangles and paint each one.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268