Drawing multiple pixels/rectangles

∥☆過路亽.° 提交于 2019-12-02 16:57:21

问题


I'm trying to make a java sand game and can't get past one bit. i've made my method that draws a rectangle at mouseX and mouseY, and i have set it up so it updates every frame so it constantly follows the mouse.

what i assume is that i would use an array to create each rectangle, and from there would use a pre-defined algorithm to float to the ground, I'm all good with that, i just don't understand how to 'hook my method' up to an array.

This is the method i am using to draw the rectangle (in it's own class called Methods)

import org.newdawn.slick.Graphics;

public class Methods {

public Graphics g = new Graphics();

public int sizeX = 4;
public int sizeY = 4;

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

And this is my main class

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

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 mouseReleased(int button, int x, int y){
    mouseX = 0;
    mouseY = 0;
}

public void mouseDragged(int oldx, int oldy, int newx, int newy) {
    mouseX = newx;
    mouseY = newy;
}

public int getID() {
    return ID;
}

}

but when i click, just one rectangle follows the mouse, instead of many being created AT the mouse :L


回答1:


Public Variables:

Rectangle boxes[] = new Rectangle[maxnum];
int boxnum = 0;

On mouse move:

boxes[boxnum] = new Rectangle[e.getX(), e.getY(), sizeX, sizeY);
boxnum = boxnum + 1;

When drawing your particles:

counter = 0;
do
{
   g.drawRect(boxes[counter].x, boxes[counter].y, sizeX, sizeY);
   counter = counter + 1;
} while (counter < maxnum);

Where maxnum is the maximum number of boxes you will have. This way you can store multiple rectangles in your array and go through the array and draw them when you update the screen. Hope this helps.



来源:https://stackoverflow.com/questions/12766615/drawing-multiple-pixels-rectangles

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