java - JComboBox change shapes and colors -
right have program can move rectangle left , right. make jcombobox change shape , color of object.
public class drawshapes extends jframe{ //code private canvasdrawarea canvas; // custom drawing canvas (extends jpanel) /** constructor set gui */ public drawshapes() { // panel jcombobox , buttons jpanel btnpanel = new jpanel(new flowlayout()); jcombobox shapes = new jcombobox(shapename); btnpanel.add(shapes); //code left/right buttons } } so above class contains , constructor sets gui. have inner class creates shape
class canvasdrawarea extends jpanel { public void paintcomponent(graphics rect) { super.paintcomponent(rect); setbackground(canvas_background); rect.setcolor(color.blue); rect.fillrect(x1, y1, rectwidth, rectheight); //these int defined earlier } } i thought if wanted change shape jcombobox, need place actionlistener jcombobox in canvasdrawarea class (the inner class). if that, can't add shape jcombobox in btnpanel. how change shape , color of object being moved?
here full code of writing question in case wants @ it.
edit: current jpanel buttons. copied code combo box.
jpanel btnpanel = new jpanel(new flowlayout()); jcombobox shapes = new jcombobox(shapename); shapes.setselectedindex(1); btnpanel.add(shapes); shapes.addactionlistener(new actionlistener(){ public void actionperformed(actionevent e) { jcombobox cb = (jcombobox)e.getsource(); //copies shapes combo box string msg = (string)cb.getselecteditem(); switch(msg){ case "rectangle": rectangle bluerect = new rectangle(x1, y1, rectwidth, rectheight, blue); canvas.add(bluerect); repaint(); case "circle": circle bluecirc = new circle(x2, y2, circwidth, circheight, blue); canvas.add(bluecirc); repaint(); }//switch end }//method end }); //action listener end and here's current rectangle class
public rectangle(int x, int y, int width, int height, color color) { setlocation(x, y); setsize(width, height); setcolor(color); } @override public void paint(graphics g) { g.setcolor(getcolor()); g.fillrect(getx(), gety(), getwidth(), getheight()); } my circle class same rectangle class. when run app, rectangle shows , no longer able moved.
- start creating concept of "shape", knows how paint self, @ specified location , specified color
- change
canvasdrawareaallow accept different instances of "shape" , repaint itself. - create
jcombobox, place within same containercanvasdrawarea, not within it. when user changes selection, tell instance ofcanvasdrawareachange shape accordingly...
the core concept is, canvasdrawarea responsible drawing shape, that's it. allow shape changed means, via series of setters , getters, provides flexibility, doesn't tie given mechanism (so change shape triggering actionevent), allows change way in shape changing mechanism works (such via random selection, radio buttons or list example)
for example...
class canvasdrawarea extends jpanel { //... private myshape shape; //.. public void setshape(myshape shape) { this.shape = shape; repaint(); } public myshape getshape() { return shape; } //... public void paintcomponent(graphics rect) { super.paintcomponent(rect); myshape shape = getshape(); if (shape != null) { shape.paint(rect); } } } - don't update state of component (or other component) within
paintmethod, example;setbackground(canvas_background);bad idea. schedule repaint cause repaint manager repeatedly repaint component, consuming cpu. instead should have been setting values before hand, perhaps in constructor...
start defining concept of "shape" , define it's requirements, example...
public interface myshape { public void setlocation(int x, int y); public void setsize(int width, int height); public void setcolor(color color); public int getx(); public int gety(); public int getwidth(); public int getheight(); public color getcolor(); public void paint(graphics g); } the create actual implementations, example...
public abstract class abstractshape implements myshape { private int x, y; private int width, height; private color color; @override public void setlocation(int x, int y) { this.x = x; this.y = y; } @override public void setsize(int width, int height) { this.width = width; this.height = height; } @override public int getwidth() { return width; } @override public int getheight() { return height; } @override public void setcolor(color color) { this.color = color; } @override public int getx() { return x; } @override public int gety() { return y; } @override public color getcolor() { return color; } } public class rectangle extends abstractshape { public rectangle() { } public rectangle(int x, int y, int width, int height, color color) { setlocation(x, y); setsize(width, height); setcolor(color); } @override public void paint(graphics g) { g.setcolor(getcolor()); g.drawrect(getx(), gety(), getwidth(), getheight()); } } where possible, deal interface, means when create more shapes, easier integrate them
Comments
Post a Comment