user1091430
user1091430

Reputation: 11

How to make Panel, Frame etc in Java to display graph on?

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:

...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.

Upvotes: 0

Views: 6171

Answers (2)

kajacx
kajacx

Reputation: 12949

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, ...)

Upvotes: 2

GETah
GETah

Reputation: 21449

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)

Upvotes: 4

Related Questions