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 just by dragging the mouse. The measurements are disproportionate and I’m using a rectangle as a pen, but it doesn’t come to 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();
}
}