• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Can't Display Progress Bar

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();

}


}
 
Marshal
Posts: 65034
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Joseph Zhao wrote:. . . I have tried to create another jframe that creates a JProgressBar  . . .

I can't see where you are creating the progress bar, not even by using ctrfl‑F. You are not going to solve anything in the midst of so much code. Your code has problems:
  • 1: Use of public variables
  • 2: Subclassing too many display classes: don't subclass frames, for example.
  • 3: Inconsistent indentation and too many blank lines. Don't double space your code.
  • 4: Methods too long. Your paintComponent() method occupies 150 lines and does calculations which should be done elsewhere.
  • I suggest you follow the examples in the Java™ Tutorials, and you may need to use a worker thread/SwingWorker object to update the data and the progress bar.
     
    Joseph Zhao
    Greenhorn
    Posts: 2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    Campbell Ritchie
    Marshal
    Posts: 65034
    247
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Joseph Zhao wrote:So should I create methods within the vertex jpanel class that does the calculations . . . .

    I think, no. I think you should create a separate class doing the calculations; the panel is only for displaying the resuts.
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!