import java.awt.*;
import javax.swing.*;
/*
* A sample implementation of a panel that draws the chart. Extend this
* class with drawing logic or use your own layouts and components.
*/
public class ChartPanel extends JPanel {
public int[][] dataArray;
public int dim2; //# of data points
public int w;
public void paintComponent(Graphics gl) {
Graphics2D g = (Graphics2D) gl;
//the Background part - Just draw once perhaps
g.setColor(new Color(222, 222, 222));
g.fillRect(0, 0, this.getWidth(), this.getHeight());
g.setColor(new Color(250, 0, 0));
g.drawLine(10, 10, 10, 310); //max y-axis of 300
g.drawLine(10, 310, dim2*50+10, 310); //variable x-axis
g.drawString("100ms", 2, 210);
g.drawString("200ms", 2, 110);
g.drawString("300ms", 2, 10);
//the update part
if(w<dim2 && dataArray!=null){ //still skeleton/ not finished
for(int j=0;j<dataArray.length;j++) {
for(int k=dataArray[j].length-w; k<dataArray[j].length; k++){
g.drawLine(50*(dim2-w)+10, dataArray[j][k-1], 50*(dim2-w)+100, dataArray[j][k]);
}
}
}
}
//Failed attemps
// public void paintComponent(Graphics gl) {
// Graphics2D g = (Graphics2D) gl;
// super.paintComponent(g);
// g.setColor(new Color(222, 222, 222));
// g.fillRect(0, 0, this.getWidth(), this.getHeight());
// g.setColor(new Color(250, 0, 0));
// g.drawLine(10, 10, 10, 310);
// g.drawLine(10, 310, dim2*50+10, 310);
// g.drawString("100ms", 2, 210);
// g.drawString("200ms", 2, 110);
// g.drawString("300ms", 2, 10);
//
// if(w<dim2 && dataArray!=null){
// for(int j=0;j<dataArray.length;j++) {
// for(int k=dataArray[j].length-w; k<dataArray[j].length; k++){
// g.drawLine(50*(dim2-w)+10, dataArray[j][k-1], 50*(dim2-w)+100, dataArray[j][k]);
// }
// }
// }
// }
// public void paint(Graphics gl) {
// Graphics2D g = (Graphics2D) gl;
// g.setColor(new Color(222, 222, 222));
// g.fillRect(0, 0, this.getWidth(), this.getHeight());
// g.setColor(new Color(250, 0, 0));
// g.drawLine(10, 10, 10, 310);
// g.drawLine(10, 310, dim2*50+10, 310);
// g.drawString("100ms", 2, 210);
// g.drawString("200ms", 2, 110);
// g.drawString("300ms", 2, 10);
// }
// public void update(Graphics gl) {
// Graphics2D g = (Graphics2D) gl;
// if(w<dim2){
// for(int j=0;j<dataArray.length;j++) {
// for(int k=dataArray[j].length-w; k<dataArray[j].length; k++){
// g.drawLine(50*(dim2-w)+10, dataArray[j][k-1], 50*(dim2-w)+100, dataArray[j][k]);
// }
// }
// }
// }
}