1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
| public class ScreenPrinter extends JPanel implements Scrollable{
private int tabValue[] = new int[2048];
private int maxUnitIncrement = 1;
private ArrayList<Integer> list = new ArrayList< Integer>();
/**
* Constructeur par defaut
*/
public ScreenPrinter(int inMaxUnitIncrement){
setSize(300, 960);
for(int i = 0; i < 2048; i++)
{
double doubleVal = Math.random();
int intVal = (int) (doubleVal * 2048) ;
tabValue[i] = intVal ;
}
maxUnitIncrement = inMaxUnitIncrement;
}
public void AddLine(){
System.out.println("add a line");
double doubleVal = Math.random();
int intVal = (int) (doubleVal * 2048) ;
list.add(intVal);
this.repaint();
}
public void paintComponent(Graphics g){
int widthJPanel = this.getWidth() ;
int heightJPanel = this.getHeight() ;
g.setColor(new Color(1.0f, 0.0f, 0.0f)); // blanc
g.fillRect(0, 0, widthJPanel, heightJPanel);
g.setColor(new Color(0.0f, 0.0f, 0.0f)); // noir
if(list.size() == 0)
return ;
int cpt = 0 ;
// on doit inverser la descente du schema pour avoir l'effet "sismographe"
ArrayList<Integer> reverseList = new ArrayList< Integer>();
for(int i=list.size()-1; i>=0; i--)
reverseList.add(list.get(i));
for(Integer oneLine : reverseList)
{
int x1 = 0 ;
int y1 = 0 ;
int x2 = widthJPanel * (2048 - oneLine) / 2048 ;
int y2 = 0 ;
g.drawLine(0, cpt, x2, cpt);
cpt++;
}
}
public Dimension getPreferredSize() {
return super.getPreferredSize();
}
//*/
@Override
public Dimension getPreferredScrollableViewportSize() {
// TODO Auto-generated method stub
return getPreferredSize();
}
@Override
public int getScrollableBlockIncrement(Rectangle visibleRect,
int orientation,
int direction) {
if (orientation == SwingConstants.HORIZONTAL) {
return visibleRect.width - maxUnitIncrement;
} else {
return visibleRect.height - maxUnitIncrement;
}
}
@Override
public boolean getScrollableTracksViewportHeight() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean getScrollableTracksViewportWidth() {
// TODO Auto-generated method stub
return false;
}
@Override
public int getScrollableUnitIncrement(Rectangle visibleRect,
int orientation,
int direction) {
//Get the current position.
int currentPosition = 0;
if (orientation == SwingConstants.HORIZONTAL) {
currentPosition = visibleRect.x;
} else {
currentPosition = visibleRect.y;
}
//Return the number of pixels between currentPosition
//and the nearest tick mark in the indicated direction.
if (direction < 0) {
int newPosition = currentPosition -
(currentPosition / maxUnitIncrement)
* maxUnitIncrement;
return (newPosition == 0) ? maxUnitIncrement : newPosition;
} else {
return ((currentPosition / maxUnitIncrement) + 1) * maxUnitIncrement - currentPosition;
}
}
//*/
} |
Partager