用java画多个矩形框,java:一次多个矩形

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值