jframe - Issue drawing rectangle in negative direction - Java -


i'm having issues attempting draw rectangle in negative direction point clicked on screen. have following class simulates screen capture software gyazo:

class drawsquare extends jpanel implements mouselistener, mousemotionlistener {  // components public jdialog frame; public rectangle rectangle; public bufferedimage bufferedimage; public point start, end;  // variables public string capturedimage;  public drawsquare(jdialog frame) {      this.frame = frame;      // read in crosshair image replace mouse icon     toolkit tool = toolkit.getdefaulttoolkit();     image newimage = gettoolkit().getimage("components/cursor.png");     cursor cursor = tool.createcustomcursor(newimage, new point (this.frame.getx(), this.frame.gety()), "img");      this.frame.setcursor(cursor);     this.frame.addmouselistener(this);     this.frame.addmousemotionlistener(this);  }  @override public void paintcomponent(graphics g) {     super.paintcomponent(g);      graphics2d g2d = (graphics2d) g.create();     // draw background overlay     g2d.drawimage(bufferedimage, width, 0, this);     if (rectangle != null) {         //g2d.setcolor(new color(225, 225, 255, 128));         frame.setopacity(0.6f);         //g2d.fill(rectangle);         system.out.println(rectangle);         g2d.setcolor(new color(72,119,205));         g2d.draw(rectangle);     }     g2d.dispose(); }  @override public void mousedragged(mouseevent e) {      this.end = e.getpoint();     int width = end.x - start.x;     int height = end.y - start.y;      rectangle.setsize(new dimension(width, height));     frame.validate();     frame.repaint(); }  @override public void mousemoved(mouseevent e) {     this.start = e.getpoint();     frame.validate();     frame.repaint(); }   @override public void mousepressed(mouseevent e) {     // x , y point mouse pressed     rectangle = new rectangle(start);     system.out.println(rectangle);     // repaint screen     frame.validate();     frame.repaint(); }  @override public void mousereleased(mouseevent e) {}  @override public void mouseclicked(mouseevent e) {}  @override public void mouseentered(mouseevent e) {}  @override public void mouseexited(mouseevent e) {} 

}

now reason issue mentioned already, when attempt draw rectangle box in opposition or negative direction of point clicked on screen, doesn't draw, rectangle information looks during such attempt:

java.awt.rectangle[x=635,y=395,width=-316,height=-194]

however, when drag rectnagle in positive direction works supposed to:

enter image description here

what i'd know how can fix using negative values width/height, or doing way entirely.

you should have 2 points - drag start point , current drag point.

the rectangle calculated:

x=min(dragstartpoint.x, dragcurrentpoint.x) y=min(dragstartpoint.y, dragcurrentpoint.y) width=abs(dragstartpoint.x - dragcurrentpoint.x) height=abs(dragstartpoint.y - dragcurrentpoint.y) 

Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -