Results 1 to 4 of 4
  1. #1
    Fautoty is offline Member
    Join Date
    Apr 2011
    Posts
    2
    Rep Power
    0

    Default Graphing Temperatures Predictions

    I have a project that I am having trouble on.

    The initial display will show the predictions made 1 day in advance. The buttons allow you to change the graph to show predictions made 2, 3, 4, ... up to 9 days in advance. Use only 2 buttons, one to increase the number of days in advance, and one to decrease it. You should not be able to move out of the range from 1 to 9.

    It will need a Key class and a Graph class. The constructor for the Graph class might look like this:
    public Graph(int[] y, Color c);
    where the array is an array of temperature values. The draw method of Graph takes 5 parameters, the Graphics, the height of the panel it is drawing in, the x and y scale (number of pixels per unit) and the x and y offsets of the origin. These are given relative to the bottom left corner of the panel. Remember that for drawing, y values are 0 at the top of the panel and increase as you go down.

    Your Graph class should have method:
    private void drawDot(Graphics g, int x, int y);
    that draws a dot centered at (x,y). Implement this by drawing a small filled circle. Remember fillOval is given the position of the upper left corner of the bounding box, not the center of the oval. The drawDot method should take this into account.

    Here is what the data will look like with lows first then high. The first day is the actual temperature while the rest are predictions from that day.

    Mar 19 64 82 64 76 65 81 62 85 58 81 61 81 63 81 62 79 64 85
    Mar 20 64 82 65 81 65 83 58 83 63 83 65 82 59 85 64 82 56 85
    Mar 21 64 82 65 84 60 83 63 83 65 84 62 85 51 82 59 77 62 75
    Mar 22 63 85 62 83 63 83 65 84 62 85 64 82 51 80 49 75 56 66
    Mar 23 59 85 63 80 65 81 62 85 64 82 51 80 49 75 61 70 49 74
    Mar 24 65 85 64 91 64 92 49 83 49 77 59 78 56 82 52 82 53 83
    Mar 25 62 92 62 89 65 83 64 84 60 81 55 78 53 84 55 84 55 82
    Mar 26 60 81 68 85 61 85 61 80 49 76 59 84 61 88 58 84 59 85
    Mar 27 65 85 58 86 60 78 50 74 52 83 63 87 62 88 59 86 59 86
    Mar 28 58 85 57 76 57 78 58 85 62 87 65 90 60 89 60 87 57 82

    I'm mostly having problems with the first part. I'm having trouble getting it to fit into the graph the right way. Here is the code I have so far.

    Java Code:
    public class Graph {
    
    	private int[] temperatures;
    	private Color color;
    	private final int DOT_RADIUS = 5;
    
    	public Graph(int[] y, Color c) {
    		this.temperatures = y;
    		this.color = c;
    	}
    
    	private void drawDot(Graphics g, int x, int y) {
    		g.fillOval(x - DOT_RADIUS, y - DOT_RADIUS, DOT_RADIUS, DOT_RADIUS);
    
    	}
    
    	public void draw(Graphics g, int height, int xScale, int yScale,
    			int xOffset, int yOffset) {
    		for (int i = 0; i < temperatures.length; i++) {
    			drawDot(g, xOffset + i * xScale, yOffset + temperatures[i] * yScale);
    		}
    
    	}
    
    }
    The GraphPanel is completed and probably won't need anything changed.

    Java Code:
    public class GraphPanel extends JPanel implements ActionListener {
    
    	private int xOffset = 50;
    	private int yOffset = 100;
    	private int labelOffset = 25;
    
    	private int xScale;
    	private int yScale;
    
    	private ArrayList<Graph> graphs;
    	private ArrayList<Key> keys;
    	private String[] yLabels = null;
    
    	public GraphPanel(int xScale, int yScale, int maxX, int maxY) {
    		this.xScale = xScale;
    		this.yScale = yScale;
    		setPreferredSize(new Dimension(xScale * maxX + xOffset, yScale * maxY
    				+ yOffset));
    		graphs = new ArrayList<Graph>();
    		keys = new ArrayList<Key>();
    	}
    
    	public void addGraph(Graph graph) {
    		graphs.add(graph);
    		repaint();
    	}
    
    	public void addKey(int x, int y, String label, Color c) {
    		keys.add(new Key(x, y, label, c));
    		repaint();
    	}
    
    	public void setYLabels(String[] labels) {
    		yLabels = labels;
    		repaint();
    	}
    
    	public void paintComponent(Graphics g) {
    		Dimension size = getSize();
    		g.setColor(Color.white);
    		g.fillRect(0, 0, size.width, size.height);
    		g.setColor(Color.black);
    		g.drawLine(0, size.height - yOffset, size.width, size.height - yOffset);
    		g.drawLine(xOffset, 0, xOffset, size.height);
    		drawHorizontalGrid(g, size.width, size.height);
    		g.setColor(Color.black);
    		if (yLabels != null)
    			for (int i = 0; i < yLabels.length; i++)
    				if (yLabels[i] != null)
    					g.drawString(yLabels[i], xOffset - labelOffset, size.height
    							- (i * yScale + yOffset));
    		for (int i = 0; i < graphs.size(); i++)
    			graphs.get(i).draw(g, size.height, xScale, yScale, xOffset, yOffset);
    		for (int i = 0; i < keys.size(); i++)
    			drawKey(g, keys.get(i), size.height);
    	}
    
    	private void drawHorizontalGrid(Graphics g, int w, int h) {
    		int deltaY = 5;
    		int a = 0;
    		String text = new String("");
    		g.setColor(Color.lightGray);
    		int gridY = h - (yOffset + deltaY * yScale);
    		while (gridY > 0) {
    			g.drawLine(xOffset, gridY, w, gridY);
    			g.drawString(text + a, 15, gridY + 20);
    			gridY -= deltaY * yScale;
    			a += deltaY;
    		}
    	}
    
    	private void drawKey(Graphics g, Key key, int height) {
    		int labelPad = 10;
    		int x = key.getX() + xOffset;
    		int y = height - yOffset + key.getY();
    		int length = key.getLength();
    		g.setColor(key.getColor());
    		g.drawLine(x, y, x + length, y);
    		g.setColor(Color.black);
    		g.drawString(key.getLabel(), x + length + labelPad, y);
    	}
    }
    Here is the main class.

    Java Code:
    	JFrame window = new JFrame("Temperature Graph");
    		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		JPanel mainPanel = new JPanel();
    		mainPanel.setLayout(new BorderLayout());
    		JLabel label = new JLabel("Graph");
    		mainPanel.add(label, BorderLayout.NORTH);
    		GraphPanel graph = new GraphPanel(5, 5, 200, 100);
    		Graph graphHighs = new Graph(actualHighs, Color.GREEN);
    		Graph graphLows = new Graph(actualHighs, Color.BLUE);
    		graph.addKey(50, 50, "Actual Highs", Color.BLUE);
    		graph.addKey(150, 50, "Actual Lows", Color.GRAY);
    		graph.addKey(250, 50, "Predicted Highs", Color.RED);
    		graph.addKey(400, 50, "Predicted Lows", Color.CYAN);
    		graph.addGraph(graphHighs);
    		graph.addGraph(graphLows);
    		mainPanel.add(graph, BorderLayout.CENTER);
    		mainPanel.add(new ButtonPanel(graph), BorderLayout.SOUTH);
    		window.add(mainPanel);
    		window.pack();
    		window.setVisible(true);
    I know this is a lot but thank you for any help you can provide. If something is unclear please ask and I will try to fill any missing details.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    What exactly is your question? Are you getting errors, if so copy and paste the exact errors.

  3. #3
    Fautoty is offline Member
    Join Date
    Apr 2011
    Posts
    2
    Rep Power
    0

    Default

    Sorry no errors, the problem is the graph points are all bunched on the bottom left. I haven't been able to put them to match up on the graph. So the xScale and yScale aren't doing what I need them to do. I don't know where to use the height in this part

    Java Code:
    		for (int i = 0; i < graphs.size(); i++)
    			graphs.get(i).draw(g, size.height, xScale, yScale, xOffset, yOffset);
    This part is where the code isn't right:

    Java Code:
    	public void draw(Graphics g, int height, int xScale, int yScale,
    			int xOffset, int yOffset) {
    		for (int i = 0; i < temperatures.length; i++) {
    			drawDot(g, xOffset + i * xScale, yOffset + temperatures[i] * yScale);
    		}
    So basically I need help on the graphing part because I can't put the data in the right spots; I can't do the part of changing the number of days it is predicted from.
    Last edited by Fautoty; 04-22-2011 at 03:24 AM. Reason: Makeing it more clear

  4. #4
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    From my experience, when graphing data, it's simplest to scale the data to the range 0-1 first, and then scale from there to the graph size. That way, it plots correctly regardless of the dimensions of the drawing region. It's also straightforward to crop and zoom when everything is normalized first.

    For example, suppose your temperature data was stored in a List called 'temperature', and you wanted your y-scale to be 0 to 100, which are stored in "yMin" and "yMax" (same for x)

    Java Code:
    public void paintComponent(Graphics g) {
      super(g); ///this clears the canvas for you ;)
    
      double yScale = 1.0/(yMax - yMin);
      double xScale = 1.0/(xMax - xMin);
    
      double[] yVals = new double[temperature.size()];
      double[] xVals = new double[temperature.size()];
    
      // the '1-' on y is here to flip the y reference to the bottom instead of the top,
      // but you could do that later if preferred
      // in this case, x just goes from 0 to (length-1), but more generally, it could be anything
    
      for(int i=0; i<yVals.length; i++) {
         yVals[i] = 1.0 - (temperature.get(i) - yMin)*yScale;
         xVals[i] = (i - xMin)*xScale;
      }
    
      Dimension size = getSize();
    
      for(int i=0; i<xVals.length; i++) {
         int pixelX = (int) (size.width*xVals[i] + 0.5);  // 0.5 to round, maybe you don't care
         int pixelY = (int) (size.height*yVals[i] + 0.5);
    
    ....plot whatever you're goign to plot using the pixel locations pixelX and pixelY.
      }
    
    
    }
    [code]

Similar Threads

  1. monthly temperatures
    By droidus in forum New To Java
    Replies: 23
    Last Post: 04-05-2011, 02:21 AM
  2. Graphing GUI
    By spyroth in forum NetBeans
    Replies: 3
    Last Post: 02-12-2011, 02:52 AM
  3. My code for converting temperatures...
    By king4ever in forum New To Java
    Replies: 8
    Last Post: 01-18-2011, 04:27 AM
  4. Graphing in java
    By collin389 in forum New To Java
    Replies: 2
    Last Post: 01-18-2010, 01:54 AM
  5. Help with graphing problem
    By adlb1300 in forum New To Java
    Replies: 2
    Last Post: 11-26-2007, 02:50 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
  •