hi i'm new here and i'm not good at explaining questions but i have been searching for this on the web for ages. here is my question: i want to draw a rectangle at the mouse x and y. I want there to be lots of rectangles so if i was to click at the 50 ,50 coordinate on the JFrame it would draw a rectangle and then if i clicked somewhere else it would draw another rectangle there, but not delete the other one so i could have clicked 5 time (
my code:
public class Game extends Canvas implements Runnable {
private static final long serialVersionUID = 1L;
public boolean running = false;
public static final String title = "tilebased game!";
private Thread thread;
public int height = 600;
public int width = 800;
private Dimension d = new Dimension(width, height);
public static Rectangle block;
public static Rectangle[] blocks = new Rectangle[2];
public static int blocknum = 0;
public static int xCreate;
public static int yCreate;
public static int xcoord;
public static int ycoord;
public Game() {
setPreferredSize(d);
setMinimumSize(d);
setMaximumSize(d);
addMouseListener(new tile());
addMouseMotionListener(new tile());
}
public void start() {
running = true;
new Thread(this).start();
}
public void stop() {
running = false;
}
public static void main(String[] args) {
Game g = new Game();
JFrame f = new JFrame();
f.add(g);
f.pack();
f.setTitle(title);
f.setResizable(false);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLocationRelativeTo(null);
f.setVisible(true);
g.start();
}
public void run() {
while(running){
tick();
render();
}
try{
Thread.sleep(5);
}catch(Exception e){
}
}
public void render() {
BufferStrategy bs = getBufferStrategy();
if (bs == null) {
createBufferStrategy(2);
return;
}
Graphics g = bs.getDrawGraphics();
tile.render(g);
g.dispose();
bs.show();
}
public void tick() {
}
}
and the other class implementing MouseListener and MouseMotionListener:
public class listener implements MouseListener, MouseMotionListener {
public static Game game;
public Image img;
@Override
public void mouseDragged(MouseEvent arg0) {
}
@Override
public void mouseMoved(MouseEvent e) {
Game.xcoord = e.getX();
Game.ycoord = e.getY();
}
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent arg0) {
}
@Override
public void mouseExited(MouseEvent arg0) {
}
@Override
public void mousePressed(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON1) {
}
}
@Override
public void mouseReleased(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON1) {
}
}
}
this is an add on to my question. what would be compatable with my code. (i'm still only learning java by the way and i'm only 13 and not very smart.) the rectangles are supposed to have a fixed height and width aswell so when you click on a specific area it will draw a 10 x 10 rectangle and it will remember all the other rectangles already drawn like in your example please help me again thank you
解决方案
See Custom Painting Approaches for working examples of the two common ways to do this:
painting objects from an ArrayList
painting on a BufferedImage