0

NB: I have never used swing before, neither graphics 2D, and I don't program very much...

What I am trying to do is to make a program which takes an array/vector as input. This array, where each index 0,1,2 etc holds either zero or one (int) - which represents "no activity" or "activity" in minute 0,1,2 etc...

I want the program to draw a discontinuous straight horizontal line - representing "activity" vs "no activity" as a function of time - based on the array that was taken as input. And this should pop up in a panel when I run the code.. The Idea is to show activity/no activity as a function of time, so the line would preferably be shown in a chart ( x-axis & y-axis )... And there will be several of these discontinuous lines above each other - for comparison of different cases.

I have tried for a while to look at examples using swing and graphics 2D, but as I have very limited amount of time - I could really need some help ..

Any code that:

  • creates a panel, frame etc - where I "easily" can see where I can insert my graph: that is a panel which is ready to display the graph I will make
  • draw a graph of discontinuous horizontal lines based on an array as described above

...is immensely appreciated :)

added from comment:

Sorry - did not finish my answer :) I could sure try to learn how to use all the different things in Swing frames, panels etc.. But at the moment my main goal is to finish my assignment for school - which is the visualization of data itself - and they do not really care how you get there, the most important thing is that it visualizes something useful... So I thought that I could decrease the time I had to spend on this if I got some code which could get me started - and not have to learn how it all works first.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
user1091430
  • 11
  • 1
  • 2
  • 1
    Creating a JFrame with a JPanel is really the first thing you should learn in Swing, have you tried? can you show the code you have tried with? – Jonas Dec 10 '11 at 17:23
  • Well, my first approach was to try to find some code which did somewhat what I was looking for - since I am not really trying to learn either Swing nor graphics at the moment.. So what I found was this page http://www.java2s.com/Code/Java/2D-Graphics-GUI/Demobarchartandpiechart.htm... – user1091430 Dec 10 '11 at 17:35
  • Sorry - did not finish my answer :) I could sure try to learn how to use all the different things in Swing frames, panels etc.. But at the moment my main goal is to finish my assignment for school - which is the visualization of data itself - and they do not really care how you get there, the most important thing is that it visualizes something useful... So I thought that I could decrease the time I had to spend on this if I got some code which could get me started - and not have to learn how it all works first... – user1091430 Dec 10 '11 at 17:39
  • 3
    We don't work for you. But we can help you if you have any specific questions. – Jonas Dec 10 '11 at 17:41

2 Answers2

4

No need to learn Graphics2D, just go for JFreeChart. Here is a simple tutorial to get you started (A minimum of Java programming knowledge is required though)

Community
  • 1
  • 1
GETah
  • 20,922
  • 7
  • 61
  • 103
  • There's a related time series example [here](http://stackoverflow.com/a/5522583/230513). – trashgod Dec 10 '11 at 18:34
  • Thank you! I tried to play around with the JfreeChart and jChart2D.. But I did not find any example with a discontinuous graphs.. – user1091430 Dec 10 '11 at 19:18
2

This is is an example, I guess it will help

import java.awt.*;
import javax.swing.*;

public class ActivityGraph extends JFrame {

int[] active = {0,1,1,0,0,0,1,0,1,0,1,0,1,1,1,1,0,0,1,0,1,1};
int length = 25, //basic length in pixels for drawing the lines
        offset = 50; //so the lines aren't sticked at the border

private ActivityGraph(String name, int x, int y, int width, int height) {
    super(name);
    setBounds(x, y, width, height);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    JPanel panel = new GraphPanel();
    //panel.setBounds(0, 0, 800, 400); not nessesary
    add(panel);
}

public static void main(String[] args) {
    new ActivityGraph("Activity Graph", 60, 60, 800, 400).setVisible(true);
}

private class GraphPanel extends JPanel {

    public void paint(Graphics g) {
        g.setColor(Color.white);
        g.fillRect(0, 0, 800, 400);
        //setting background (method setBackground() doesn't want to work for me)
        g.setColor(Color.black);
        for(int i = 0; i<active.length; i++) {
            if(active[i]==0) {
                g.drawLine(offset + i*length, offset + length, offset + i*length + length, offset + length);
            }
            else {
                g.drawLine(offset + i*length, offset, offset + i*length + length, offset);
            }
            /*
             * draw line from XY point to another XY point
             * notice that X = Y = 0 point is in left top corner
             * so higher Y values will mean "downer" points acctualy
             */
        }
    }

}

}

If you want, I can send you a graph drawer for math functions (like sinus, power, ...)

Diogo Cruz
  • 66
  • 12
kajacx
  • 12,361
  • 5
  • 43
  • 70