Subject:
|
Re: Java LDraw/LDLite
|
Newsgroups:
|
lugnet.cad.dev
|
Date:
|
Thu, 25 Feb 1999 22:13:44 GMT
|
Viewed:
|
1588 times
|
| |
| |
In lugnet.cad.dev, blisses@worldnet.att.net (Steve Bliss) writes:
> How do you draw (pixels) in Java? Are there built-in functions, or
> would it require platform-specific code, or what? Or graphics library,
> perhaps?
It's surprisingly easy. Java actually doesn't suck! :)
You just allocate a buffer, designate it as being a graphics buffer, and call
functions to do graphics primitives. Here's an example -- not probably a
great example because it's 3 years old and not documented worth a darn -- but
it was quick to dig up and does give an example of raster blasting at a low
but completely portable level.
I'm sure the standard libraries have faster code than this for filling
rectangles, but I needed bilinear-interpolated shaded rectangles, so I had to
do it by hand.
BTW, one of the BEST things about it is that you can work entirely in 24-bit
(or 32-bit) color and the built-in graphics subsystem takes care of color
quantization totally transparently! (no pun intended :)
--Todd
_____________________________________________________________________________
// Basic raw (low-level) image class for Mandelbrot fractal explorer.
//
// Todd Lehman, May 28, 1996
import java.awt.*;
import java.applet.*;
import java.awt.image.ColorModel;
import java.awt.image.MemoryImageSource;
public class RawImage
{
protected int dx, dy;
private int pixels[];
private Image img;
private MemoryImageSource mis;
private boolean isdirty;
public RawImage(int dx, int dy)
{
this.dx = dx;
this.dy = dy;
this.pixels = new int[dx * dy];
this.mis = new MemoryImageSource(dx, dy, ColorModel.getRGBdefault(),
pixels, 0, dx);
this.isdirty = true;
for (int p = dx*dy-1; p >= 0; p--) pixels[p] = 0xFF000000;
}
public int GetWidth() { return dx; }
public int GetHeight() { return dy; }
public synchronized Image GetImage()
{
if (this.isdirty)
{
this.isdirty = false;
}
return this.img;
}
public MemoryImageSource GetMemoryImageSource()
{
return mis;
}
public int GetPixelColor(int x, int y)
{
if (x < 0) x = 0;
else if (x >= this.dx) x = this.dx-1;
if (y < 0) y = 0;
else if (y >= this.dy) y = this.dy-1;
return pixels[(y * this.dx) + x];
}
public void SolidFillRect(int x0, int y0, int dx, int dy, int color)
{
for (int y = y0+dy-1; y >= y0; y--)
{
int p = (y * this.dx) + x0 + dx - 1;
for (int x = x0+dx-1; x >= x0; x--)
{
pixels[p--] = color;
}
}
this.isdirty = true;
}
public void SmoothFillRect(int x0, int y0, int dx, int dy,
int c00, int c10, int c01, int c11)
{
if ((x0 < 0) || (x0+dx >= this.dx) ||
(y0 < 0) || (y0+dy >= this.dy))
{
return;
}
int A = 0xFF000000;
int R = 0x00FF0000;
int G = 0x0000FF00;
int B = 0x000000FF;
int X = 0x00FF0000;
int r00=(c00 & R)<<0, r01=(c01 & R)<<0, r0=r00, r0inc=(r01-r00)/dy;
int g00=(c00 & G)<<8, g01=(c01 & G)<<8, g0=g00, g0inc=(g01-g00)/dy;
int b00=(c00 & B)<<16, b01=(c01 & B)<<16, b0=b00, b0inc=(b01-b00)/dy;
int r10=(c10 & R)<<0, r11=(c11 & R)<<0, r1=r10, r1inc=(r11-r10)/dy;
int g10=(c10 & G)<<8, g11=(c11 & G)<<8, g1=g10, g1inc=(g11-g10)/dy;
int b10=(c10 & B)<<16, b11=(c11 & B)<<16, b1=b10, b1inc=(b11-b10)/dy;
int p = (y0 * this.dx) + x0;
for (int y = 0; y < dy; y++)
{
int r = r0, rinc = (r1 - r0) / dx;
int g = g0, ginc = (g1 - g0) / dx;
int b = b0, binc = (b1 - b0) / dx;
for (int x = 0; x < dx; x++)
{
pixels[p++] = A |
((r & X) >>> 0) |
((g & X) >>> 8) |
((b & X) >>> 16);
r += rinc;
g += ginc;
b += binc;
}
r0 += r0inc; r1 += r1inc;
g0 += g0inc; g1 += g1inc;
b0 += b0inc; b1 += b1inc;
p += this.dx - dx;
}
this.isdirty = true;
}
}
|
|
Message has 2 Replies: | | Re: Java LDraw/LDLite
|
| (...) It's probably even easier now, with the new 2D graphics API finished (1), and a 3D API in the works. I don't claim to know how these work (or even if they're useful for anything :-) but I've seen the 2D API favorably compared to the (...) (26 years ago, 25-Feb-99, to lugnet.cad.dev)
| | | Re: Java LDraw/LDLite
|
| I guess I'll be learning Java RSN. Anyone have a preference on what development tools to use? More important, any pointers to good learning tools (ie, books)? Thanks, Steve (...) (26 years ago, 26-Feb-99, to lugnet.off-topic.fun)
|
Message is in Reply To:
| | Re: Java LDraw/LDLite
|
| (...) How do you draw (pixels) in Java? Are there built-in functions, or would it require platform-specific code, or what? Or graphics library, perhaps? Steve (26 years ago, 25-Feb-99, to lugnet.cad.dev)
|
51 Messages in This Thread:
- Entire Thread on One Page:
- Nested:
All | Brief | Compact | Dots
Linear:
All | Brief | Compact
This Message and its Replies on One Page:
- Nested:
All | Brief | Compact | Dots
Linear:
All | Brief | Compact
|
|
|
|