swing - Java changing colour of shape -
i'm trying create rectangle in java, done. can fill in solid colour, done. want alter solid colour of shape itself. know graphics can utilize g.setcolor(); have had component setup special way shown below:
public class design extends jcomponent { private static final long serialversionuid = 1l; private list<shape> shapesdraw = new arraylist<shape>(); private list<shape> shapesfill = new arraylist<shape>(); graphicsdevice gd = graphicsenvironment.getlocalgraphicsenvironment().getdefaultscreendevice(); int screenwidth = gd.getdisplaymode().getwidth(); int screenheight = gd.getdisplaymode().getheight(); public void paint(graphics g) { super.paintcomponent(g); graphics2d g2d = (graphics2d) g; for(shape s : shapesdraw){ g2d.draw(s); } for(shape s : shapesfill){ g2d.fill(s); } } public void drawrect(int xpos, int ypos, int width, int height) { shapesdraw.add(new rectangle(xpos, ypos, width, height)); repaint(); } public void fillrect(int xpos, int ypos, int width, int height) { shapesfill.add(new rectangle(xpos, ypos, width, height)); repaint(); } public void drawtriangle(int leftx, int topx, int rightx, int lefty, int topy, int righty) { shapesdraw.add(new polygon( new int[]{leftx, topx, rightx}, new int[]{lefty, topy, righty}, 3)); repaint(); } public void filltriangle(int leftx, int topx, int rightx, int lefty, int topy, int righty) { shapesfill.add(new polygon( new int[]{leftx, topx, rightx}, new int[]{lefty, topy, righty}, 3)); repaint(); } public dimension getpreferredsize() { homecoming new dimension(screenwidth, screenheight); } public int getwidth() { homecoming screenwidth; } public int getheight() { homecoming screenheight; } }
as can see, instead of drawing , filling, uses list draw off of that. there way can alter colour within list< shape >? preferably want colour changeable within each draw/fill shape.
thanks help.
updated answer:
my class follows shapewrapper example:
public class design extends jcomponent { private static final long serialversionuid = 1l; private list<shapewrapper> shapesdraw = new arraylist<shapewrapper>(); private list<shapewrapper> shapesfill = new arraylist<shapewrapper>(); graphicsdevice gd = graphicsenvironment.getlocalgraphicsenvironment().getdefaultscreendevice(); int screenwidth = gd.getdisplaymode().getwidth(); int screenheight = gd.getdisplaymode().getheight(); public void paintcomponent(graphics g) { super.paintcomponent(g); graphics2d g2d = (graphics2d) g; for(shapewrapper s : shapesdraw){ g2d.setcolor(s.color); g2d.draw(s.shape); } for(shapewrapper s : shapesfill){ g2d.setcolor(s.color); g2d.fill(s.shape); } } public void drawrect(int xpos, int ypos, int width, int height) { shapesdraw.add(new rectangle(xpos, ypos, width, height)); repaint(); } public void fillrect(int xpos, int ypos, int width, int height) { shapesfill.add(new rectangle(xpos, ypos, width, height)); repaint(); } public void drawtriangle(int leftx, int topx, int rightx, int lefty, int topy, int righty) { shapesdraw.add(new polygon( new int[]{leftx, topx, rightx}, new int[]{lefty, topy, righty}, 3)); repaint(); } public void filltriangle(int leftx, int topx, int rightx, int lefty, int topy, int righty) { shapesfill.add(new polygon( new int[]{leftx, topx, rightx}, new int[]{lefty, topy, righty}, 3)); repaint(); } public dimension getpreferredsize() { homecoming new dimension(getwidth(), getheight()); } public int getwidth() { homecoming screenwidth; } public int getheight() { homecoming screenheight; } } class shapewrapper { color color; shape shape; public shapewrapper(color color , shape shape){ this.color = color; this.shape = shape; } }
now coding in eclipse , works fine except 1 thing!! every time says shapesdraw/shapesfill.add() says:
the method add(shapewrapper) in type list not applicable arguments (rectangle)
so close! please respond.
you can utilize like:
private class shapewrapper { private color color; private shape shape; public shapewrapper(color color , shape shape){ this.color = color; this.shape = shape; } }
instead of plain shape
storing shape
+color
.
and paint them next :
public void paintcomponent(graphics g) { super.paintcomponent(g); graphics2d g2d = (graphics2d) g; for(shapewrapper s: shapesdraw){ g2d.setcolor(s.color); g2d.draw(s.shape); } for(shapewrappers s : shapesfill){ g2d.setcolor(s.color); g2d.fill(s.shape); } }
edit: according exception, seek add together typed list(shapewrapper
) object of class(shape
), prepare methods next :
public void drawrect(int xpos, int ypos, int width, int height) { shapewrapper wr = new shapewrapper(color.red,new rectangle(xpos, ypos, width, height)); shapesdraw.add(wr); repaint(); }
java swing colors paint graphics2d
No comments:
Post a Comment