Results 1 to 8 of 8
  1. #1
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    Default [newbie] Rectangle2D not drawn within applet

    I have this code snippet, and, somehow the applet is being drawn as an empty applet within Eclipse.

    :confused:

    Java Code:
    [B]Chart.java[/B]
    package homenetwork.bkr.training;
    import java.awt.*;
    import java.awt.font.*;
    import java.awt.geom.*;
    import javax.swing.*;
    
    @SuppressWarnings("serial")
    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")));
    			}
    		});
    	}
    }
    
    [B]ChartComponent.java[/B]
    package homenetwork.bkr.training;
    
    import java.awt.*;
    import java.awt.font.*;
    import java.awt.geom.*;
    import javax.swing.*;
    
    @SuppressWarnings("serial")
    public class ChartComponent extends JComponent {
    	
    	/**
    	 * Constructs a ChartComponent
    	 * @param v: the array of values for the chart
    	 * @param n: the array 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 paintComponents(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);  //TODO user can select color from a palette
    			g2.fill(rect);
    			g2.setPaint(Color.BLACK);
    			g2.draw(rect);
    			[B]if (debug) System.out.println("Draw rectangle");[/B]
    //Nothing is displayed in the console.
    			
    			//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;
    	private boolean debug = true;
    }

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Remove the "s" from paintComponents(Graphics g).

  3. #3
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    Default

    oh thanks. I would have expected that Java would have displayed the usual "50 line exception" :)

    Any more ideas coz there is no color in the applet..

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    there is no color in the applet
    I see a bar graph with red fill and black outline.

  5. #5
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    Default

    This is weird...I see a vanilla white applet loading.

    Video here.
    NOTE: Requires TeamViewer - Free Remote Access and Remote Desktop Sharing over the Internet

  6. #6
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Does your applet tag have the parameters? the applet is looking for them:
    Java Code:
    String v = getParameter("values");
    [b]if (v == null) return;[/b]
    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));
    }

  7. #7
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    Default

    I think so:

    Chart.java
    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")));
    }

    });
    }
    }

  8. #8
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

Similar Threads

  1. [SOLVED] [newbie] running an applet ??
    By jon80 in forum New To Java
    Replies: 4
    Last Post: 05-31-2009, 09:42 PM
  2. Replies: 0
    Last Post: 04-02-2009, 09:00 PM
  3. Canvas not being drawn
    By tacosc in forum Java Applets
    Replies: 4
    Last Post: 03-29-2009, 10:11 AM
  4. Newbie in applet, Help me
    By barney in forum Java Applets
    Replies: 1
    Last Post: 08-07-2007, 07:14 AM
  5. How do erase image drawn
    By fernando in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 05:26 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
  •