多矩形生成

时间:2012-10-08 02:26:54

标签: java arrays arraylist slick2d particles

  

可能重复:
  Drawing multiple pixels/rectangles

在我的代码中,我编写了一个在mouseX,mouseY处创建矩形的方法。但它所做的只是更新那个矩形的位置,使它跟随鼠标,我想让它在每次方法运行时在鼠标上创建一个新的,有人可以帮忙吗?

这是我的方法

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

主类Control调用drawParticle方法;

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

}

谢谢 - Shamus

2 个答案:

答案 0 :(得分:3)

它的长短之处在于,您需要在每个绘制周期中维护要绘制的对象列表。

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

答案 1 :(得分:1)

执行此操作的一种方法是创建List作为成员变量,并在每次用户单击鼠标时添加新的Rectangle。然后在render()方法中,遍历List的{​​{1}}并绘制每个。{/ p>