0

I am creating a JDialog Form in java.

I have created a class, using g.drawline to graph an array of numbers into a linegraph. I can run this class seperately just fine, but I am wondering how I can place this class/frame into a jdialog form.

Here is the graph ( reason for using double at first, is it will be used to track dollars after)

import java.awt.Graphics;
import java.awt.Color;
import javax.swing.JFrame;

public class AccountGraph extends JFrame {

private double[] arrayDollar = {23000, 1400, 94506, 23450, 23656, 23767, 700, 24000, 8456, 23450, 23656, 2367};
private double[] arrayPx = new double[12];
private double max;
private double min;
private double range;

public AccountGraph() {
    dollarToPx(this.arrayDollar, this.arrayPx);
    setTitle("Graph");
    setSize(340, 340);
    setVisible(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
}

public void dollarToPx(double[] arrayDollar, double[] arrayPx) {

    this.max = arrayDollar[0];          //find max of array

    for (int i = 1; i < arrayDollar.length; i++) {
        if (arrayDollar[i] > this.max) {
            this.max = arrayDollar[i];
        }

    }

    this.min = arrayDollar[0];        //find min of array
    for (int i = 1; i < arrayDollar.length; i++) {
        if (arrayDollar[i] < this.min) {
            this.min = arrayDollar[i];
        }

    }

    this.range = this.max - this.min;   //range of data (max-min)

    double scale = 260/this.range;   //scale range to graph px - 260 px = amount of px for graph

    for (int i = 0; i < arrayDollar.length; i++) {
        double px = (300) - (scale) * (arrayDollar[i] - this.min); //equation for y px on graph 
        System.out.println(px);
        arrayPx[i] = px;

    }

}

public void paint(Graphics g) {

    g.setColor(Color.BLACK);

    g.drawLine(40, 40, 40, 300);
    g.drawLine(40, 300, 277, 300);

    g.setColor(Color.LIGHT_GRAY);
    g.drawLine(62, 40, 62, 300);
    g.drawLine(83, 40, 83, 300);
    g.drawLine(105, 40, 105, 300);
    g.drawLine(126, 40, 126, 300);
    g.drawLine(148, 40, 148, 300);
    g.drawLine(170, 40, 170, 300);
    g.drawLine(191, 40, 191, 300);
    g.drawLine(213, 40, 213, 300);
    g.drawLine(234, 40, 234, 300);
    g.drawLine(255, 40, 255, 300);
    g.drawLine(277, 40, 277, 300);

    int convAr[] = new int[12];  // double cannot be input into g.drawLine method

    for (int i = 0; i < convAr.length; i++) {
        convAr[i] = (int) arrayPx[i];

    }

    g.setColor(Color.green);

    g.drawLine(40, convAr[0], 62, convAr[1]); //would have done for loop, but x values did not increase at -->exact<-- linear rate
    g.drawLine(62, convAr[1], 83, convAr[2]);
    g.drawLine(83, convAr[2], 105, convAr[3]);
    g.drawLine(105, convAr[3], 126, convAr[4]);
    g.drawLine(126, convAr[4], 148, convAr[5]);
    g.drawLine(148, convAr[5], 170, convAr[6]);
    g.drawLine(170, convAr[6], 191, convAr[7]);
    g.drawLine(191, convAr[7], 213, convAr[8]);
    g.drawLine(213, convAr[8], 234, convAr[9]);
    g.drawLine(234, convAr[9], 255, convAr[10]);
    g.drawLine(255, convAr[10], 277, convAr[11]);

}

public static void main(String[] args) {
    AccountGraph ag = new AccountGraph();

}

}
user3424451
  • 89
  • 1
  • 9
  • Consider providing a [runnable example](https://stackoverflow.com/help/mcve) which demonstrates your problem. This will result in less confusion and better responses. This will provide us with clues as the structure of code and any potential issues you might have...As a side note, you can add windows to windows... – MadProgrammer Aug 01 '14 at 00:57

1 Answers1

1

You have two main problems...

Firstly

You are extending your class from JFrame. This might seem like a reasonable place to start, but it will lock you into never been able to use the component for anything else other then a frame.

Secondly

You are painting directly to a top level container, take a look at Why not to draw directly inside JFrame for reasons why this a bad idea...

On top of this, you are not calling super.paint, which can introduce the potential for painting artifacts and other problems. paint is a very important method, be very, very careful with it's use.

A Solution

Start by moving the code in the frame to a class that extends from something like JPanel. Instead of overriding paint, use protected void paintComponent(Graphics) instead and make sure you call super.paintComponent before you do any other custom painting.

Take a look at Performing Custom Painting and Painting in AWT and Swing for more information about painting in Swing

Consider overriding getPreferredSize to provide sizing hints to the layout managers, so you component isn't laid out at a size of 0x0...

Don't assume or use "magic" numbers, instead of 40, 40 for example, you should be relying on known states, such as getWidth and getHeight, which will tell you the current size of the component when you paint it. This is not absolutely required and you can "fix" the size of your rendering, but it will make your component much more flexible if you do ;)

Once you have this JPanel, you can add it either to your JFrame or JDialog...

Side Notes

  • You should extending from windows based classes like JFrame and JDialog, not only does it look you into a single use, you are providing little "extended" functionality to the classes. Instead, develop your UI against containers like JPanel and simply add them to instance of these window based classes as needed.
  • It might be beyond the scope of the question, but you might consider having a look at JFreeChart
Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • I appreciate your help, time and your detailed comment. You made it very easy to understand. Thanks for helping a noob – user3424451 Aug 01 '14 at 01:10
  • Thanks for the JFreeChart suggestion.Will definitely add for future use. Here, I thought about the: double px = (300) - (scale) * (arrayDollar[i] - this.min); equation in my head and just decided to complete it. – user3424451 Aug 01 '14 at 01:13