java - Storing shapes in JComponent -


i unsure of how store multiple different shapes using 1 arraylist of type shape. here main.

public class a1 { public static boolean rdraw = false; public static boolean edraw = false; public static boolean ldraw = false; public static void main(string[] args) {     jframe frame = new jframe();     jbutton rect = new jbutton("rectangle");     rect.addactionlistener(new actionlistener (){         public void actionperformed(actionevent e){             if(e.getsource()==rect){                 rdraw = true;                 edraw = false;                 ldraw = false;             }         }     });     jbutton ellipse = new jbutton("ellipse");     ellipse.addactionlistener(new actionlistener (){         public void actionperformed(actionevent e){             if(e.getsource()==ellipse){                 rdraw = false;                 edraw = true;                 ldraw = false;                   }         }     });     jbutton edge = new jbutton("edge");     edge.addactionlistener(new actionlistener (){         public void actionperformed(actionevent e){             if(e.getsource() == edge){                 rdraw = false;                 edraw = false;                 ldraw = true;                    }         }     });     jbutton label = new jbutton("label");     jtextfield labelfield = new jtextfield(20);     jpanel panel = new jpanel();     panel.add(rect);     panel.add(ellipse);     panel.add(edge);     panel.add(label);     panel.add(labelfield);     frame.add(panel,borderlayout.north);     jcomponent canvas = new canvas();     frame.add(canvas,borderlayout.center);     frame.setdefaultcloseoperation(jframe.exit_on_close);     frame.setsize(600, 600);     frame.settitle("graph draw");     frame.setvisible(true);     } } 

in canvas, unsure of how use drawshape? , put in paintcomponent store shapes.

public class canvas extends jcomponent { private point p,p2; private rectangle r; private ellipse2d ep; private line2d l; arraylist<shape> shapes = new arraylist<shape>();  public canvas(){     class mymouselistener implements mouselistener{         @override         public void mouseclicked(mouseevent arg0) {             // todo auto-generated method stub          }          @override         public void mouseentered(mouseevent arg0) {             // todo auto-generated method stub          }          @override         public void mouseexited(mouseevent arg0) {             // todo auto-generated method stub          }         @override         public void mousepressed(mouseevent e) {             if(a1.rdraw == true && a1.edraw == false && a1.ldraw == false){                 p = e.getpoint();                 r = new rectangle(p.x, p.y, p.x - p.x, p.y - p.y);               }             else if (a1.rdraw == false && a1.edraw == true && a1.ldraw == false){                 p = e.getpoint();                 ep = new ellipse2d.double(p.x, p.y, p.x - p.x, p.y - p.y);             }             else if (a1.rdraw == false && a1.edraw == false && a1.ldraw == true){                 p = e.getpoint();                 l = new line2d.double(p.x, p.y, p.x - p.x, p.y - p.y);             }         }         @override         public void mousereleased(mouseevent e) {             if(a1.rdraw == true && a1.edraw == false && a1.ldraw == false){                 if (r.width != 0 || r.height != 0)                 {                     addrectangle(r);                 }                  r = null;             }             else if (a1.rdraw == false && a1.edraw == true && a1.ldraw == false){                 if (ep.getwidth() != 0 || ep.getheight() != 0){                     addellipse(ep);                 }                 ep = null;             }             else if (a1.rdraw == false && a1.edraw == false && a1.ldraw == true){                 if (l.getp1() != null || l.getp2() != null){                     addline(l);                 }                 l = null;             }         }     }     class mymousemotionlistener implements mousemotionlistener{         @override         public void mousedragged(mouseevent e) {             if(a1.rdraw == true && a1.edraw == false && a1.ldraw == false){                 int x = math.min(p.x, e.getx());                 int y = math.min(p.y, e.gety());                 int width = math.abs(p.x - e.getx());                 int height = math.abs(p.y - e.gety());                 r.setbounds(x, y, width, height);                 repaint();             }             else if (a1.rdraw == false && a1.edraw == true && a1.ldraw == false){                 int x = math.min(p.x, e.getx());                 int y = math.min(p.y, e.gety());                 int width = math.abs(p.x - e.getx());                 int height = math.abs(p.y - e.gety());                 ep.setframe(x, y, width, height);                 repaint();             }             else if (a1.rdraw == false && a1.edraw == false && a1.ldraw == true){                 p2 = e.getpoint();                 l.setline(p, p2);                 repaint();             }         }         @override         public void mousemoved(mouseevent arg0) {             // todo auto-generated method stub          }     }     mouselistener mlistener = new mymouselistener();     addmouselistener(mlistener);     mousemotionlistener mmlistener = new mymousemotionlistener();     addmousemotionlistener(mmlistener); } public void paintcomponent(graphics g){     super.paintcomponent(g);     graphics2d g2d = (graphics2d) g;     (shape shape:shapes){      }     if(a1.rdraw == true && a1.edraw == false && a1.ldraw == false){         g2d.draw(r);     }     else if (a1.rdraw == false && a1.edraw == true && a1.ldraw == false){         g2d.draw(ep);     }     else if (a1.rdraw == false && a1.edraw == false && a1.ldraw == true){         g2d.draw(l);     } } public void addrectangle(rectangle rect){     if(r != null){         rectangle cr = new rectangle(rect);         shapes.add(cr);         repaint();     } } public void addellipse(ellipse2d ellipse){     if (ep != null){         ellipse2d cep = new ellipse2d.double(ellipse.getx(),ellipse.gety(),ellipse.getwidth(),ellipse.getheight());         shapes.add(cep);         repaint();     } } public void addline(line2d line){     if(l != null){         line2d cl = new line2d.double(line.getp1(), line.getp2());         shapes.add(cl);         repaint();     } } } 

your paintcomponent method appears doing , little: little in it's not drawing shapes, , in it's trying things static boolean fields shouldn't.

"...and put in paintcomponent store shapes."

this shouldn't part of paintcomponent method. method drawing items on component , nothing more. should not used program logic. code storing shapes should in mouse listener type code, not in paintcomponent.

i think need in paintcomponent this:

public void paintcomponent(graphics g) {     super.paintcomponent(g);     graphics2d g2d = (graphics2d) g;     (shape shape : shapes) {         g2d.draw(shape); // !! added     } } 

as couple of side notes:

  • you've got static code within a1 class, including static fields, , of should instance code, , of code within main method should removed , placed constructor. main method should small.
  • you'll want rename canvas class different creates potential name clash core java class, java.awt.canvas, , might confuse others or compiler should move class package in future.
  • you can combine mouselistener , mousemotionlistener code single class extends mouseadapter. simplify code, , allow avoid posting empty method stops.
  • since drawing selections mutually exclusive, use jradiobuttons or jtogglebuttons held in single buttongroup, user can see selection active, , selection of 1 button turns off selection of others.

Comments

Popular posts from this blog

gridview - Yii2 DataPorivider $totalSum for a column -

java - Suppress Jboss version details from HTTP error response -

Sass watch command compiles .scss files before full sftp upload -