Results 1 to 6 of 6

Thread: Random drawRect

  1. #1
    Wallsurfer is offline Member
    Join Date
    Mar 2009
    Posts
    11
    Rep Power
    0

    Default Random drawRect

    Hi there.
    This is my first code post on this site. I am trying to create a building on the screen, really it is just a rectangle. Which I would like to have random parameters and then I will use a loop to make 5 of them once i have the first part figured out.

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    // new definition of a building class which will make a new 
    // frame and that frame will store some rect. images
    public class Skyline { 
     
      public static void main (String[] args){
    
        // create a new frame
        JFrame frame = new JFrame("Skyline");
        frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    
        // here the program will ask for a new panel which will get its definition
        // from the other .class file
        frame.getContentPane().add(new SkylinePanel());
    
        frame.pack();
        frame.setVisible(true);
    
      }
    }
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    // new definition of a building class which will make a new 
    // frame and that frame will store some rect. images
    public class Building { 
      
      // set private varibles even tho the whole program will be able to access them... I think
      private int areaX, areaY, x, y;
      private Color color;
      
      // Constructor get the building blueprints
      public Building (int sizeX, int sizeY, Color shade, int upperX, int upperY) {
        
    	 areaY = sizeY;
        areaX = sizeX;
        color = shade;
        x = upperX;
        y = upperY;
      }
    
      // makes the draw method which will be called in the driver program
      // this will use the graphics method
      public void draw (Graphics page) {
    
        // set the color and passes a varible to get the color
        page.setColor (color);
        // sets the rect properties 
        page.fillRect (x, y, areaX, areaY);
        
      }
      
      // area mutator
      public void setAreaX ( int sizeX ) {
        
        areaX = sizeX;
        
      }
      
      // Color mutator
      public void setColor ( Color shade ) {
      
        color = shade;
      
      }  
      
      // x mutator
      public void setX ( int upperX ) {
      
        x = upperX;
        
      }
    
      // y mutator
      public void setY ( int upperY ) {
      
        y = upperY;
        
      } 
      
      // area accessor
      public int getAreaX () {
      
        return areaX;
        
      }
        // area accessor
      public int getAreaY () {
      
        return areaY;
        
      }
      // Color accessor
      public Color getColor () {
      
        return color;
        
      }
      // x accessor
      public int getX () {
      
        return x;
        
      }
      // y accessor
      public int getY () {
      
        return y;
        
      }
    }
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    // new definition of a building class which will make a new 
    // frame and that frame will store some rect. images
    public class Building { 
      
      // set private varibles even tho the whole program will be able to access 
      private int areaX, areaY, x, y;
      private Color color;
      
      // Constructor get the building blueprints
      public Building (int sizeX, int sizeY, Color shade, int upperX, int upperY) {
        
        areaY = sizeY;
        areaX = sizeX;
        color = shade;
        x = upperX;
        y = upperY;
      }
    
      // makes the draw method which will be called in the driver program
      // this will use the graphics method
      public void draw (Graphics page) {
    
        // set the color and passes a varible to get the color
        page.setColor (color);
        // sets the rect properties 
        page.fillRect (x, y, areaX, areaY);
        
      }
      
      // area mutator
      public void setAreaX ( int sizeX ) {
        
        areaX = sizeX;
        
      }
      
      // Color mutator
      public void setColor ( Color shade ) {
      
        color = shade;
      
      }  
      
      // x mutator
      public void setX ( int upperX ) {
      
        x = upperX;
        
      }
    
      // y mutator
      public void setY ( int upperY ) {
      
        y = upperY;
        
      } 
      
      // area accessor
      public int getAreaX () {
      
        return areaX;
        
      }
        // area accessor
      public int getAreaY () {
      
        return areaY;
        
      }
      // Color accessor
      public Color getColor () {
      
        return color;
        
      }
      // x accessor
      public int getX () {
      
        return x;
        
      }
      // y accessor
      public int getY () {
      
        return y;
        
      }
    }
    This is for my java class and the code is modified from Java Software Solutions foundations of program design 4th edition Chapter 4 page 180-181

    It very modified tho.

    Thanks for any help. I believe that the problem is with this statment :

    new Building ( rndZ, rndB, Color.blue, rndC, rndD ).draw(page);

    not sure tho.

  2. #2
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    Ehm, you copied the class building twice, so I assume one of those should be the class SkylinePanel.
    I die a little on the inside...
    Every time I get shot.

  3. #3
    Wallsurfer is offline Member
    Join Date
    Mar 2009
    Posts
    11
    Rep Power
    0

    Default Haha

    like it says my first post lawl !

    ok sorry about that part
    here is the code

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    // name the driver program
    public class SkylinePanel extends JPanel {
      
      // declare the amount of buildings to be placed on the panel
      private Building building1;
     
      public SkylinePanel(){
    
        int rndZ =(int)((Math.random()*10));
        building1 = new Building ( rndZ, rndZ, Color.blue, rndZ, rndZ );
        
        setPreferredSize (new Dimension(300, 200));
        setBackground (Color.black);
      }
    
      // sets the buildings which where declared earlier
      // uses the paint method to display to the screen
      // this also calls the draw method which is in the file skyline 
      public void paintComponent (Graphics page) {
    
        super.paintComponent(page);
    
        building1.draw(page);
    
      }
    }
    I have changed it several times putting the call for the Building class in the paintComponent area , like this new Building (rndZ, rndZ, Color.blue, rndZ, rndZ ).draw(page);

    I sometimes can get a black screen to appear other times nothing.

  4. #4
    Wallsurfer is offline Member
    Join Date
    Mar 2009
    Posts
    11
    Rep Power
    0

    Default

    Interesting no one has any info on this... seems super easy

  5. #5
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    Congratulations. What's the problem/question?

  6. #6
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default Is this what you want?

    The question is at the top toadaly...

    I have an idea, but it may not be exactly what you are looking for.

    I think that your problem is that you are creating a rectangle in Building, and only creating one Building in SkylinePanel. The code creating your rectangle is this:
    Java Code:
    private Building building1;
    I believe you really want this statement

    Java Code:
    private Building building1, building2, building3, building4, building5;
    You would then have to position the rectangles created by this statement. I'm not sure how you should do that though.

    Hope this helps! :)
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

Similar Threads

  1. Replies: 8
    Last Post: 04-19-2009, 06:50 PM
  2. Random
    By koolhoney in forum New To Java
    Replies: 11
    Last Post: 09-30-2008, 04:39 AM
  3. Replies: 4
    Last Post: 09-05-2008, 05:47 PM
  4. Using Random
    By razmyasdfg in forum CLDC and MIDP
    Replies: 1
    Last Post: 07-27-2008, 11:47 PM
  5. random numbers without random class`
    By carlos123 in forum New To Java
    Replies: 4
    Last Post: 01-17-2008, 11:44 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
  •