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();
}
}