Rectangle2D is not updating according to the X and Y of the mouse

1

Hello, I'm a beginner in Java, and I'm having problems with my code. My goal with it is that you can draw by just dragging the mouse. The measurements are disproportionate and I'm using a rectangle as a pen, but that's not the case because it's just a test.

Here is my code:         package Game;

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;

import javax.swing.JFrame;

public class Jogo extends JFrame implements MouseMotionListener {


    BufferedImage backBuffer;
    int janelaH = 500,
        janelaW = 800,
        mouseX = 250,
        mouseY = 400;


    public void paint() {
        Graphics g = getGraphics();
        Graphics2D g2 = (Graphics2D) g;
        Graphics bbg = backBuffer.getGraphics();

        Rectangle2D rect = new Rectangle2D.Double(this.mouseX,this.mouseY, 100, 100);
        g2.setPaint(new Color(255 , 255 , 255));
        g2.fill(rect);
    }

    public void iniciar() {
        setTitle("jogo");
        setVisible(true);
        setSize(janelaW, janelaH);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setResizable(false);
        setLayout(null);
        backBuffer = new BufferedImage(janelaW,janelaH, BufferedImage.TYPE_INT_RGB);
    }

    public void run() {
        iniciar();
        while(true) {
             paint();
        }
    }

    @Override
    public void mouseDragged(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseMoved(MouseEvent e) {
        // TODO Auto-generated method stub
        this.mouseX = (int) e.getX();
        this.mouseY = (int) e.getY();

    }

    public static void main(String[] args) {
        Jogo teste = new Jogo();
        teste.run();
    }
}
    
asked by anonymous 01.08.2015 / 22:26

0 answers