Advertisement

[java] How to do the fatest redraws in applets ?

Started by September 27, 2000 02:17 AM
0 comments, last by GeertPoels 24 years, 3 months ago
Hi, I''ve seen lots of different implementations on the drawing side of a (demo)-applet. Going from the usual override of the paint and repaint methods to defining an extra thread, some using a sleep, some putting all drawing code in the repaint, ... . I believe calling repaint() "en masse" doesn''t work with an applet since the browser''s repaintthread has a lower priority and may in some cases never reach the repaint. What''s the safest way to implement an applet that requires fast redrawing ? Are some optimisations recommended depending on the case (much maths) ? Tnx Geert

Well you can set a image up to act like a buffer screen (like the good old days of chain-x in 68 asm), then you can opt. & refine to your hart''s content..

take a look @ this code, it may give you some ideas (well it compiles with _my_ jdk 1.6

import java.applet.*;
import java.awt.image.*;
import java.awt.*;

public class DirectDraw extends Applet implements Runnable {
private DirectImage bitmap;
private Image bitImg;
private int xs, ys;
private int width, height;
private Thread ticker;
private boolean running = false;

public void init () {
width = getSize().width;
height = getSize().height;
bitmap = new DirectImage(width, height);
bitImg = createImage(bitmap);
}
public void paint (Graphics g) {
bitImg.flush();
g.drawImage(bitImg, 0, 0, null);
}
public void run () {
int off = 0;
while (running) {
waveForm(off += 4);
repaint();
try {
ticker.sleep(1000 / 30);
} catch (InterruptedException e) { ; }
}
}
public void start () {
if (ticker == null || !ticker.isAlive()) {
running = true;
ticker = new Thread(this);
ticker.setPriority(Thread.MIN_PRIORITY + 1);
ticker.start();
}
}
public void stop () {
running = false;
}
public void update (Graphics g) {
paint(g);
}
public void waveForm (int off) {
for (int ii = 0; ii < bitmap.pixels.length; ii++)
bitmap.pixels[ii] = 0xFF000000;
int base = (height >> 1);
double twoPI = Math.PI * 2;
double scale = (double) height * 0.75;
for (int x = 0; x < width; x++) {
double rad = ((x + off) * twoPI) / width;
int y = ((int) (Math.sin(rad) * scale) / 2) + base;
bitmap.pixels[y * bitmap.width + x] = 0xFFFFFFFF;
}
}
}

This topic is closed to new replies.

Advertisement