swing - Java paintComponent as a JComponent -
don't know if specific title, have asked question has gone dead. trying execute paintcomponent() can draw rectangles, triangles , more class being jcomponent.
here code far:
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() { return new dimension(getwidth(), getheight()); } public int getwidth() { return screenwidth; } public int getheight() { return screenheight; } } class shapewrapper { color color; shape shape; public shapewrapper(color color , shape shape){ this.color = color; this.shape = shape; } }
as shown above, works fine, except being able choose colour. want able define rectangles , triangles respective positions , lengths want add colour it.
but error.
the error says:
the method add(shapewrapper) in type list< shapewrapper > not applicable arguments (rectangle)
and:
the method add(shapewrapper) in type list< shapewrapper > not applicable arguments (polygon)
please help! stressed trying figure out blocking me doing many things.
the answer pretty basic...shape
not type of shapewrapper
, therefore can't added list
decalred list<shapewrapper>
what should doing instead of
shapesdraw.add(new rectangle(xpos, ypos, width, height));
is more like...
shapesdraw.add(new shapewrapper(color.black, new rectangle(xpos, ypos, width, height)));
the same goes ...triangle
methods. need wrap resulting polygon
in shapewrapper
before trying add list
Comments
Post a Comment