java:一次多个矩形 [英] java : multiple rectangles at once

查看:162
本文介绍了java:一次多个矩形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的,我不擅长解释问题,但我一直在网上搜索这个问题已经很久了。这是我的问题:我想在鼠标x和y处绘制一个矩形。我希望有很多矩形,所以如果我点击JFrame上的50,50坐标它会绘制一个矩形然后如果我点击其他地方它会在那里绘制另一个矩形,但不删除另一个所以我可以点击5次(< - 示例)然后我将同时有五个矩形。请帮助我,尽可能简单。提前谢谢你,请帮助我。

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.

我的代码:

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

    }

}

和另一个实现 MouseListener 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) {

        }
    }

}

这是添加到我的问题。什么与我的代码兼容。 (我仍然只是学习java而且我只有13而且不是很聪明。)矩形应该有一个固定的高度和宽度,所以当你点击一个特定的区域时它会画出10 x 10矩形,它会记住已经绘制的所有其他矩形,如你的例子中请再次帮助我谢谢

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:


  1. 在一个BufferedImage上绘制一个ArrayList

  2. 绘画对象

这篇关于java:一次多个矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆