import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
import javax.swing.JFrame;
import java.awt.Dimension;
import java.awt.Color;
abstract class BufferedCanvas extends Canvas {
private Image bImage; // offscreen buffer
private Graphics bg = null; // offscreen graphics context
private Graphics2D g2d = null; // onscreen graphics context
private int w, h; // width and height of canvas (Component)
private Container parent;
BufferedCanvas(Container parent) {
super();
setBackground(Color.black);
this.parent = parent;
}
public void paint(Graphics g) {
g2d = (Graphics2D) g; // cast to Graphics2D (just in case)
// create a new buffer image
bImage = createImage(maxx(), maxy());
bg = bImage.getGraphics();// get Graphics context for image
offscreenPaint();
bg.dispose(); // dispose graphics context
bImage.flush(); // flush (empty) image
bg = null;
}
abstract protected void offscreenPaint();
public void update() {
g2d.drawImage(bImage, 0, 0, this); // load buffer to screen
}
public void setColor(Color clr) {
if (bg != null) {
bg.setColor(clr);
}
}
public void setFont(Font fnt) {
if (bg != null) {
bg.setFont(fnt);
}
}
public void setXORMode(Color clr) {
if (bg != null) {
bg.setXORMode(clr);
}
}
public void drawline(int x1, int y1, int x2, int y2) {
if (bg != null) {
bg.drawLine(x1, y1, x2, y2);
}
}
public void drawline(int x1, int y1, int x2, int y2, Color clr) {
if (bg != null) {
Color currentColor = bg.getColor();
bg.setColor(clr);
bg.drawLine(x1, y1, x2, y2);
bg.setColor(currentColor);
}
}
public void drawarc(int x, int y, int w, int h, int startAngle, int arcAngle) {
if (bg != null) {
bg.drawArc(x, y, w, h, startAngle, arcAngle);
}
}
public void drawarc(int x, int y, int w, int h, int startAngle, int arcAngle, Color clr) {
if (bg != null) {
Color currentColor = bg.getColor();
bg.setColor(clr);
bg.drawArc(x, y, w, h, startAngle, arcAngle);
bg.setColor(currentColor);
}
}
public void drawfillarc(int x, int y, int w, int h, int startAngle, int arcAngle) {
if (bg != null) {
bg.fillArc(x, y, w, h, startAngle, arcAngle);
}
}
public void drawfillarc(int x, int y, int w, int h, int startAngle, int arcAngle, Color clr) {
if (bg != null) {
Color currentColor = bg.getColor();
bg.setColor(clr);
bg.fillArc(x, y, w, h, startAngle, arcAngle);
bg.setColor(currentColor);
}
}
public void drawoval(int x, int y, int w, int h) {
if (bg != null) {
bg.drawOval(x, y, w, h);
}
}
public void drawoval(int x, int y, int w, int h, Color clr) {
if (bg != null) {
Color currentColor = bg.getColor();
bg.setColor(clr);
bg.drawOval(x, y, w, h);
bg.setColor(currentColor);
}
}
public void drawfilloval(int x, int y, int w, int h) {
if (bg != null) {
bg.fillOval(x, y, w, h);
}
}
public void drawfilloval(int x, int y, int w, int h, Color clr) {
if (bg != null) {
Color currentColor = bg.getColor();
bg.setColor(clr);
bg.fillOval(x, y, w, h);
bg.setColor(currentColor);
}
}
public void drawroundbox(int x, int y, int w, int h, int arcw, int arch) {
if (bg != null) {
bg.drawRoundRect(x, y, w, h, arcw, arch);
}
}
public void drawroundbox(int x, int y, int w, int h, int arcw, int arch, Color clr) {
if (bg != null) {
Color currentColor = bg.getColor();
bg.setColor(clr);
bg.drawRoundRect(x, y, w, h, arcw, arch);
bg.setColor(currentColor);
}
}
public void drawfillroundbox(int x, int y, int w, int h, int arcw, int arch) {
if (bg != null) {
bg.fillRoundRect(x, y, w, h, arcw, arch);
}
}
public void drawfillroundbox(int x, int y, int w, int h, int arcw, int arch, Color clr) {
if (bg != null) {
Color currentColor = bg.getColor();
bg.setColor(clr);
bg.fillRoundRect(x, y, w, h, arcw, arch);
bg.setColor(currentColor);
}
}
public void drawbox(int x, int y, int w, int h) {
if (bg != null) {
bg.drawRect(x, y, w, h);
}
}
public void drawbox(int x, int y, int w, int h, Color clr) {
if (bg != null) {
Color currentColor = bg.getColor();
bg.setColor(clr);
bg.drawRect(x, y, w, h);
bg.setColor(currentColor);
}
}
public void drawfillbox(int x, int y, int w, int h) {
if (bg != null) {
bg.fillRect(x, y, w, h);
}
}
public void drawfillbox(int x, int y, int w, int h, Color clr) {
if (bg != null) {
Color currentColor = bg.getColor();
bg.setColor(clr);
bg.fillRect(x, y, w, h);
bg.setColor(currentColor);
}
}
public void draw3Dbox(int x, int y, int w, int h, boolean raised) {
if (bg != null) {
bg.draw3DRect(x, y, w, h, raised);
}
}
public void draw3Dbox(int x, int y, int w, int h, boolean raised, Color clr) {
if (bg != null) {
Color currentColor = bg.getColor();
bg.setColor(clr);
bg.draw3DRect(x, y, w, h, raised);
bg.setColor(currentColor);
}
}
public void drawString(String str, int x, int y) {
if (bg != null) {
bg.drawString(str, x, y);
}
}
public void delay(int msec) {
try {
Thread.sleep(msec);
} catch (Exception e) {
System.err.println(e);
}
}
public int maxx() {
return parent.getBounds().width;
}
public int maxy() {
return parent.getBounds().height;
}
}
class DrawView extends BufferedCanvas {
DrawView(JFrame parent) {
super(parent);
}
protected void offscreenPaint() {
setXORMode(Color.white); // set white as the XORed colour
for (int i = 0; i < maxx(); i++)
{
drawoval(i, i % 200, 100, 100);
update();
drawoval(i, i % 200, 100, 100);
}
}
}
class drawTest {
public static void main(String[] args) {
JFrame frame = new JFrame("Test Application");
Dimension screenSize = frame.getToolkit().getScreenSize();
frame.setBounds(screenSize.width/4, screenSize.height/4,
screenSize.width/2, screenSize.height/2);
frame.getContentPane().add(new DrawView(frame));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
} |