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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
| public class ScreenPrinter3 extends JLabel implements Scrollable, MouseMotionListener{
private int tabValue[] = new int[2048];
private int maxUnitIncrement = 1;
//private ArrayList<int[]> list = new ArrayList< int[]>();
private ArrayList<Integer> list = new ArrayList< Integer>();
/**
* Constructeur par defaut
*/
public ScreenPrinter3(int inMaxUnitIncrement){
maxUnitIncrement = inMaxUnitIncrement;
}
/**
*
*/
public void AddLine(){
double doubleVal = Math.random();
int intVal = (int) (doubleVal * 2048) ;
list.add(intVal);
this.repaint();
}
/**
*
*/
public void paintComponent(Graphics g){
// largeur
int widthJPanel = this.getWidth() ;
// hauteur
//int heightJPanel = this.getHeight() ;
int heightJPanel = list.size() ;
Dimension dim = new Dimension(widthJPanel, heightJPanel);
setMinimumSize(dim);
setPreferredSize(dim);
setMaximumSize(dim);
//g.setColor(new Color(1.0f, 1.0f, 1.0f)); // blanc
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++;
}
Print(reverseList);
}
public Dimension getPreferredSize() {
return new Dimension(super.getWidth(), list.size());
//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;
}
}
//*/
/**
*
*/
public void Print(ArrayList<Integer> inReverseList){
System.out.println("fonction test du panel");
try {
Calendar calendar = Calendar.getInstance();
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
int day = calendar.get(Calendar.DAY_OF_MONTH);
int month = calendar.get(Calendar.MONTH);
int year = calendar.get(Calendar.YEAR);
// on recupere la taille de l'image, en largeur 2048, en hauteur le nombre de ligne
int width = 2048;
int height = inReverseList.size();
// Create a buffered image with transparency
BufferedImage bimage = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_BINARY);
// Draw the image on to the buffered image
Graphics2D bGr = bimage.createGraphics();
int cpt = 0 ;
for(Integer oneLine : inReverseList)
{
bGr.drawLine(oneLine, cpt, 0, cpt);
cpt++;
}
bGr.dispose();
StringBuffer strBffr = new StringBuffer();
strBffr.append(year).append(month).append(day).append("_").append(hour).append("_").append(minute).append(".png");
ImageIO.write(bimage,"PNG",new File(strBffr.toString()));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* Converts a given Image into a BufferedImage
*
* @param img The Image to be converted
* @return The converted BufferedImage
*/
public static BufferedImage toBufferedImage(Image img)
{
if (img instanceof BufferedImage)
{
return (BufferedImage) img;
}
// Create a buffered image with transparency
BufferedImage bimage = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB);
// Draw the image on to the buffered image
Graphics2D bGr = bimage.createGraphics();
bGr.drawImage(img, 0, 0, null);
bGr.dispose();
// Return the buffered image
return bimage;
}
} |
Partager