Results 1 to 1 of 1
  1. #1
    rhexis is offline Member
    Join Date
    Nov 2011
    Posts
    56
    Rep Power
    0

    Default Problems drawing lines on a Graph

    I have googled around and manage to find a code in this forum that teaches you how to plot a graph.

    I have manipulated it to suit my code, but have found a problem.

    I will present to you 2
    Java Code:
    import java.awt.*;
    import java.awt.font.*;
    import java.awt.geom.*;
    import java.util.ArrayList;
    
    import javax.swing.*;
    
    public class GraphingData extends JPanel {
    	ArrayList<Double> data;
    
    	final int PAD = 20;
    
    	protected void paintComponent(Graphics g) {
    		data = new ArrayList();
    		data.add(0.8660254037844386);
    		data.add(0.8660254037844386);
    		data.add(0.8660254037844386);
    		data.add(0.8660254037844386);
    		data.add(0.8660254037844386);
    		data.add(0.8660254037844386);
    		data.add(0.8660254037844386);
    		data.add(0.8660254037844386);
    		data.add(0.8660254037844386);
    		data.add(0.7071067811865476);
    		data.add(0.8660254037844386);
    		data.add(0.5);
    		data.add(0.7071067811865476);
    		data.add(0.5);
    		data.add(0.5);
    		data.add(0.7071067811865476);
    		data.add(0.5);
    		data.add(0.5);
    		data.add(0.7071067811865476);
    		data.add(0.5);
    		super.paintComponent(g);
    		Graphics2D g2 = (Graphics2D) g;
    		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
    				RenderingHints.VALUE_ANTIALIAS_ON);
    		int w = getWidth();
    		int h = getHeight();
    		// Draw ordinate.
    		g2.draw(new Line2D.Double(PAD, PAD, PAD, h - PAD));
    		// Draw abcissa.
    		g2.draw(new Line2D.Double(PAD, h - PAD, w - PAD, h - PAD));
    		// Draw labels.
    		Font font = g2.getFont();
    		FontRenderContext frc = g2.getFontRenderContext();
    		LineMetrics lm = font.getLineMetrics("0", frc);
    		float sh = lm.getAscent() + lm.getDescent();
    		// Ordinate label.
    		String s = "data";
    		float sy = PAD + ((h - 2 * PAD) - s.length() * sh) / 2 + lm.getAscent();
    		for (int i = 0; i < s.length(); i++) {
    			String letter = String.valueOf(s.charAt(i));
    			float sw = (float) font.getStringBounds(letter, frc).getWidth();
    			float sx = (PAD - sw) / 2;
    			g2.drawString(letter, sx, sy);
    			sy += sh;
    		}
    		// Abcissa label.
    		s = "x axis";
    		sy = h - PAD + (PAD - sh) / 2 + lm.getAscent();
    		float sw = (float) font.getStringBounds(s, frc).getWidth();
    		float sx = (w - sw) / 2;
    		g2.drawString(s, sx, sy);
    		// Draw lines.
    		double xInc = (double) (w - 2 * PAD) / (data.size() - 1);
    		double scale = (double) (h - 2 * PAD) / getMax();
    		System.out.println("Graphing Data 1");
    		System.out.println("XINC: " + xInc);
    		System.out.println("scale: " + scale + "\n");
    		g2.setPaint(Color.blue.darker());
    		for (int i = 0; i < data.size() - 1; i++) {
    			double x1 = PAD + i * xInc;
    			System.out.println("x1: " + x1);
    			double y1 = h - PAD - scale * data.get(i);
    			System.out.println("y1: " + y1);
    			double x2 = PAD + (i + 1) * xInc;
    			System.out.println("x2: " + x2);
    			double y2 = h - PAD - scale * data.get(i);
    			System.out.println("y2: " + y2 + "\n");
    			g2.draw(new Line2D.Double(x1, y1, x2, y2));
    		}
    		// Mark data points.
    		g2.setPaint(Color.red);
    		for (int i = 0; i < data.size(); i++) {
    			double x = PAD + i * xInc;
    			double y = h - PAD - scale * data.get(i);
    			g2.fill(new Ellipse2D.Double(x - 2, y - 2, 4, 4));
    		}
    	}
    
    	private int getMax() {
    		int max = -Integer.MAX_VALUE;
    		for (int i = 0; i < data.size(); i++) {
    			if (data.get(i) > max)
    				max = 2;
    		}
    		return max;
    	}
    
    	public static void main(String[] args) {
    		JFrame f = new JFrame();
    		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		f.add(new GraphingData());
    		f.setSize(400, 400);
    		f.setLocation(200, 200);
    		f.setVisible(true);
    	}
    }

    Java Code:
    import java.awt.*;
    import java.awt.font.*;
    import java.awt.geom.*;
    import javax.swing.*;
    
    public class GraphingData2 extends JPanel {
    	double[] data = { 0.8660254037844386, 0.8660254037844386,
    			0.8660254037844386, 0.8660254037844386, 0.8660254037844386,
    			0.8660254037844386, 0.8660254037844386, 0.8660254037844386,
    			0.8660254037844386, 0.7071067811865476, 0.8660254037844386, 0.5,
    			0.7071067811865476, 0.5, 0.5, 0.7071067811865476, 0.5, 0.5,
    			0.7071067811865476, 0.5 };
    	final int PAD = 20;
    
    	protected void paintComponent(Graphics g) {
    		super.paintComponent(g);
    		Graphics2D g2 = (Graphics2D) g;
    		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
    				RenderingHints.VALUE_ANTIALIAS_ON);
    		int w = getWidth();
    		int h = getHeight();
    		// Draw ordinate.
    		g2.draw(new Line2D.Double(PAD, PAD, PAD, h - PAD));
    		// Draw abcissa.
    		g2.draw(new Line2D.Double(PAD, h - PAD, w - PAD, h - PAD));
    		// Draw labels.
    		Font font = g2.getFont();
    		FontRenderContext frc = g2.getFontRenderContext();
    		LineMetrics lm = font.getLineMetrics("0", frc);
    		float sh = lm.getAscent() + lm.getDescent();
    		// Ordinate label.
    		String s = "data";
    		float sy = PAD + ((h - 2 * PAD) - s.length() * sh) / 2 + lm.getAscent();
    		for (int i = 0; i < s.length(); i++) {
    			String letter = String.valueOf(s.charAt(i));
    			float sw = (float) font.getStringBounds(letter, frc).getWidth();
    			float sx = (PAD - sw) / 2;
    			g2.drawString(letter, sx, sy);
    			sy += sh;
    		}
    		// Abcissa label.
    		s = "x axis";
    		sy = h - PAD + (PAD - sh) / 2 + lm.getAscent();
    		float sw = (float) font.getStringBounds(s, frc).getWidth();
    		float sx = (w - sw) / 2;
    		g2.drawString(s, sx, sy);
    		// Draw lines.
    		double xInc = (double) (w - 2 * PAD) / (data.length - 1);
    		double scale = (double) (h - 2 * PAD) / getMax();
    
    		System.out.println("Graphing Data 2");
    		System.out.println("XINC: " + xInc);
    		System.out.println("scale: " + scale + "\n");
    
    		g2.setPaint(Color.green.darker());
    		for (int i = 0; i < data.length - 1; i++) {
    			double x1 = PAD + i * xInc;
    			System.out.println("X1: " + x1);
    			double y1 = h - PAD - scale * data[i];
    			System.out.println("Y1: " + y1);
    			double x2 = PAD + (i + 1) * xInc;
    			System.out.println("X2: " + x2);
    			double y2 = h - PAD - scale * data[i + 1];
    			System.out.println("Y2: " + y2 + "\n");
    			g2.draw(new Line2D.Double(x1, y1, x2, y2));
    		}
    		// Mark data points.
    		g2.setPaint(Color.red);
    		for (int i = 0; i < data.length; i++) {
    			double x = PAD + i * xInc;
    			double y = h - PAD - scale * data[i];
    			g2.fill(new Ellipse2D.Double(x - 2, y - 2, 4, 4));
    		}
    	}
    
    	private int getMax() {
    		int max = -Integer.MAX_VALUE;
    		for (int i = 0; i < data.length; i++) {
    			if (data[i] > max)
    				max = 2;
    		}
    		return max;
    	}
    
    	public static void main(String[] args) {
    		JFrame f = new JFrame();
    		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		f.add(new GraphingData2());
    		f.setSize(400, 400);
    		f.setLocation(200, 200);
    		f.setVisible(true);
    	}
    }
    The key difference between the both of them is that the first one uses ArrayList, whereas the second one uses a fixed sized Array.

    Both codes are somewhat similar, except their output is different.

    For the ArrayList Code, the problem is that it doesn't draw the line properly, despite having proper x1,y1,x2,y2 values, whereas the Array Code plots the graph perfectly fine. I know so, because I did a println command and compared the output of the drawline for both of them, and they are the same.

    May I know what went wrong?


    ***EDIT: NEVERMIND. FOUND OUT WHAT'S WRONG. was pure careless. MAN! Wasted an hour.

    Mod can either close/delete. Thank you! :)
    Last edited by rhexis; 02-17-2012 at 05:44 PM.

Similar Threads

  1. Replies: 2
    Last Post: 12-05-2011, 02:00 AM
  2. Drawing Lines using looping statements
    By trebor11584 in forum New To Java
    Replies: 13
    Last Post: 07-06-2011, 12:00 PM
  3. Replies: 0
    Last Post: 04-20-2010, 08:28 AM
  4. Demonstration of drawing lines in SWT
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 06-28-2008, 09:27 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
  •