0

So i have the following code. basically what it does is get current location of x and y. If the user clicks inside the block, move the block so it wont be as jumpy.

public void mouseDragged(MouseEvent arg0) {
    Graphics g = getGraphics();


     x = arg0.getX();
     y = arg0.getY();
   if(x>= oldx || x<= oldx+20 && y>=oldy || y<=oldy+20){
    g.clearRect(oldx, oldy, 20, 20);
    g.fillRect(oldx, oldy+20, 20, 20);



    if(arg0.getX() == oldx && arg0.getY() == oldy){


    }else{
        g.clearRect(oldx, oldy+20, 20, 20);
        g.fillRect(arg0.getX(),  arg0.getY()+20, 20, 20);
        oldx = arg0.getX();
        oldy = arg0.getY();


    }
   }

}
Shaunak D
  • 20,588
  • 10
  • 46
  • 79
Alex
  • 175
  • 7
  • 1
    Don't use `getGraphics` EVER. Override the components `paintComponent` method and perform the painting from with in it. Use the `mouseDragged` event to update the state of the UI in order for the `paintComponent` method to paint it and call `repaint`! – MadProgrammer Apr 15 '15 at 02:58
  • Slight clarification: Don't use `getGraphics()` to obtain a component's Graphics context as you're trying to do, but it's OK to use this method to get a Graphics object from a BufferedImage. – Hovercraft Full Of Eels Apr 15 '15 at 03:12
  • Thanks for the input, but I am unfamiliar with paintComponent method. I was only taught to use getGraphics to show visual objects – Alex Apr 15 '15 at 03:16

0 Answers0