java - JLabel goes out of alignment (aka funky) when overlaying a rectangle on it -
i have gui making popular software imagemagick
in java swing.
now, implementing crop
feature , trying implement drawable box denote region cropped.
issue although have gotten rectangle draw on jlabel
, jlabel
starts move around once finish painting graphics on it.
example, here screenshot of app before , after selection made.
here code mousereleased()
event listener
private void input_showermousereleased(java.awt.event.mouseevent evt) { end_x = evt.getx(); end_y = evt.gety(); paint(input_shower.getgraphics()); input_shower.revalidate(); }
here code paint()
method
public void paint(graphics g) { super.paintcomponents(g); graphics2d g2 = (graphics2d) g; g2.setcolor(color.red); g2.setstroke(new basicstroke(5)); rectangle2d.double rectangle = new rectangle2d.double(start_x, start_y, (end_x - start_x), (end_y - start_y)); g2.draw(rectangle); }
are there ideas why happening , possible solutions?
this dangerous code:
private void input_showermousereleased(java.awt.event.mouseevent evt) { end_x = evt.getx(); end_y = evt.gety(); paint(input_shower.getgraphics()); input_shower.revalidate(); }
since you're painting directly component graphics object not given jvm. don't this, , instead paint passively.
instead use end_x , end_y in listened jcomponent's paintcomponent method , draw that.
e.g.,
import java.awt.color; import java.awt.dimension; import java.awt.graphics; import java.awt.event.mouseadapter; import java.awt.event.mouseevent; import java.awt.image.bufferedimage; import java.io.ioexception; import java.net.url; import javax.imageio.imageio; import javax.swing.*; @suppresswarnings("serial") public class paintcomponentcorrect extends jpanel { public static final string bull_fight = "https://duke.kenai.com/misc/bullfight.jpg"; private static final color rect_color = new color(150, 150, 255); private int startx; private int starty; private int endx; private int endy; private bufferedimage img; public paintcomponentcorrect() throws ioexception { url url = new url(bull_fight); img = imageio.read(url); mymouseadapt mymouseadapt = new mymouseadapt(); addmouselistener(mymouseadapt); addmousemotionlistener(mymouseadapt); } @override protected void paintcomponent(graphics g) { super.paintcomponent(g); if (img != null) { g.drawimage(img, 0, 0, null); } g.setcolor(rect_color); int x = math.min(startx, endx); int y = math.min(starty, endy); int width = math.abs(startx - endx); int height = math.abs(starty - endy); g.drawrect(x, y, width, height); } @override public dimension getpreferredsize() { if (ispreferredsizeset() || img == null) { return super.getpreferredsize(); } return new dimension(img.getwidth(), img.getheight()); } private class mymouseadapt extends mouseadapter { private bufferedimage subimg; @override public void mousepressed(mouseevent e) { if (e.getbutton() != mouseevent.button1) { return; } startx = e.getx(); starty = e.gety(); endx = startx; endy = starty; repaint(); } @override public void mousereleased(mouseevent e) { if (e.getbutton() != mouseevent.button1) { return; } endx = e.getx(); endy = e.gety(); repaint(); int x = math.min(startx, endx); int y = math.min(starty, endy); int w = math.abs(startx - endx); int h = math.abs(starty - endy); subimg = img.getsubimage(x, y, w, h); imageicon icon = new imageicon(subimg); joptionpane.showmessagedialog(paintcomponentcorrect.this, icon); } @override public void mousedragged(mouseevent e) { endx = e.getx(); endy = e.gety(); repaint(); } } private static void createandshowgui() { paintcomponentcorrect mainpanel = null; try { mainpanel = new paintcomponentcorrect(); } catch (ioexception e) { e.printstacktrace(); system.exit(-1); } jframe frame = new jframe("paintcomponent correct"); frame.setdefaultcloseoperation(jframe.dispose_on_close); frame.getcontentpane().add(mainpanel); frame.pack(); frame.setlocationbyplatform(true); frame.setvisible(true); } public static void main(string[] args) { swingutilities.invokelater(new runnable() { public void run() { createandshowgui(); } }); } }
Comments
Post a Comment