Results 1 to 4 of 4
  1. #1
    Join Date
    Mar 2011
    Posts
    93
    Rep Power
    0

    Default Help needed with "Variable is not public in component" error.

    Hello. I'm working on a GUI programming exercise. On compilation, I get multiple errors of the same sort:
    "Error: width is not public in Component; cannot be accessed by outside package". The error message first identifies the line
    Java Code:
    g.drawRect(0, 0, width-1, height-1);
    and then seems to identify every reference to the variable "width". The variable "width" is defined in the paintComponent method (along with a height variable).

    Can anyone tell me what I've done wrong?

    Here's the code:
    Java Code:
    /* A program to display a more complex GUI.  The user can select colors and draw on a display area using the mouse.  A clear button repaints the display.
    */
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class SimplePaint1 extends JApplet {
    	
    	/* The main routine opens a window that displays a 	drawing area and color palette.  This main routine 	allows the clase to run as a stand-alone application 	as well as an applet.  The main routine is not used 	when SimplePaint is run as an applet.
    	*/
    	public static void main(String[] args) {
    		JFrame window = new JFrame("Click and Draw");
    		SimplePaintPanel content = new SimplePaintPanel			();
    		window.setContentPane(content);
    		window.setSize(600, 480);
    		window.setLocation(100, 100);
    		window.setDefaultCloseOperation						(JFrame.EXIT_ON_CLOSE);
    		window.setVisible(true);
    	}
    
    	/* The init method of the applet simply creates a 	SimplePaintPanel and uses it as the content pane of 	the applet.  All the work is done by the 	SimplePaintPanel.
    	*/
    	public void init() {
    		setContentPane( new SimplePaintPanel() );
    	}
    
    	/*The SimplePaint panel creates a large white 	drawing surface and samples of color the user can 	click.  When the class is run as a stand-alone 	application, the content pane is a SimplePaintPanel.  	The SimplePaintPanel class does the real work of the 	program
    	*/
    	public static class SimplePaintPanel extends JPanel 		implements MouseListener, MouseMotionListener {
    	
    		// Color constants
    		private final static int BLACK = 0, 
    			RED = 1, GREEN = 2, BLUE = 3, CYAN = 4, 			MAGENTA = 5, YELLOW = 6;
    		private int currentColor = BLACK;
    
    		// Variables used when the user is drawing:
    		private int prevX, prevY; 
    		private boolean dragging;
    		
    		// A graphics context for the panel
    		private Graphics drawingGraphics;
    
    		/* The constructor for the SimplePaintPanel 			sets the 	background color to white, and adds 		the mouse listener
    		*/
    		SimplePaintPanel() {
    			setBackground(Color.LIGHT_GRAY);
    			addMouseListener(this);
    			addMouseMotionListener(this);
    		}
    
    		// Draws and repaints the contents of the 			// panel.
    		public void paintComponent(Graphics g) {
    			// Fills with the background color
    			super.paintComponent(g);
    
    	 		// Finds the width and height of the panel.			int width = getWidth();
    			int height = getHeight();
    			
    			/* This is the height of each color sample.  			There are 7 color samples and a CLEAR 				button that is 50x50 pixels.  There is a 			border 3 pixels wide between each color 			sample.  So the height of each color sample 			is total height of the panel reduced by the 			height of the CLEAR button and the height 			of the border at top and bottom, all 				divided by seven color samples.
    			*/
    			int colorSpace = (height - 56)/7;
    		 
    			/* Draw a border 3 pixels wide around the 			panel in gray.  The border is made by 				constructing 3 rectangles of different 				sizes.
    			*/
    			g.setColor(Color.DARK_GRAY);
    			g.drawRect(0, 0, width-1, height-1);
    			g.drawRect(1, 1, width-3, height-3);
    			g.drawRect(2, 2, width-5, height-5);
    
    			/* Draw a 56 pixel-wide rectangle along the 			right edge of the panel, as a space for the 			color samples.
    			*/
    			g.fillRect(width-56, 0, 56, height);
    			
    			/* Draw the CLEAR button, a 50x50 pixel 			rectangle in the lower right corner of the 			panel, allowing for a 3-pixel wide border  
    			*/
    			g.setColor(Color.WHITE);
    			g.fillRect(width-53, height-53, 50, 50);
    			g.setColor(Color.BLACK);
    			g.drawRect(width-53, height-53, 49, 49);
    			g.drawString("CLEAR", width-48, height-23);
    
    			// Draw the color sample rectangles
    			g.setColor(Color.BLACK);
    			g.fillRect(width-53, 3+0*colorSpace, 50, 				colorSpace-3);
    			g.setColor(Color.RED);
    			g.fillRect(width-53, 3+1*colorSpace, 50, 				colorSpace-3);
    			g.setColor(Color.GREEN);
    			g.fillRect(width-53, 3+2*colorSpace, 50, 				colorSpace-3);
    			g.setColor(Color.BLUE);
    			g.fillRect(width-53, 3+3*colorSpace, 50, 				colorSpace-3);
    			g.setColor(Color.CYAN);
    			g.fillRect(width-53, 3+4*colorSpace, 50, 				colorSpace-3);
    			g.setColor(Color.MAGENTA);
    			g.fillRect(width-53, 3+5*colorSpace, 50, 				colorSpace-3);
    			g.setColor(Color.YELLOW);
    			g.fillRect(width-53, 3+6*colorSpace, 50, 				colorSpace-3);
    
    			/* Draw a 2-pixel wide highlight border 			around the selected color sample
    			*/
    			g.setColor(Color.PINK);
    			g.drawRect(width-55, 
    				1+currentColor*colorSpace, 53, 					colorSpace);
    			g.drawRect(width-54, 
    				2+currentColor*colorSpace, 51, 					colorSpace-2);
    		}  	// end paintComponent()
    
    		/* Change the drawing color after the user has
     		clicked the color sample (mouse click with 			y-coordinate y).
    		*/
    		private void changeColor(int y) {
    			// Width and height of the panel
    			int width = getWidth(); 	
    			int height = getHeight();
    			int colorSpace = (height-56)/7;
    
    			// determine which color sample was chosen
    			int newColor = y / colorSpace;
    
    			// make sure the color number is valid
    			if(newColor < 0 || newColor > 6)
    				return;
    
    			/* Remove the highlight by drawing over it 			in gray.  Then change the current color and 			draw the highlight around the new color 			sample.
    			*/
    			Graphics g = getGraphics();
    			g.setColor(Color.GRAY);
    			g.drawRect(width-55, 
    				1+currentColor*colorSpace, 53, 					colorSpace);
    			g.drawRect(width-54, 
    				2+currentColor*colorSpace, 51, 					colorSpace-2);
    			currentColor = newColor;
    			g.setColor(Color.PINK);
    			g.drawRect(width-55, 
    				1+currentColor*colorSpace, 53, 					colorSpace);
    			g.drawRect(width-54, 
    				2+currentColor*colorSpace, 51, 					colorSpace-2);
    			g.dispose();
    		}	// end of changeColor()
    
    		/* This routine sets up the graphics context 		for drawing the mouse movements using the 			currently selected color.
    		*/
    		private void setUpDrawingGraphics() {
    			drawingGraphics = getGraphics();
    			switch (currentColor) {
    				case BLACK:
    					drawingGraphics.setColor							(Color.BLACK);
    					break;
    				case RED:
    					drawingGraphics.setColor							(Color.RED);
    					break;
    				case GREEN:
    					drawingGraphics.setColor							(Color.GREEN);
    					break;
    				case BLUE:
    					drawingGraphics.setColor							(Color.BLUE);
    					break;
    				case CYAN:
    					drawingGraphics.setColor							(Color.CYAN);
    					break;
    				case MAGENTA:
    					drawingGraphics.setColor							(Color.MAGENTA);
    					break;
    				case YELLOW:
    					drawingGraphics.setColor							(Color.YELLOW);
    					break;
    			}
    		}	// end setUpDrawingGraphics()
    
    		/*
    		This routine is called whenever the user clicks 		the mouse anywhere inside the panel. There are 		3 possible responses, depending on where the 		user clicked: change the current color; clear 		the drawing; or begin drawing.  Or do nothing 		if the user clicks on a border.
    		*/
    		public void mousePressed(MouseEvent evt) {
    			// X and Y coordinates where the user 				// clicked
    			int x = evt.getX();
    			int y = evt.getY();
    
    			// width and height of the panel
    			int width = getWidth();
    			int height = getHeight();
    
    			// Ignore mouse presses when the user is 			// drawing
    			if (dragging == true)
    				return;
    
    			/* If user clicked on the right of the 				drawing area, the click either occurred on 			a color sample or on the CLEAR button.
    			*/
    			if (x > width-53) {
    				// Click on the CLEAR button.
    				if (y > height-53) {	 	
    					repaint();	
    				}
    				// Click on a color sample.
    				else {
    					changeColor(y);
    				}
    			}
    			// If the user clicked on the drawing area, 			// then begin drawing.
    			else if (x > 3 && x < width-56 && y > 3 && 				y < height-3) {
    				prevX = x;
    				prevY = y;
    				dragging = true;
    				setUpDrawingGraphics();
    			}
    		}
    
    		/* This routine is called when the user 			releases the mouse button.  If the user is 			drawing, the drawing is done, and drawing 			should be set to false, and we can get rid of 		the graphics context we used when drawing.
    		*/
    		public void mouseReleased (MouseEvent evt) {
    			if (dragging == false)
    				return;
    			dragging = false;
    			drawingGraphics.dispose();
    			drawingGraphics = null;
    		}
    
    		/* This routine is called whenever the user 			move the mouse while a mouse button is held 			down.  If the user is drawing, draw a line 			segment from the previous mouse location to the 		curent mouse location and set up prevX and 			prevY for the next routine call.  When the user 		rags outside of the drawing area, the values of 		z and y are held to lie within the drawing 			area.  This prevents drawing on the color 			samples and CLEAR button.
    		*/
    		public void mouseDragged(MouseEvent evt) {
    			if (dragging == false)
    				return;
    			
    			// X and Y coordinates of mouse
    			int x = evt.getX();
    			int y = evt.getY();
    
    			// Holding x within the drawing area
    			if (x < 3)
    				x = 3;
    			if (x > getWidth() - 57)
    				x = getWidth() - 57;
    
    			// Holding y within the drawing area
    			if (y < 3)
    				y = 3;
    			if (y > getHeight() - 4)
    				y = getHeight() - 4;
    
    			// draw the line
    			drawingGraphics.drawLine(prevX, prevY, x, 				y);
    
    			// prepare coordinates for next drawing 			// segment
    			prevX = x;
    			prevY = y;
    		}
    
    		/* Empty routines for other mouse events 			required by the MouseListener and 					MouseMotionListener interfaces
    		*/
    		public void mouseEntered(MouseEvent evt) { }
    		public void mouseExited(MouseEvent evt) { }
    		public void mouseClicked(MouseEvent evt) { }			public void mouseMoved(MouseEvent evt) { }
    	}
    }

  2. #2
    jbarke12 is offline Member
    Join Date
    May 2013
    Location
    Charleston, South Carolina USA
    Posts
    82
    Rep Power
    0

    Default Re: Help needed with "Variable is not public in component" error.

    your width on line 57 would be commented out in your code. Width therefore wasn't defined within the correct scope therefore you are getting that error.

  3. #3
    Join Date
    Mar 2011
    Posts
    93
    Rep Power
    0

    Default Re: Help needed with "Variable is not public in component" error.

    Thank you, jbarke12.

    Indeed, when I removed the comment line (on line 57 above) altogether, the program then compiled without error and ran flawlessly.

    I write the code on a simple Win text editor (Win WordPad), and on my computer, the offending comment was less than one line long. It was only after I posted a copy of the code to this web-site and saw how it was printed out here that I thought the comment line could be "hiding" the width definition.

    Anyways, thanks for your help. I'm sorry to have wasted your time on this non-issue.

  4. #4
    jbarke12 is offline Member
    Join Date
    May 2013
    Location
    Charleston, South Carolina USA
    Posts
    82
    Rep Power
    0

    Default Re: Help needed with "Variable is not public in component" error.

    No worries.

Similar Threads

  1. Replies: 0
    Last Post: 12-07-2012, 08:29 AM
  2. Replies: 1
    Last Post: 11-02-2012, 11:38 PM
  3. "x" cannot be resolved to a variable - error
    By Naxix in forum New To Java
    Replies: 1
    Last Post: 03-18-2012, 08:31 PM
  4. Error "variable is never read"
    By Phitur in forum New To Java
    Replies: 6
    Last Post: 09-07-2010, 05:30 AM
  5. Error "can not find symbol variable"
    By FullMetalHollow in forum New To Java
    Replies: 5
    Last Post: 10-04-2009, 09:51 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
  •