swing - Java add rectangles to ArrayList and then draw them -
swing - Java add rectangles to ArrayList and then draw them -
i´m in need of help guys. seek add together rectangles arraylist , loop through list draw them all. i'm not sure if i'm using right approach, here code far.
edit: code not draw rectangles added arraylist. don't know if added right way, or accessed through for-loop in right way.
in testprogram
import java.awt.borderlayout; import java.awt.graphics; import java.util.arraylist; import javax.swing.jframe; public class testprogram extends jframe { private shaperectangle rectangle; public arraylist<shapes> list = new arraylist<shapes>(); public testprogram(string title) { super(title); setlayout(new borderlayout()); setdefaultcloseoperation(exit_on_close); setlocationrelativeto(null); initcomponents(); setsize(500, 700); setvisible(true); } private void initcomponents() { rectangle = new shaperectangle(); add(rectangle, borderlayout.center); list.add(rectangle); graphics g = getgraphics(); (int = 0; < list.size(); i++) { rectangle.draw(g); } } public static void main(string args[]) { new testprogram("drawing program"); } }
in class shaperectangles:
import java.awt.graphics; public class shaperectangle extends shapes { public shaperectangle() {} @override public void paintcomponent(graphics g) { super.paintcomponents(g); draw(g); } @override public void draw(graphics g) { g.drawline(20,20,60,60); g.drawline(130,30,80,11); g.drawrect(200,30,20,140); } }
in class shapes:
import java.awt.graphics; import javax.swing.jpanel; public abstract class shapes extends jpanel { abstract public void draw(graphics g);
}
this bad, either want add together component absolute positioning, or want draw it, take one.
this.add(rectangle, borderlayout.center); // add together component of panel. list.add(rectangle); // add together method of drawing on panel.
the latter work improve because drawing. if need drag-and-drop, add together child, add together drawing child.
you can phone call repaint()
on jframe update graphics after changing rectangle's coordinates , sizes.
import java.awt.borderlayout; import java.awt.graphics; import java.util.arraylist; import javax.swing.jframe; import javax.swing.swingutilities; public class drawshapes extends jframe { public arraylist<shape> shapelist = new arraylist<shape>(); public drawshapes(string title) { super(title); this.setlayout(new borderlayout()); this.setdefaultcloseoperation(exit_on_close); this.setsize(500, 700); this.setlocationrelativeto(null); this.initcomponents(); } private void initcomponents() { shapelist.add(new rectangleshape(20, 20, 60, 60)); shapelist.add(new rectangleshape(130, 30, 80, 11)); shapelist.add(new rectangleshape(200, 30, 20, 140)); } @override public void paint(graphics g) { (shape s : shapelist) { s.draw(g); } } public static void main(string args[]) { swingutilities.invokelater(new runnable() { @override public void run() { drawshapes d = new drawshapes("drawing program"); d.setvisible(true); } }); } }
rectangleshape import java.awt.graphics; public class rectangleshape extends shape { public rectangleshape(int x, int y, int width, int height) { super(x, y, width, height); } public rectangleshape() { super(); } @override public void draw(graphics g) { g.drawrect(getx(), gety(), getwidth(), getheight()); } }
shape import java.awt.graphics; public abstract class shape { private int x; private int y; private int width; private int height; public shape() { this(0, 0, 1, 1); } public shape(int x, int y, int width, int height) { this.x = x; this.y = y; this.width = width; this.height = height; } public abstract void draw(graphics g); public int getx() { homecoming x; } public void setx(int x) { this.x = x; } public int gety() { homecoming y; } public void sety(int y) { this.y = y; } public int getwidth() { homecoming width; } public void setwidth(int width) { this.width = width; } public int getheight() { homecoming height; } public void setheight(int height) { this.height = height; } }
java swing arraylist
Comments
Post a Comment