java - Drawing a polygon with mouse clicks not working? -
im trying draw shape mouse clicks..but frame displaying white background... have use buffered image because part of assignment...otherwise working... can me this? please...
import java.awt.borderlayout; import java.awt.color; import java.awt.dimension; import java.awt.eventqueue; import java.awt.graphics; import java.awt.graphics2d; import java.awt.image; import java.awt.rectangle; import java.awt.component.*; import java.awt.point; import java.awt.shape; import java.awt.event.mouseadapter; import java.awt.event.mouseevent; import java.awt.geom.ellipse2d; import java.awt.geom.line2d; import java.awt.geom.path2d; import java.awt.image.bufferedimage; import java.util.arraylist; import java.util.list; import java.awt.event.inputevent; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.uimanager; import javax.swing.unsupportedlookandfeelexception; public class drawpolygon extends jframe{ static int x, y; private static bufferedimage bi = new bufferedimage(500,500, bufferedimage.type_int_rgb); public static void main(string[] args) { new drawpolygon(); } public drawpolygon() { eventqueue.invokelater(new runnable() { @override public void run() { try { uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname()); } catch (classnotfoundexception ex) { } catch (instantiationexception ex) { } catch (illegalaccessexception ex) { } catch (unsupportedlookandfeelexception ex) { } jframe frame = new jframe("test"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setlayout(new borderlayout()); frame.add(new polypane()); frame.pack(); frame.setlocationrelativeto(null); frame.setvisible(true); } }); } public static class polypane extends jpanel { private mousehandler mousehandler; private path2d currentshape; private list<path2d> lstploys; private point lastpoint; private point currentpoint; private image img; private boolean initialize = true; public polypane() { lstploys = new arraylist<>(); } @override public dimension getpreferredsize() { return new dimension(500, 500); } @override public void addnotify() { super.addnotify(); addmouselistener(getmousehandler()); addmousemotionlistener(getmousehandler()); } @override public void removenotify() { removemouselistener(getmousehandler()); removemousemotionlistener(getmousehandler()); super.removenotify(); } public mousehandler getmousehandler() { if (mousehandler == null) { mousehandler = new mousehandler(); } return mousehandler; } ////////////akshay public void boundaryfill(int x, int y, color bcolor, color fcolor){ int current = bi.getrgb(x, y); if((current != bcolor.getrgb()) && (current != fcolor.getrgb())){ //bi.setrgb(x, y, fcolor.getrgb()); bi.setrgb(x, y, fcolor.getrgb()); repaint(); boundaryfill(x+1, y, bcolor, fcolor); boundaryfill(x-1, y, bcolor, fcolor); boundaryfill(x, y-1, bcolor, fcolor); boundaryfill(x, y+1, bcolor, fcolor); boundaryfill(x+1, y, bcolor, fcolor); boundaryfill(x-1, y, bcolor, fcolor); boundaryfill(x, y-1, bcolor, fcolor); boundaryfill(x, y+1, bcolor, fcolor); } else return; } /* @override protected void paintcomponent(graphics g) { super.paintcomponent(g); graphics2d g2d = (graphics2d) g.create(); if (lastpoint != null) { g2d.setcolor(color.red); g2d.filloval(lastpoint.x - 2, lastpoint.y - 2, 4, 4); } if (currentshape != null) { g2d.setcolor(color.red); g2d.draw(currentshape); if (lastpoint != null && currentpoint != null) { system.out.println(lastpoint + " - " + currentpoint); g2d.setcolor(new color(255, 0, 0, 64)); g2d.draw(new line2d.float(lastpoint, currentpoint)); } } g2d.setcolor(color.black); (shape shape : lstploys) { g2d.draw(shape); } g2d.dispose(); }*/ public void paint(graphics g){ super.paint(g); if(initialize){ img = (image) createpolygon(); g.drawimage(img, 0, 0, null); //g.drawi //initialize = false; }else{ g.drawimage((image)bi, 0,0,null); } } public bufferedimage createpolygon(){ graphics2d g2d = bi.creategraphics(); g2d.setcolor(color.white); g2d.fillrect(0, 0, 500, 500); g2d.setcolor(color.black); if (lastpoint != null) { g2d.setcolor(color.red); g2d.filloval(lastpoint.x - 2, lastpoint.y - 2, 4, 4); } if (currentshape != null) { g2d.setcolor(color.red); g2d.draw(currentshape); if (lastpoint != null && currentpoint != null) { system.out.println(lastpoint + " - " + currentpoint); g2d.setcolor(new color(255, 0, 0, 64)); g2d.draw(new line2d.float(lastpoint, currentpoint)); } } g2d.setcolor(color.black); (shape shape : lstploys) { g2d.draw(shape); } g2d.dispose(); return bi; } public class mousehandler extends mouseadapter { @override public void mouseclicked(mouseevent e) { if (e.getbutton() == mouseevent.button1) { if (e.getclickcount() == 1) { system.out.println("first point"); point p = e.getpoint(); lastpoint = p; if (currentshape == null) { currentshape = new path2d.float(); currentshape.moveto(p.x, p.y); } else { currentshape.lineto(p.x, p.y); } repaint(); } else if (e.getclickcount() == 2) { system.out.println("closed"); currentshape.closepath(); lstploys.add(currentshape); currentshape = null; lastpoint = null; repaint(); } else if (e.getclickcount() == 3) { //// initialize = false; x = e.getx(); y = e.gety(); system.out.println("right!!"+x+y); boundaryfill(x, y, color.black, color.blue); } } else if (e.getbutton() == mouseevent.button3) { if (e.getclickcount() == 1) { system.out.println("right!!"); } } } @override public void mousemoved(mouseevent e) { if (currentshape != null) { currentpoint = e.getpoint(); repaint(); } else { currentpoint = null; } } } } }
it might solve problem.
put below line under else if (e.getclickcount() == 3)
in mouseclicked()
method , remove paint()
method.
initialize = false;
the problem initialize
variable set false
@ start up.
Comments
Post a Comment