Java arraylist of graphics
How can I store an ArrayList of graphics so that I can then call a drawString() command on a specific ArrayList position?
For example this is what I want but none of the code works :(
ArrayList<Graphics> list= new ArrayList<Graphics>();
//create a certain number of graphics
for (i开发者_运维技巧nt i = 0; i < 5; i++) {
list.add(new Graphics[i] = null);
}
//run drawString on a certain array value independently of the others
((Graphics) columns.get(2)).drawString("v", 50, 50);
((Graphics) columns.get(4)).drawString("xx", 10, 100);
You can't store an ArrayList of Grapics.
Custom painting is done by overriding the paintComponent() method of JPanel. Then in that method you iterate over the Strings that you want to paint using the Grapics object of the paintComponent() method.
Check out the Draw On Component
example from Custom Painting Approaches. The example draws a series of rectangles stored in an ArrayList, but the concept is the same.
精彩评论