Joseph Zhao

Greenhorn
+ Follow
since May 25, 2019
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Joseph Zhao

So should I create methods within the vertex jpanel class that does the calculations and the paint method calls them? I need to create a graphing software and the paint method creates the graph. Thank you for the help.
4 years ago
Hi, i am trying to display a progress bar in my jframe application while another loop iterates through a method that is doing calculations. There is a main jframe called graph that has a Jpanel that intializes the calculation method and i want to make a progress bar show up so that while the jpanel is calling the method in another class, the progress bar is loading. I have tried to create another jframe that creates a JProgressBar and putting it in the jpanel contructor, but this jframe is blank. Also the Jpanel method is calling a paint method that paints over the main jframe a number graph. How should i add the progress bar? thank you for the help.

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.Random;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;

//main jframe
class graph extends JFrame {

   public graph() {
   
       super("Scatterplot");
       
       double[][]p = new double[][]{{2,1},{3,10},{4,99},{3.6,34}};
     
      vertex v = new vertex(p,6, false, false, true);
       
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
      getContentPane().add(v);
     
      pack();
   
    setLocationRelativeTo(null);
   
    setVisible(true);
   
   
   
   }



}



import java.awt.*;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;

import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.util.List;

import javax.swing.JProgressBar;
import javax.swing.SwingWorker;
import javax.swing.border.EmptyBorder;

//jpanel that calls method with a loop
class vertex extends JPanel{

//data points of all the transactions (date,amount)
public double[][] points;

public JLabel[] yInts = new JLabel[12];

public JLabel[] ints;

public double max = 0.0;

public double axis = 0.0;

public double size = 800.0;

public double yAxis = size/10.0;

public predictionAL lobf;

public regression n;

public boolean linear;


public vertex(double[][] p, double months, boolean linear, boolean expo, boolean quad){

ints = new JLabel[(int) months];

this.points = new double[p.length][p[0].length];

this.linear = linear;

for(int x=0;x<p.length; x++){

for(int y=0;y<p[0].length;y++){

points[x][y] = p[x][y];

}

}


for(int x=0;x<p.length; x++){

if(Double.compare(p[x][1], this.max)>0){

this.max = p[x][1];

}
}

//max is the nearest tens
max = (10.0*Math.ceil(max/10.0));

axis = size/months;

setPreferredSize(new Dimension((int)(size)+20,(int)(size)+20));

setLayout(null);

if(this.linear){
lobf = new predictionAL(p);

}else{

n = new regression(expo, quad, p);


}

}



public void paintComponent(Graphics g) {
   
   super.paintComponent(g);

   Graphics2D g2d = (Graphics2D) g;

// (0,0) = (0,250) on frame.

//makes the points denser than the axis

//label the marks on the axis, where 10 is then 20 ...

g2d.setStroke(new BasicStroke(1));

//graph lines
for(int x=0; x <7; x++){

g2d.setColor(Color.red);
//y-axis lines , x=
g2d.draw(new Line2D.Double(x*axis,0,x*axis,size));

}

for(int x=0; x <11; x++){

g2d.setColor(Color.red);
//x-axis lines, y=
g2d.draw(new Line2D.Double(0, x*yAxis,size,x*yAxis));

}

g2d.setStroke(new BasicStroke(3));


g2d.setColor(Color.black);
for(int t=0; t <ints.length; t++){

ints[t] = new JLabel();

add(ints[t]);

//x axis
ints[t].setSize(200,13);

ints[t].setLocation((int)(t*axis), (int)(size));

if(t!=0){
ints[t].setText(Integer.toString(ints.length-t));

}

ints[t].setFont(new Font("Comic Sans MS", Font.PLAIN, 13));


}

for(int t=0; t <11; t++){
yInts[t] = new JLabel();

add(yInts[t]);

yInts[t].setSize(200,13);

yInts[t].setLocation(0,(int) (yAxis*(10-t)));

if(t!=0){

yInts[t].setText(Integer.toString((int)(this.max/10.0)*t));
}else{
yInts[t].setText("(6,0)");

}


yInts[t].setFont(new Font("Comic Sans MS", Font.PLAIN, 13));

}
//plot points
g2d.setStroke(new BasicStroke(5));

for(int x=0; x<points.length; x++){

double z = (points[x][0]);

double y = (points[x][1]);

double ratio = y/max;

g2d.draw(new Ellipse2D.Double(axis*(ints.length-z), (size)-(size)*ratio,2,2));
}

g2d.setColor(Color.BLACK);

g2d.setStroke(new BasicStroke(2));

//time axis, x- axis
g2d.draw(new Line2D.Double(0,(size),size,(size)));

//amount axis, y -axis
g2d.draw(new Line2D.Double(0,(size),0,0));

if(this.linear){

double yRatio = (-ints.length*lobf.slope+lobf.yInt);


//draw line of best fit
g2d.draw(new Line2D.Double(0,size-(yRatio*size/max),size - axis*(lobf.yInt/lobf.slope),size));

}else{




n.approximate();

System.out.println(n.estimate.arrayM[0][0]);

int[] xPoints = {5, 10 , 15, 20, 25, 30 ,35 };

int[] yPoints = new int[xPoints.length];

if(n.exponential){

for(int i=0; i<xPoints.length;i++){

yPoints[i] = (int) ((int)this.n.estimate.arrayM[0][0]*Math.exp(this.n.estimate.arrayM[1][0]*xPoints[i])+n.estimate.arrayM[2][0]);

}


}else{


for(int i=0; i<xPoints.length;i++){

yPoints[i] = (int) ((int)this.n.estimate.arrayM[0][0]*xPoints[i]*xPoints[i] + this.n.estimate.arrayM[1][0]*xPoints[i] + this.n.estimate.arrayM[2][0]);

}


}

g2d.drawPolyline(xPoints, yPoints, 3);

}



}

}

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingWorker;
import javax.swing.border.EmptyBorder;

public class regression{
//method with the loop for the calculation
boolean exponential, quadratic;

matrix data, estimate, gradient, residue;

double scale;

public regression(boolean exponential, boolean quadratic, double[][] data) {

this.exponential = exponential;

this.quadratic = quadratic;

this.data = new matrix(data.length,data[0].length);

this.data.arrayM = data;

}

public void approximate(){

this.findDerivative(this.exponential,this.quadratic);

matrix expAve = new matrix(3,1);

double beta1 = 0.9;
               
for(int i=0;i<100000000;i++){

matrix prev = expAve;

expAve.times(beta1);

matrix a = this.estimate.subtract(expAve);

this.computeGradient(a);

this.gradient.times(scale);

prev.times(beta1);

expAve = prev.addition(this.gradient);

this.estimate = this.estimate.subtract(expAve);

this.calcResidue();

}


}
4 years ago