Results 1 to 3 of 3
Thread: Graphical view of an array
- 02-13-2009, 06:25 PM #1
Member
- Join Date
- Feb 2009
- Posts
- 5
- Rep Power
- 0
Graphical view of an array
Hi, I am pretty new to Java so I will try my best to describe my problem. Recently I have been assigned to do an exercise where I am to draw all elements in a random generated array with the "drawRect()" method. I would be very glad if someone could also explain how to put some space (5-10 pixels) between the drawn rectangles.
I will include a part of my code where I need the help:
Java Code:[B]public class PaintEx extends JPanel{ //declares a new array with 50 spaces int [] array = new int[50]; public PaintEx(){ } public void paintComponent(Graphics g){ super.paintComponent(g); g.setColor(Color.BLACK); //loop for filling the array with random elements from 0 to 300, and //also draw them as rectangles for (int i = 0; i<array.length; i ++){ Random a = new Random(); array [i] = a.nextInt(300); g.drawRect(array[i], 300-array[i], 10, array[i]); }[/B]
This code allows me to draw the graphics somewhat correct. The problem is that they appear in growing order starting from lower values and end with higher values. As I understand, the random utility should take care of a new random value every time the loop draws a rectangle. This does not happen, and the rectangles are mostly overlapping each other. My question: where did I do it wrong at the drawing bit? Or maybe even the array bit?
The reason for "300-array[i]" is because I want the rectangles to appear in a normal coordinate system and not the inverted y which Java uses. All help/pinpoints are gladly appreciated :).Last edited by ventrue; 02-13-2009 at 06:30 PM. Reason: Code fault
- 02-13-2009, 06:51 PM #2
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
question, why do you use an array when you do not re-use the values? a simple int would suffice, exchanging array.length with a constant.
also, try creating your Random object outside of the loop and see if that does something more to your liking.
sorry, can't be of too specific help due to my minimal experience with swing. anyways, you need to position your next rectangle based on its dimensions as well as the previous rectangle's dimensions and position. don't wanna be too specific, because i'm not too sure what you're really trying to do.
- 02-13-2009, 07:31 PM #3
Member
- Join Date
- Feb 2009
- Posts
- 5
- Rep Power
- 0
Thank you so much!! It worked :). As you said, moving the Random object out of the loop fixed the overlapping, but I also did another small modification I did not think of before: simply introducing a new integer "k" that handles the spacing between the rectangles + their positioning by a fixed value.
Sorry for not being able to post images but the forum says my post count needs to be 20+.Java Code:public class PaintEx extends JPanel{ int [] array = new int[20]; public PaintEx(){ } public void paintComponent(Graphics g){ super.paintComponent(g); g.setColor(Color.BLACK); Random a = new Random(); int k = 0; for (int i = 0; i<array.length; i ++){ array [i] = a.nextInt(300); g.drawRect(k, 300-array[i], 10, array[i]); k=k+15; }
Thanks again :)
Similar Threads
-
graphical representation of website structure
By mrpwnt in forum New To JavaReplies: 0Last Post: 12-09-2008, 10:08 PM -
Updating a view using actions in a seperate view
By xcallmejudasx in forum EclipseReplies: 0Last Post: 10-24-2008, 09:24 PM -
Use of Java2D on SWT or Draw2D graphical context
By Java Tip in forum SWTReplies: 0Last Post: 06-28-2008, 09:20 PM -
Graphical Ftp client
By Java Tip in forum java.netReplies: 0Last Post: 04-07-2008, 08:10 PM -
Help about how to implement a graphical editor in java
By xiul in forum Java 2DReplies: 0Last Post: 11-29-2007, 08:19 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks