Results 1 to 3 of 3
  1. #1
    ventrue is offline Member
    Join Date
    Feb 2009
    Posts
    5
    Rep Power
    0

    Default 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 07:30 PM. Reason: Code fault

  2. #2
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    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.

  3. #3
    ventrue is offline Member
    Join Date
    Feb 2009
    Posts
    5
    Rep Power
    0

    Default

    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.

    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;
                    
                }
    Sorry for not being able to post images but the forum says my post count needs to be 20+.

    Thanks again :)

Similar Threads

  1. graphical representation of website structure
    By mrpwnt in forum New To Java
    Replies: 0
    Last Post: 12-09-2008, 11:08 PM
  2. Updating a view using actions in a seperate view
    By xcallmejudasx in forum Eclipse
    Replies: 0
    Last Post: 10-24-2008, 10:24 PM
  3. Use of Java2D on SWT or Draw2D graphical context
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 06-28-2008, 10:20 PM
  4. Graphical Ftp client
    By Java Tip in forum java.net
    Replies: 0
    Last Post: 04-07-2008, 09:10 PM
  5. Replies: 0
    Last Post: 11-29-2007, 09:19 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •