Results 1 to 7 of 7
  1. #1
    quinnvanorder is offline Member
    Join Date
    Feb 2012
    Posts
    31
    Rep Power
    0

    Default Trouble making graphics display

    I have a project where I am supposed to draw auto generated buildings of a specified width and height with random yellow windows. I feel that i can do this myself, however I am hitting a brick wall with the first step here. In my book they have a basic graphics class that is supposed to be making a oval. I typed the program out in hopes of tweaking this base program to fit my needs.

    The problem is, even though I typed the code over faithfully, i am hitting a compile error, and was wondering if I could get a second set of eyes to point out what is going on.
    The error I am getting is on line 15 of Building.java
    Java Code:
    super.paintComponent(page);
    I am using the Eclipse editor

    The error is
    Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problems:
    page cannot be resolved
    page cannot be resolved
    page cannot be resolved

    at Building.paintComponent(Building.java:15)
    at javax.swing.JComponent.paint(Unknown Source)
    at javax.swing.JComponent.paintChildren(Unknown Source)
    at javax.swing.JComponent.paint(Unknown Source)
    at javax.swing.JComponent.paintChildren(Unknown Source)
    at javax.swing.JComponent.paint(Unknown Source)
    at javax.swing.JLayeredPane.paint(Unknown Source)
    at javax.swing.JComponent.paintChildren(Unknown Source)
    at javax.swing.JComponent.paintToOffscreen(Unknown Source)
    at javax.swing.RepaintManager$PaintManager.paintDoubl eBuffered(Unknown Source)
    at javax.swing.RepaintManager$PaintManager.paint(Unkn own Source)
    at javax.swing.RepaintManager.paint(Unknown Source)
    at javax.swing.JComponent.paint(Unknown Source)
    at java.awt.GraphicsCallback$PaintCallback.run(Unknow n Source)
    at sun.awt.SunGraphicsCallback.runOneComponent(Unknow n Source)
    at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
    at java.awt.Container.paint(Unknown Source)
    at java.awt.Window.paint(Unknown Source)
    at javax.swing.RepaintManager.paintDirtyRegions(Unkno wn Source)
    at javax.swing.RepaintManager.paintDirtyRegions(Unkno wn Source)
    at javax.swing.RepaintManager.prePaintDirtyRegions(Un known Source)
    at javax.swing.RepaintManager.access$700(Unknown Source)
    at javax.swing.RepaintManager$ProcessingRunnable.run( Unknown Source)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)



    Here are the two files
    LAB_5.java
    Java Code:
    import javax.swing.JFrame;
    public class Lab_5 
    {
    
    	// Creates main frame
    	public static void main(String[] args) 
    	{	JFrame frame = new JFrame ("Building");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		Building panel = new Building();
    		
    		frame.getContentPane().add(panel);
    		
    		frame.pack();
    		frame.setVisible(true);
    				
    	}
    
    }
    and the other file
    Building.java
    Java Code:
    import javax.swing.JPanel;
    import java.awt.*;
    
    public class Building extends JPanel
    { 
    	private final int BASEX = 120, BASEY = 60; //base point for head
    	//constructor, sets up main charastics
    	public Building()
    	{
    		setBackground (Color.blue);
    		setPreferredSize (new Dimension(320,200));
    		setFont (new Font("Arial", Font.BOLD, 16));
    	}
    	//draws a face
    	public void paintComponent (Graphics Page)
    	{
    		super.paintComponent(page);
    		page.setColor (Color.yellow);
    		page.fillOval (BASEX,BASEY, 80, 80);
    		
    	}
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Trouble making graphics display

    Spelling matters and cApItAlIzAtIon too. Page != page.

    Oh, and welcome to the Java-Forums.org by the way!

  3. #3
    quinnvanorder is offline Member
    Join Date
    Feb 2012
    Posts
    31
    Rep Power
    0

    Default Re: Trouble making graphics display

    Oh wow, of course thanks. I cant believe I missed that.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Trouble making graphics display

    Quote Originally Posted by quinnvanorder View Post
    Oh wow, of course thanks. I cant believe I missed that.
    You're welcome. Yeah, I had to look at it several times myself to see the error, but once seen, it all makes sense. I think that sometimes we all need a second set of eyeballs. Since we don't have them, the forum can be a poor substitute.

    Much luck with your project!

  5. #5
    quinnvanorder is offline Member
    Join Date
    Feb 2012
    Posts
    31
    Rep Power
    0

    Default Re: Trouble making graphics display

    Ok so I have made much progress since then.

    The exact assignment is below

    "Design and implement a class called Building that represents a graphical depiction of a building, Allow the parameters to the constructor to specify the buildings width and height. Each building should be colored black and should contain a few random windows of yellow. Create a program that draws a random skyline of buildings."

    My two files are below. My progress so far is that I have made it display a window with a ground and a sun.
    From there it uses a set coordinate set BASEX and BASEY to build an entire house, complete with 4 windows, which have a 50/50 chance of appearing, based on whether the random number for that window is a 1 or a 0. I got this working for a single house as per the instructions, but the final step was to make it produce an entire skyline of houses with random windows based on the width and height passed. This is where I ran into problems. As i was building this, I went to extra trouble to make the entire thing be based off of one coordinate set, so that in a loop I could just add the width of the house to BASEX and theoreticaly it would create a whole new house to the right of the first one.

    So I took my working code and put the entire "house making" part and put it in a for loop that checks to make sure that BASEX+width were less than the window width (to prevent it making more houses than i have room to render). yet as soon as I put the house code in the 4 loop it does not diaplay the houses at all, only the background stuff. Any pointers would be appreciated.

    I will include both my working code without the for loop and the broken code with my attempt

    Lab_5.java
    Java Code:
    import javax.swing.JFrame;
    public class Lab_5 
    {
    
    	// Creates main frame
    	public static void main(String[] args) 
    	{	JFrame frame = new JFrame ("Building");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		Building panel = new Building(100,100);
    		
    		frame.getContentPane().add(panel);
    		
    		frame.pack();
    		frame.setVisible(true);
    				
    	}
    
    }

    Building.java (broken)
    Java Code:
    import javax.swing.JPanel;
    import java.awt.*;
    import java.util.Random;
    
    public class Building extends JPanel
    { 
    	int width, height; 
    	private final int xSize = 800, ySize = ((height+(width/2))+300);
    	int ground = (ySize-(ySize/8));
    	private int BASEX = 0, BASEY = ground; //base point for the house
        int quad1x, quad2x, quad3x, quad4x, quad1y, quad2y, quad3y, quad4y, spacerX, spacerY, w1, w2, w3, w4;// used to find the 4 quadrants of house for windows
    
    	//constructor, sets up main charastics
    	public Building(int w, int h)
    	{
    		setBackground (Color.blue);
    		setPreferredSize (new Dimension(xSize,ySize));
    		setFont (new Font("Arial", Font.BOLD, 16));
    		width = w;
    		height = h;
    	}
    	//draws a face
    	public void paintComponent (Graphics page)
    	{
    	    Random generator = new Random();
    		w1= generator.nextInt(2);
    		w2= generator.nextInt(2);
    		w3= generator.nextInt(2);
    		w4= generator.nextInt(2);
    		
    		super.paintComponent (page);
    		page.setColor (Color.yellow);
    		page.fillOval (xSize-100,-40, 80, 80); //sun
    	    page.setColor (Color.green); 
    	    page.fillRect (0, ground, xSize, (ySize-ground));  // ground
    
    	    for(int i = BASEX;(i+width)<xSize; i+=width)
    	    {
    	    	
    	    	int[]x = new int[5]; //crates the various x variables for the house polygon
    		    int[]y = new int[5];
    		    int n;  // count of points
    		    x[0]=(BASEX+(width/2)); /*top*/ x[1]=(BASEX+width); /*right*/ x[2]=x[1];  /*right bottom*/ x[3]=BASEX;  /*left Bottom*/ x[4]=x[3]; /*left*/
    		    y[0]=((BASEY)-height-(width/2)); /*top*/ y[1]=((BASEY)-height); /*right*/ y[2]=(BASEY);  /*right bottom*/  y[3]=(BASEY);  /*left bottom*/ y[4]=y[1]; /*left*/
    		    n = 5;
    		    
    		    Polygon myPoly = new Polygon(x, y, n);  // House
    		    page.setColor (Color.black);
    		    page.fillPolygon(myPoly);
    		   
    		    // windows vars
    		    quad1x =x[4]; quad2x =(x[4]+(width/2)); quad3x =x[4]; quad4x =quad2x;
    		    quad1y =y[4]; quad2y =y[4]; quad3y =(y[4]+(height/2)); quad4y =quad3y; 
    		    spacerX = (((width/2)/4)/2);
    		    spacerY = (((height/2)/4)/2);
    		  
    		    
    		    //top left window 
    		    page.setColor(Color.yellow);
    		    if (w1==1)
    		  	{
    		    	 page.fillRect(quad1x+spacerX, quad1y+spacerY, ((width/2)-spacerX*2), ((height/2)-spacerY*2));
    		  	}
    		    
    		    //top right
    		    if (w2==1)
    		    {	
    		    	page.fillRect(quad2x+spacerX, quad1y+spacerY, ((width/2)-spacerX*2), ((height/2)-spacerY*2));
    		    }
    		    //bottom left
    		    if (w3==1)
    		    {	
    		    	page.fillRect(quad1x+spacerX, quad3y+spacerY, ((width/2)-spacerX*2), ((height/2)-spacerY*2));
    		    }
    		    // bottom right
    		    if (w4==1)
    		    {	
    		    	page.fillRect(quad4x+spacerX, quad4y+spacerY, ((width/2)-spacerX*2), ((height/2)-spacerY*2));
    		    }
    		    BASEX+=width;
      
    	    }
    	    //House	    
    	    
    
    	
    	}
    }
    Building.java (working)
    Java Code:
    import javax.swing.JPanel;
    import java.awt.*;
    import java.util.Random;
    
    public class Building extends JPanel
    { 
    	int width, height; 
    	private final int xSize = 800, ySize = ((height+(width/2))+300);
    	int ground = (ySize-(ySize/8));
    	private int BASEX = 0, BASEY = ground; //base point for the house
        int quad1x, quad2x, quad3x, quad4x, quad1y, quad2y, quad3y, quad4y, spacerX, spacerY, w1, w2, w3, w4;// used to find the 4 quadrants of house for windows
    
    	//constructor, sets up main charastics
    	public Building(int w, int h)
    	{
    		setBackground (Color.blue);
    		setPreferredSize (new Dimension(xSize,ySize));
    		setFont (new Font("Arial", Font.BOLD, 16));
    		width = w;
    		height = h;
    	}
    	//draws a face
    	public void paintComponent (Graphics page)
    	{
    	    Random generator = new Random();
    		w1= generator.nextInt(2);
    		w2= generator.nextInt(2);
    		w3= generator.nextInt(2);
    		w4= generator.nextInt(2);
    		
    		super.paintComponent (page);
    		page.setColor (Color.yellow);
    		page.fillOval (xSize-100,-40, 80, 80); //sun
    	    page.setColor (Color.green); 
    	    page.fillRect (0, ground, xSize, (ySize-ground));  // ground
    
    	    	int[]x = new int[5]; //crates the various x variables for the house polygon
    		    int[]y = new int[5];
    		    int n;  // count of points
    		    x[0]=(BASEX+(width/2)); /*top*/ x[1]=(BASEX+width); /*right*/ x[2]=x[1];  /*right bottom*/ x[3]=BASEX;  /*left Bottom*/ x[4]=x[3]; /*left*/
    		    y[0]=((BASEY)-height-(width/2)); /*top*/ y[1]=((BASEY)-height); /*right*/ y[2]=(BASEY);  /*right bottom*/  y[3]=(BASEY);  /*left bottom*/ y[4]=y[1]; /*left*/
    		    n = 5;
    		    
    		    Polygon myPoly = new Polygon(x, y, n);  // House
    		    page.setColor (Color.black);
    		    page.fillPolygon(myPoly);
    		   
    		    // windows vars
    		    quad1x =x[4]; quad2x =(x[4]+(width/2)); quad3x =x[4]; quad4x =quad2x;
    		    quad1y =y[4]; quad2y =y[4]; quad3y =(y[4]+(height/2)); quad4y =quad3y; 
    		    spacerX = (((width/2)/4)/2);
    		    spacerY = (((height/2)/4)/2);
    		  
    		    
    		    //top left window 
    		    page.setColor(Color.yellow);
    		    if (w1==1)
    		  	{
    		    	 page.fillRect(quad1x+spacerX, quad1y+spacerY, ((width/2)-spacerX*2), ((height/2)-spacerY*2));
    		  	}
    		    
    		    //top right
    		    if (w2==1)
    		    {	
    		    	page.fillRect(quad2x+spacerX, quad1y+spacerY, ((width/2)-spacerX*2), ((height/2)-spacerY*2));
    		    }
    		    //bottom left
    		    if (w3==1)
    		    {	
    		    	page.fillRect(quad1x+spacerX, quad3y+spacerY, ((width/2)-spacerX*2), ((height/2)-spacerY*2));
    		    }
    		    // bottom right
    		    if (w4==1)
    		    {	
    		    	page.fillRect(quad4x+spacerX, quad4y+spacerY, ((width/2)-spacerX*2), ((height/2)-spacerY*2));
    		    }
    		    
      
    	  
    	   	    
    	    
    
    	
    	}
    }

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Trouble making graphics display

    I think you need another class. Some suggestions:
    • First of all, the class you're calling Building is actually responsible for displaying the whole shebang, so this should be renamed SkyLine or something similar.
    • Next you should create a class called Building that does not extend a JComponent such as a JPanel but in fact should extend nothing.
    • Give Building a draw(Graphics g) method that lets it draw itself.
    • Give it a constructor that allows you to pass in its x position and its height (the latter will be randomly determined in the SkyLine class. Let Building choose random spots for windows.
    • Give SkyLine an ArrayList of Building -- ArrayList<Building>
    • In SkyLine's paintComponent method iterate through the ArrayList calling draw(g) on each Building object held by the collection passing in the Graphics object obtained from the JVM (the paintComponent parameter).

  7. #7
    quinnvanorder is offline Member
    Join Date
    Feb 2012
    Posts
    31
    Rep Power
    0

    Default Re: Trouble making graphics display

    Well I tried to follow those steps and i made my code hopelessly convoluted, so I went back to the origonal and am trying to make it accept 3 variables this time. It takes the x coordinate to start with, the width and the height.

    I am able to make it take in and assign the values and it all works fine, but i cannot assign the variable that i get for x to the BASEX, thus moving everything to the right.

    I have a test display code setup that displays 2 numbers, first that of the variable that it pulls in (xCord), and then that of the variable BASEX. These variables should match, why do they not?

    Lab_5.java
    Java Code:
    import javax.swing.JFrame;
    public class Lab_5 
    {
    
    	// Creates main frame
    	public static void main(String[] args) 
    	{	JFrame frame = new JFrame ("Building");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		Building panel = new Building(69,100,100);
    
    		frame.getContentPane().add(panel);
    		
    		frame.pack();
    		frame.setVisible(true);
    				
    	}
    
    }
    Building.java
    Java Code:
    import javax.swing.JPanel;
    import java.awt.*;
    import java.util.Random;
    
    public class Building extends JPanel
    { 
    	public int width, height, quad1x, quad2x, quad3x, quad4x, quad1y, quad2y, quad3y, quad4y, spacerX, spacerY, w1, w2, w3, w4, xCord= 0;// used to find the 4 quadrants of house for windows
    	int ground = 350;
    	
    	//constructor, sets up main charastics
    	public Building(int xC, int w, int h)
    	{
    		setBackground (Color.blue);
    		setPreferredSize (new Dimension(800,400));
    		setFont (new Font("Arial", Font.BOLD, 16));
    		width = w;
    		height = h;
    		xCord = xC;
    	
    		
    	}
    
    	public int BASEX = xCord , BASEY = ground; //base point for the house
    
    	
    	//draws a face
    	public void paintComponent (Graphics page)
    	{
    	    Random generator = new Random();
    		w1= generator.nextInt(2);
    		w2= generator.nextInt(2);
    		w3= generator.nextInt(2);
    		w4= generator.nextInt(2);
    		
    		super.paintComponent (page);
    		page.setColor (Color.yellow);
    		page.fillOval (700,-40, 80, 80); //sun
    	    page.setColor (Color.green); 
    	    page.fillRect (0, ground, 800, 50);  // ground
    
    	    int[]x = new int[5]; //crates the various x variables for the house polygon
    		int[]y = new int[5];
    		int n;  // count of points
    		x[0]=(BASEX+(width/2)); /*top*/ x[1]=(BASEX+width); /*right*/ x[2]=x[1];  /*right bottom*/ x[3]=BASEX;  /*left Bottom*/ x[4]=x[3]; /*left*/
    		y[0]=((BASEY)-height-(width/2)); /*top*/ y[1]=((BASEY)-height); /*right*/ y[2]=(BASEY);  /*right bottom*/  y[3]=(BASEY);  /*left bottom*/ y[4]=y[1]; /*left*/
    		n = 5;
    		    
    		Polygon myPoly = new Polygon(x, y, n);  // House
    		page.setColor (Color.black);
    		page.fillPolygon(myPoly);
    				    // windows vars
    		quad1x =x[4]; quad2x =(x[4]+(width/2)); quad3x =x[4]; quad4x =quad2x;
    		quad1y =y[4]; quad2y =y[4]; quad3y =(y[4]+(height/2)); quad4y =quad3y; 
    		spacerX = (((width/2)/4)/2);
    		spacerY = (((height/2)/4)/2);
    		  
    		    
    		 
    		page.setColor(Color.yellow);
    		//test code
    		String derp = "" + xCord, derp2 = "" + BASEX;
    		page.drawString (derp, 50, 50);
    		page.drawString (derp2, 50, 65);
    		
    		 
    		//top left window
    		 if (w1==1)
    		{
    		 	 page.fillRect(quad1x+spacerX, quad1y+spacerY, ((width/2)-spacerX*2), ((height/2)-spacerY*2));
    		}
    		   
    		//top right
    		   if (w2==1)
    		    {	
    		    	page.fillRect(quad2x+spacerX, quad1y+spacerY, ((width/2)-spacerX*2), ((height/2)-spacerY*2));
    		    }
    		    //bottom left
    		    if (w3==1)
    		    {	
    		    	page.fillRect(quad1x+spacerX, quad3y+spacerY, ((width/2)-spacerX*2), ((height/2)-spacerY*2));
    		    }
    		    // bottom right
    		    if (w4==1)
    		    {	
    		    	page.fillRect(quad4x+spacerX, quad4y+spacerY, ((width/2)-spacerX*2), ((height/2)-spacerY*2));
    		    }
    		    
      
    	   	    
    	    
    
    	
    	}
    }

Similar Threads

  1. Can't display my graphics in a JPanel
    By handro1104 in forum AWT / Swing
    Replies: 2
    Last Post: 02-14-2011, 03:40 PM
  2. Drawing a graphics onto another Graphics ?
    By Ziden in forum Java Applets
    Replies: 0
    Last Post: 01-08-2011, 07:30 PM
  3. Replies: 1
    Last Post: 12-01-2010, 06:35 PM
  4. Replies: 1
    Last Post: 05-31-2010, 08:46 AM
  5. Replies: 8
    Last Post: 12-29-2008, 07:12 AM

Posting Permissions

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