user2279603
user2279603

Reputation: 85

java : multiple rectangles at once

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 (<--example) then i would have five rectangles all at once. Please help me and make it as simple as possible. Thank you in advance please help me.

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

Upvotes: 0

Views: 405

Answers (3)

camickr
camickr

Reputation: 324108

See Custom Painting Approaches for working examples of the two common ways to do this:

  1. painting objects from an ArrayList
  2. painting on a BufferedImage

Upvotes: 2

Virginia Pasek
Virginia Pasek

Reputation: 31

If I understand your question correctly, it sounds like you would like to avoid redrawing the screen when a new object is added. There is a demo found at the Oracle website here which shows how to draw an object when you click.

http://docs.oracle.com/javase/tutorial/uiswing/examples/painting/SwingPaintDemo4Project/src/painting/SwingPaintDemo4.java

If you want to maintain the objects (ie. don't erase the previous one), comment out the line

repaint(CURR_X,CURR_Y,CURR_W+OFFSET,CURR_H+OFFSET);

Upvotes: 1

Durandal
Durandal

Reputation: 20059

There are two ways to do this, either remember each rectangle in a list and render them all (in proper order, oldest first in case they overlap), or create a BufferedImage to remember everything you have drawn so far.

Upvotes: 2

Related Questions