Multiple Rectangle Generation [duplicate]

这一生的挚爱 提交于 2019-12-11 09:36:46

问题


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


回答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.




回答2:


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

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


来源:https://stackoverflow.com/questions/12774618/multiple-rectangle-generation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!