Hi, I started to follow some tutorial on YouTube but now I'm really stuck. The window pups up but the little box that's suppose to follow my mouse doesn't show. And the colors doesn't change when click.
Thanks for the help, I appreciate it!
package mouse; import java.awt.*; import java.awt.event.MouseEvent; import java.awt.event.MouseMotionListener; import javax.swing.JFrame; public class Mouse extends JFrame implements MouseMotionListener { private Image dpImage; private Graphics dbg; boolean mouseDragged; int mx, my; public Mouse(){ setSize (400, 300); setVisible(true); setResizable(false); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); addMouseMotionListener(this); } @Override public void paint(Graphics g){ dpImage = createImage(getWidth(), getHeight()); dbg = dpImage.getGraphics(); paintComponents(dbg); g.drawImage(dpImage, 0, 0, this); } public void paintComponet(Graphics g) { if(mouseDragged){ g.setColor(Color.DARK_GRAY); g.fillRect(0, 0, getWidth(), getHeight()); g.setColor(Color.LIGHT_GRAY); g.fillRect(mx, my, 20, 20); } else{ g.setColor(Color.LIGHT_GRAY); g.fillRect(0, 0, getWidth(), getHeight()); g.setColor(Color.DARK_GRAY); g.fillRect(mx, my, 20, 20); } repaint(); } public static void main (String[] args){ Mouse mouse = new Mouse(); } @Override public void mouseDragged(MouseEvent e) { mx = e.getX()-10; my = e.getY()-10; mouseDragged = true; e.consume(); } @Override public void mouseMoved(MouseEvent e) { mx = e.getX(); my = e.getY(); mouseDragged = false; e.consume(); } }