Advertisement
kendy

ChartPanel

Oct 19th, 2012
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.76 KB | None | 0 0
  1. import java.awt.*;
  2. import javax.swing.*;
  3.  
  4. /*
  5.  * A sample implementation of a panel that draws the chart. Extend this
  6.  * class with drawing logic or use your own layouts and components.
  7.  */
  8. public class ChartPanel extends JPanel {
  9.     public int[][] dataArray;
  10.     public int dim2; //# of data points
  11.     public int w;
  12.  
  13.     public void paintComponent(Graphics gl) {
  14.         Graphics2D g = (Graphics2D) gl;
  15.        
  16.         //the Background part - Just draw once perhaps
  17.         g.setColor(new Color(222, 222, 222));
  18.         g.fillRect(0, 0, this.getWidth(), this.getHeight());
  19.         g.setColor(new Color(250, 0, 0));
  20.         g.drawLine(10, 10, 10, 310); //max y-axis of 300
  21.         g.drawLine(10, 310, dim2*50+10, 310); //variable x-axis
  22.         g.drawString("100ms", 2, 210);
  23.         g.drawString("200ms", 2, 110);
  24.         g.drawString("300ms", 2, 10);
  25.        
  26.         //the update part
  27.         if(w<dim2 && dataArray!=null){ //still skeleton/ not finished
  28.             for(int j=0;j<dataArray.length;j++) {
  29.                 for(int k=dataArray[j].length-w; k<dataArray[j].length; k++){
  30.                     g.drawLine(50*(dim2-w)+10, dataArray[j][k-1], 50*(dim2-w)+100, dataArray[j][k]);
  31.                 }
  32.             }
  33.         }
  34.     }
  35.  
  36. //Failed attemps    
  37. //    public void paintComponent(Graphics gl) {
  38. //      Graphics2D g = (Graphics2D) gl;
  39. //      super.paintComponent(g);
  40. //        g.setColor(new Color(222, 222, 222));
  41. //        g.fillRect(0, 0, this.getWidth(), this.getHeight());
  42. //        g.setColor(new Color(250, 0, 0));
  43. //        g.drawLine(10, 10, 10, 310);
  44. //        g.drawLine(10, 310, dim2*50+10, 310);
  45. //        g.drawString("100ms", 2, 210);
  46. //        g.drawString("200ms", 2, 110);
  47. //        g.drawString("300ms", 2, 10);
  48. //        
  49. //        if(w<dim2 && dataArray!=null){
  50. //          for(int j=0;j<dataArray.length;j++) {
  51. //              for(int k=dataArray[j].length-w; k<dataArray[j].length; k++){
  52. //                  g.drawLine(50*(dim2-w)+10, dataArray[j][k-1], 50*(dim2-w)+100, dataArray[j][k]);
  53. //              }
  54. //          }
  55. //      }
  56. //    }
  57.  
  58. //    public void paint(Graphics gl) {
  59. //      Graphics2D g = (Graphics2D) gl;
  60. //      g.setColor(new Color(222, 222, 222));
  61. //      g.fillRect(0, 0, this.getWidth(), this.getHeight());
  62. //      g.setColor(new Color(250, 0, 0));
  63. //      g.drawLine(10, 10, 10, 310);
  64. //      g.drawLine(10, 310, dim2*50+10, 310);
  65. //      g.drawString("100ms", 2, 210);
  66. //      g.drawString("200ms", 2, 110);
  67. //      g.drawString("300ms", 2, 10);
  68. //    }
  69. //    public void update(Graphics gl) {
  70. //      Graphics2D g = (Graphics2D) gl;
  71. //          if(w<dim2){
  72. //          for(int j=0;j<dataArray.length;j++) {
  73. //              for(int k=dataArray[j].length-w; k<dataArray[j].length; k++){
  74. //                  g.drawLine(50*(dim2-w)+10, dataArray[j][k-1], 50*(dim2-w)+100, dataArray[j][k]);
  75. //              }
  76. //          }
  77. //      }
  78. //    }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement