Results 1 to 8 of 8
  1. #1
    Menre is offline Member
    Join Date
    Feb 2008
    Posts
    79
    Rep Power
    0

    Default Code errors (incompatible types, cannot find variables, etc)

    Hi All,

    I tried to creat an Applet chart by following a Java book written by Cay S. et al. The book is called Core Java 2, volume 11 (7th edition, 2004). I lifted some code from their book and when I build the file, I get some errors. I have been able to correct some, but still left with others such as incompatible types, cannot find symbol variables, etc.

    My code is pasted below. Could someone have a look at it for me please?

    Thanks in advance.
    Menre
    Java Code:
    import java.awt.*;
    import java.awt.font.*;
    import java.awt.geom.*;
    import javax.swing.*;
    
    /**
     * @author Menre: Code lifted from and based on the book 
     *Core Java 2 v.11 by Cay S. Horstmann and Gary
     * @version 1.00 2009/8/27
     */
    
    public class Chart extends JApplet 
    
    {
      public void init()
           
         {
            EventQueue.invokeLater(new Runnable()
            {
            	public void run()
            	{
            		String v = getParameter("values");
            		if (v == null) return;
            		int n = Integer.parseInt(v);
            		double[] values = new double[n];
            		String[] names = new String[n];
            		for (int i = 0; i < n; i++)
            		{
            			values[i] = Double.parseDouble(getParameter("value." + (i + 1)));
            			names[i] = getParameter("name." + (i + 1));
            		}
            		
            		add(new ChartComponent(values, names, getParameter("title")));
            	}
            });    	
            	
        }
    }
    
    /**
     *A component that draws a bar chart.
     */
     
     class ChartComponent extends JComponent
     {
     	/**
     	 *Construct a ChartComponent.
     	 *@param v the array of values for the chart
     	 *@param n the arrays of names for the values
     	 *@param t the title of the chart
     	 */
     	 public ChartComponent(double[] v, String[] n, String t)
     	 {
     	 	values = v;
     	 	names = n;
     	 	title = t; 	
     	 	
     	 }
     	 
     	 public void paintComponent(Graphics g)
     	 {
     	 	Graphics2D g2 = (Graphics2D) g;
     	 	
     	 	// compute the minimum and maximum values
     	 	if (values == null) return;
     	 	double minValue = 0;
     	 	double maxValue = 0;
     	 	for (double v : values)
     	 	{
     	 		if (minValue > v) minValue = v;
     	 		if (maxValue < v) maxValue = v;
     	 	}
     	 	if  (maxValue == minValue) return;
     	 	
     	 	int panelWidth = getWidth();
     	 	int panelHeight = getHeight();
     	 	
     	 	
     	 	Font titleFont = new Font("SansSerif", Font.BOLD, 20);
     	 	Font labelFont = new Font("SansSerif", Font.PLAIN, 10);
     	 	
     	 	//compute the extent of the title
     	 	FontRenderContext = g2.getFontRenderContext();
     	 	Rectangle2D titleBounds = titleFont.getStringBounds(title, context);
     	 	double titleWidth = titleBounds.getWidth();
     	 	double top = titleBounds.getHeight();
     	 	
     	 	//draw the title
     	 	double y = -titleBounds.getY(); //ascent
     	 	double x = (panelWidth - titleWidth) / 2;
     	 	g2.setFont(titleFont);
     	 	g2.drawString(title, (float) x, (float) y);
     	 	
     	 	//compute the extent of the bar labels
     	 	LineMetrics labelMetrics = labelFont.getLineMetrics("", context);
     	 	double bottom = labelMetrics.getHeight();
     	 	
     	 	y = panelHeight - labelMetrics.getDescent();
     	 	g2.setFont(labelFont);
     	 	
     	 	//get the scale factor and width for the bars
     	 	double scale = (panelHeight - top - bottom) / (maxValue - minValue);
     	 	int barWidth = panelWidth / values.lenght;
     	 	
     	 	//draw the bars
     	 	for (int i = 0; i < values.lenght; i++)
     	 	{
     	 		
     	 		// get the coordinates of the bar rectangle
     	 		double x1 = i * barWidth + 1;
     	 		double y1 = top;
     	 		double height = values[i] * scale;
     	 		if (values[i] >= 0) y1 += (maxValues - value[i]) * scale;
     	 		else
     	 		{
     	 			y1 += maxValue * scale;
     	 			height = -height;
     	 		}
     	 		
     	 		//fill the bar and draw the bar outline
     	 		Rectangle2D rect = new Rectangle2D.Double(x1, y1, barWidth - 2, height);
     	 		g2.setPaint(Color.Red);
     	 		g2.fill(rect);
     	 		g2.setPaint(Color.BLACK);
     	 		g2.draw(rect);
     	 		
     	 		//draw the center label below the bar
     	 		Rectangle2D lableBounds = labelFont.getStringBounds(names[i], context);
     	 		
     	 		double labelWidth = labelBounds.getWidth();
     	 		x = x1 + (barWidth - labelWidth) / 2;
     	 		g2.drawString(names[i], (float) x, (float) y);
     	 		}
     	 	}
     	 	
     	 	private double[] values;
     	 	private String[] names;
     	 	private String[] title;
     }

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

    Default

    Your code is filled with typos and careless errors and omissions.
    For instance:
    Java Code:
        //FontRenderContext = g2.getFontRenderContext();  // missing variable named "context"
        FontRenderContext context = g2.getFontRenderContext();
    and
    Java Code:
        //int barWidth = panelWidth / values.lenght; // misspelled "length"
        int barWidth = panelWidth / values.length;
    I recommend that you proof-read your code, comparing each error line with the book's code, and keep repeating until it compiles. Best of luck.
    Last edited by Fubarable; 08-28-2009 at 12:37 AM.

  3. #3
    Menre is offline Member
    Join Date
    Feb 2008
    Posts
    79
    Rep Power
    0

    Default

    Hi,

    Thanks for your quick response. I agree with you my code was populated with errors. I have corrected all now by going through the book and my code again. That however has not solved the problem. I think the issue is more than just typos. I had 11 errors when I submitted my first post and after the corrections, I now have one error left and it says 'cannot find symbol constructor ChartComponent...'

    I presume it is referring to this line of code 'add(new ChartComponent(values, names, getParameter("title")));'

    So what do you think is the error here?

    I hope I am not complicating issues here. See the lines below. The first one is exactly what the book says. I have now changed it to the second one by adding the square brackets to the String t. That has made the incompatible types issue to go away.

    ' public ChartComponent(double[] v, String[] n, String t)' - came from book

    'public ChartComponent(double[] v, String[] n, String[] t)' - now changed

    Doing that has taken some errors off. Do you think I did the right thing? How do I correct the last error above?


    Menre

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

    Default

    Can you do the following:
    1) repost your most recent rendition of the code, again with code tags.
    2) post the exact error message
    3) indicate which line of the code is causing the error (the error message will give you a line number so you'll know the exact line -- no need to guess) and mark it with a comment.

    Much luck!

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

    Default

    edit:

    No, don't do this:
    Java Code:
    public ChartComponent(double[] v, String[] n, String[] t)
    rather look at how you declare your title variable at the very bottom of your code. In the book is this an array of String? I think that it isn't.

  6. #6
    Menre is offline Member
    Join Date
    Feb 2008
    Posts
    79
    Rep Power
    0

    Default

    Hello again,

    I have done as you said and I am sure you will notice the corrections that I have made to get to where we are now. Line 33 is the line with the error according to the error message that came up, and I have made a comment on it. The line below is the error message I got.

    Java Code:
    cannot find symbol constructor ChartComponent(double[],java.lang.String[],java.lang.String)
    This is now the version of the full code after taken off the errors.
    Java Code:
    import java.awt.*;
    import java.awt.font.*;
    import java.awt.geom.*;
    import javax.swing.*;
    
    /**
     * @author Menre: Code lifted from and based on the book 
     *Core Java 2 v.11 by Cay S. Horstmann and Gary
     * @version 1.00 2009/8/27
     */
    
    public class Chart extends JApplet 
    
    {
      public void init()
           
         {
            EventQueue.invokeLater(new Runnable()
            {
            	public void run()
            	{
            		String v = getParameter("values");
            		if (v == null) return;
            		int n = Integer.parseInt(v);
            		double[] values = new double[n];
            		String[] names = new String[n];
            		for (int i = 0; i < n; i++)
            		{
            			values[i] = Double.parseDouble(getParameter("value." + (i + 1)));
            			names[i] = getParameter("name." + (i + 1));
            		}
            		
            		add(new ChartComponent(values, names, getParameter("title"))); //this is the line for the error message. It says line 33.
            	}
            });    	
            	
        }
    }
    
    /**
     *A component that draws a bar chart.
     */
     
     class ChartComponent extends JComponent
     {
     	/**
     	 *Construct a ChartComponent.
     	 *@param v the array of values for the chart
     	 *@param n the arrays of names for the values
     	 *@param t the title of the chart
     	 */
     	 public ChartComponent(double[] v, String[] n, String[] t)
     	 {
     	 	values = v;
     	 	names = n;
     	 	title = t; 	
     	 	
     	 }
     	 
     	 public void paintComponent(Graphics g)
     	 {
     	 	Graphics2D g2 = (Graphics2D) g;
     	 	
     	 	// compute the minimum and maximum values
     	 	if (values == null) return;
     	 	double minValue = 0;
     	 	double maxValue = 0;
     	 	for (double v : values)
     	 	{
     	 		if (minValue > v) minValue = v;
     	 		if (maxValue < v) maxValue = v;
     	 	}
     	 	if  (maxValue == minValue) return;
     	 	
     	 	int panelWidth = getWidth();
     	 	int panelHeight = getHeight();
     	 	
     	 	
     	 	Font titleFont = new Font("SansSerif", Font.BOLD, 20);
     	 	Font labelFont = new Font("SansSerif", Font.PLAIN, 10);
     	 	
     	 	//compute the extent of the title
     	 	FontRenderContext context = g2.getFontRenderContext();
     	 	Rectangle2D titleBounds = titleFont.getStringBounds("title", context);
     	 	double titleWidth = titleBounds.getWidth();
     	 	double top = titleBounds.getHeight();
     	 	
     	 	//draw the title
     	 	double y = -titleBounds.getY(); //ascent
     	 	double x = (panelWidth - titleWidth) / 2;
     	 	g2.setFont(titleFont);
     	 	g2.drawString("title", (float) x, (float) y);
     	 	
     	 	//compute the extent of the bar labels
     	 	LineMetrics labelMetrics = labelFont.getLineMetrics("", context);
     	 	double bottom = labelMetrics.getHeight();
     	 	
     	 	y = panelHeight - labelMetrics.getDescent();
     	 	g2.setFont(labelFont);
     	 	
     	 	//get the scale factor and width for the bars
     	 	double scale = (panelHeight - top - bottom) / (maxValue - minValue);
     	 	int barWidth = panelWidth / values.length;
     	 	
     	 	//draw the bars
     	 	for (int i = 0; i < values.length; i++)
     	 	{
     	 		
     	 		// get the coordinates of the bar rectangle
     	 		double x1 = i * barWidth + 1;
     	 		double y1 = top;
     	 		double height = values[i] * scale;
     	 		if (values[i] >= 0) y1 += (maxValue - values[i]) * scale;
     	 		else
     	 		{
     	 			y1 += maxValue * scale;
     	 			height = -height;
     	 		}
     	 		
     	 		//fill the bar and draw the bar outline
     	 		Rectangle2D rect = new Rectangle2D.Double(x1, y1, barWidth - 2, height);
     	 		g2.setPaint(Color.RED);
     	 		g2.fill(rect);
     	 		g2.setPaint(Color.BLACK);
     	 		g2.draw(rect);
     	 		
     	 		//draw the centered label below the bar
     	 		Rectangle2D labelBounds = labelFont.getStringBounds(names[i], context);
     	 		
     	 		double labelWidth = labelBounds.getWidth();
     	 		x = x1 + (barWidth - labelWidth) / 2;
     	 		g2.drawString(names[i], (float) x, (float) y);
     	 		}
     	 	}
     	 	
     	 	private double[] values;
     	 	private String[] names;
     	 	private String[] title;
     }
    I have used the code tag and hope you find it readable.
    Once again, thanks.
    Menre

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

  8. #8
    Menre is offline Member
    Join Date
    Feb 2008
    Posts
    79
    Rep Power
    0

    Smile

    Hi,

    Once again, thanks. I have now finally resolved it after reading your post #5 and by looking at the code again.

    It is now working without any errors. The title variable was not declared as an array of String, but I referenced is as an array of String at the bottom by mistakenly adding the square brackets. I have now taken them off and it is doing the right thing.

    Your post #5 helped. But you know, some books ain't just perfect. They come with their own typos as well and it takes the effort of a pro to notice that.

    Thanx,
    Menre (a smiling Java coder)

Similar Threads

  1. Open type (Ctrl-Shift-T) does not find types
    By Zhenya_Merom in forum Eclipse
    Replies: 2
    Last Post: 11-04-2009, 11:15 AM
  2. Replies: 3
    Last Post: 03-03-2009, 10:48 PM
  3. Replies: 3
    Last Post: 12-14-2008, 02:52 PM
  4. Replies: 6
    Last Post: 10-08-2008, 09:09 AM
  5. problem with scanner class:incompatible types
    By fred in forum New To Java
    Replies: 1
    Last Post: 07-20-2007, 08:02 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
  •