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 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
| public class OscilloscopeExemple extends Canvas {
private static final int BUFFER_SIZE = 1000;
private static final Color MAIN_GRID_COLOR = Color.WHITE;
private static final Color SECONDARY_GRID_COLOR = Color.GRAY;
private static final int HORIZONTAL_GRID_DIVISION_NUMBER = 10;
private static final int VERTICAL_GRID_DIVISION_NUMBER = 10;
private static final Color SIGNAL_COLOR = Color.RED;
private FunctionThread thread;
private final BackgroundGrid grid;
private double[] buffer;
private int next;
private volatile boolean bufferFilled;
private final AtomicLong lastInput;
private int lastWidth;
private int lastHeight;
private BufferStrategy buffereStrategy;
private float scalex;
private float scaley;
private double adaptedScalex;
private final IFuntion function;
private int midy;
private final int updateRate;
private Timer timer;
public OscilloscopeExemple(int bufferSize, float scalex, float scaley, IFuntion function) {
this(bufferSize, scalex, scaley, function, -1);
}
/**
*
* @param bufferSize nombre de mesures conservées
* @param scalex facteur d'échelle x
* @param scaley facteur d'échelle y
* @param function function de signal
* @param fps frame rate (si <0, on dessine en RT, sinon on dessin à intervalle régulier)
*/
public OscilloscopeExemple(int bufferSize, float scalex, float scaley, IFuntion function, int fps) {
this.function = function;
buffer = new double[bufferSize];
next = 0;
bufferFilled = false;
lastInput = new AtomicLong();
grid = new BackgroundGrid(MAIN_GRID_COLOR, SECONDARY_GRID_COLOR,
HORIZONTAL_GRID_DIVISION_NUMBER, VERTICAL_GRID_DIVISION_NUMBER);
lastWidth = lastHeight = -1;
this.scalex=scalex;
this.scaley=scaley;
if ( fps<=0 ) {
updateRate=-1;
}
else {
updateRate = 1000/fps;
}
setIgnoreRepaint(true);
addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
initOrReset();
}
});
}
private synchronized void initOrReset() {
final int width = getWidth();
final int height = getHeight();
if (lastWidth != width || lastHeight != height) {
grid.setSize(width, height);
midy = height >> 1;
adaptedScalex = (scalex * width)/buffer.length;
}
}
public void setScaleX(float scalex) {
this.scalex=scalex;
adaptedScalex = (scalex * getWidth())/buffer.length;
draw();
}
public void setScaleY(float scaley) {
this.scaley=scaley;
draw();
}
public synchronized void start() {
if (thread == null) {
initOrReset();
createBufferStrategy(2);
buffereStrategy = getBufferStrategy();
thread = new FunctionThread();
thread.start();
if ( updateRate>0 ) {
timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
draw();
}
}, updateRate, updateRate);
}
}
}
public synchronized void stop() {
if (thread != null) {
thread.stopThread();
thread = null;
buffereStrategy.dispose();
buffereStrategy=null;
if ( timer!=null ) {
timer.cancel();
}
}
}
private void draw() {
if ( buffereStrategy==null ) return;
Graphics2D grBuffer = (Graphics2D) buffereStrategy.getDrawGraphics();
grBuffer.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
grBuffer.drawImage(grid.getImage(), 0, 0, null);
synchronized (this) {
grBuffer.setColor(SIGNAL_COLOR);
if ( bufferFilled ) {
double prevx=0;
int prevy=0;
for(int i=next; i<buffer.length; i++) {
if ( i==0 ) {
prevx = 0;
prevy = (int)(midy + buffer[i]*scaley);
}
else {
double x = prevx+adaptedScalex;
int y = (int)(midy + buffer[i]*scaley);
grBuffer.drawLine( (int)prevx, prevy, (int)x, y);
prevx = x;
prevy = y;
}
}
for(int i=0; i<next; i++) {
double x = prevx+adaptedScalex;
int y = (int)(midy + buffer[i]*scaley);
grBuffer.drawLine( (int)prevx, prevy, (int)x, y);
prevx = x;
prevy = y;
}
}
else {
double prevx=0;
int prevy=0;
for(int i=0; i<next; i++) {
if ( i==0 ) {
prevx = 0;
prevy = (int)(midy + buffer[i]*scaley);
}
else {
double x = prevx+adaptedScalex;
int y = (int)(midy + buffer[i]*scaley);
grBuffer.drawLine( (int)prevx, prevy, (int)x, y);
prevx = x;
prevy = y;
}
}
}
}
grBuffer.dispose();
buffereStrategy.show();
}
@Override
public void paint(Graphics g) {
// ne fait rien
}
@Override
public void paintAll(Graphics g) {
// ne fait rien
}
public void updateFunction(long newValue) {
if (lastInput.compareAndSet(lastInput.longValue(), newValue)) {
synchronized (this) {
buffer[next++] = this.function.getValue(newValue);
if (next == buffer.length) {
next = 0;
bufferFilled = true;
}
}
if ( timer==null ) draw();
}
}
private class FunctionThread extends Thread {
private volatile boolean running = true;
@Override
public void run() {
while (running) {
updateFunction(System.currentTimeMillis());
}
}
public void stopThread() {
running = false;
}
}
@FunctionalInterface
public interface IFuntion {
double getValue(long t);
}
// *************************** METHODES DE TEST ***************************
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> run());
}
public static void run() {
//System.setProperty("sun.java2d.transaccel", "True");
// System.setProperty("sun.java2d.ddforcevram", "True");
JFrame frame = new JFrame("Démo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
OscilloscopeExemple panel = new OscilloscopeExemple(BUFFER_SIZE, 1, 1, (t)-> Math.sin(t/30d) * 100 );
frame.getContentPane().add(panel);
frame.setSize(300, 300);
frame.setLocationRelativeTo(null);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowOpened(WindowEvent e) {
panel.start();
}
@Override
public void windowClosed(WindowEvent e) {
panel.stop();
}
});
frame.setVisible(true);
}
// ************************************************************************************
} |
Partager