0

I would like to know how to make a simple graph using the method g.drawLine and using loops to update the coordinates, I am really really new at this and have no idea what I need to do or where to start, I do have this simple program that uses *'s instead of a line to create the graph, but what I need to do is change the graph from *'s to a line. Any help would greatly be appreciated

here is what I have:

import java.awt.*;

public class program5 {

    public static void main(String[] args) {
        graph1();
    }

    public static void graph1() {
        DrawingPanel panel =new DrawingPanel(300,500);
        Graphics g = panel.getGraphics();

        double  t=10;

        for (int i=1; i<20; i++) {
            t= t*(.8); 
            double z = t * 50;
            int y= -(int)z + 430;
            int x = 10 * i;
            g.drawString("*",x,y);
        }
    }
}
Shimon Rachlenko
  • 5,469
  • 40
  • 51
  • There's a nice answer here to help you: http://stackoverflow.com/questions/5801734/how-to-draw-lines-in-java – CodeGuy Mar 07 '13 at 16:49
  • What did you try so far? What about calling `g.drawLine()` with the coordinates you already have - plus the previous ones? – Thomas Mar 07 '13 at 16:50
  • `Graphics g = panel.getGraphics();` Whatever you are reading that made you think that was a good idea, abandon. – Andrew Thompson Mar 07 '13 at 16:50
  • I know that I could do this by just plotting the graph points, but I have to use a loop.. so I would like to know how to tell java that I need the coordinates to start at the first point and then go to the second, and then tell it to update the first points with the second points and then move to the 3rd point, etc... until I get to the last one – user2145216 Mar 07 '13 at 16:54
  • yeah I know this isn't the best way to do it, but i'm taking a class... and my instructor really isn't up to par on this but this is how he wants it done. – user2145216 Mar 07 '13 at 16:56
  • when I did g.drawLine(x1,y1,10,38) it did sorta what I wanted it to do... the line started at the top but then made lines off that one point to all the other points.. I need to somehow make a loop that "jumps" from coordinate point to coordinate point. This is really frustrating stuff! – user2145216 Mar 07 '13 at 17:12

1 Answers1

0

Why not use JavaFX to make your chart? Java FX documentation - Line Charts

kevin0xf
  • 53
  • 4
  • Java instructors directions. I know this is a horrible way to do this and i'm new at it... – user2145216 Mar 07 '13 at 17:00
  • The reason it is jumping from the first point is most likely because you didn't update the start point. drawLine(x1, y1, x2, y2) x1,y1 is your start and x2 y2 is the end point. Each time you loop you should set x1, y1 to the previous x2, y2 and then set x2, y2 to the new point.(I can't comment directly on the question so adding it here, sorry). – kevin0xf Mar 07 '13 at 17:49
  • I know mentally how it should be done in theory, but as far as writing the loop and telling it to do this stuff thats where I get lost here is what i have so far. – user2145216 Mar 07 '13 at 18:00
  • import java.awt.*; public class graph { public static void main(String[] args) { DrawingPanel panel =new DrawingPanel(300,500); Graphics g = panel.getGraphics(); int repCount; int startY; int oldY; int oldX; int newX; repCount = 20; startY = 10 *50; for (int i = 1 ; i <= repCount ; i++){ – user2145216 Mar 07 '13 at 18:04
  • oldY = startY * 4/5; oldX = 10 * i; newX = 10 * (i +1); startY = oldY * 4/5; g.drawLine(oldX,oldY,newX,startY); for (int e = 1; e <=2 ; e++){ oldX =newX; newX = 10 *(i+1); oldY = startY * 4/5; g.drawLine(oldX,startY, newX, oldY); } }}} – user2145216 Mar 07 '13 at 18:05
  • Try breaking your problem down. The loop you posted originally just calculates x and y for each point, because it only needs an x and y to draw the asterisk. Use that loop and store the x and y results. Then you can make a loop to actually draw your line which needs 2 points. If you need it to be all in one loop, you can try to merge it after you have it working separately. – kevin0xf Mar 07 '13 at 18:17
  • hmm.. I dont have much time but, how do I start off with saying x1=10 and y1=38 (these are the first two points) and then i'm lost on how to to make the for loop, I just look at other ones that have been made i dont understand them much.... but moving on after that then i just do the g.drawLine(x1,y1,x2,y2);? im really new at this so i kinda have to be spoon fed...lol – user2145216 Mar 07 '13 at 18:23
  • Yes, once you have your variables set the last line in your loop can be g.drawLine(x1, y1, x2, y2). Then as long as your variables are set right every iteration, it will draw the line properly. – kevin0xf Mar 07 '13 at 18:37