[Library] Turing-like drawing functions
Author |
Message |
rizzix
|
Posted: Sat Feb 21, 2004 11:22 pm Post subject: [Library] Turing-like drawing functions |
|
|
it's like turing with "offscreenonly" mode on always. I'll be adding more to it soon.
Java: | import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
public 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;
}
} |
here's how you use it:
Java: | import java.awt.*;
import javax.swing.JFrame;
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++ )
{
drawline (0, 0 ,i, 400); // drawn in white (on buffer)
drawoval (i, i % 200, 100, 100);
update ();
drawline (0, 0, i, 400); // drawn in background color (XORed)
drawoval (i, i % 200, 100, 100);
}
}
} |
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
Homer_simpson
|
Posted: Fri Feb 27, 2004 6:10 pm Post subject: (No subject) |
|
|
this is purty awsome dude...
i made some 3d engine graphical thingy too... i'll post it up soon... |
|
|
|
|
|
|
|