Board index » jbuilder » Newbie: TImage equivalent?

Newbie: TImage equivalent?


2003-11-21 05:05:59 AM
jbuilder14
Hi,
for getting started with java/swing, i'd like to build
a trivial pixel orientated app.
Perhaps someone could translate following simple Delphi "source"
to Java to point me in the right direction :-)
---------------------------------------------------------
In Delphi:
Create new app.
Drop TImage1 on Form.
Set Align = Left.
Drop TSplitter1 on Form.
Drop TImage2 on Form.
Set Align = Client.
Code the Formcreate as:
procedure TForm1.FormCreate(Sender: TObject);
var
x,y:integer;
begin
image1.picture.bitmap.width:=image1.width;
image1.picture.bitmap.height:=image1.height;
image1.picture.bitmap.Pixelformat:=pf24bit;
for x:=0 to image1.width-1 do
for y:=0 to image1.height-1 do
image1.picture.bitmap.canvas.pixels[x,y]:=rgb(random(256),0,0);
image2.picture.bitmap.width:=image2.width;
image2.picture.bitmap.height:=image2.height;
image2.picture.bitmap.Pixelformat:=pf24bit;
with image2.Picture.bitmap.canvas do
begin
brush.color:=clWhite;
FillRect(rect(0,0,image2.width,image2.height));
textout(100,100,'This is a test');
end;
end;
--------------------------------------------------------
In Java:
???
thanks a lot,
--
Andreas
He screamed: THIS IS SIG!
 
 

Re:Newbie: TImage equivalent?

Andreas Koch wrote:
Quote
Hi,

for getting started with java/swing, i'd like to build
a trivial pixel orientated app.
Perhaps someone could translate following simple Delphi "source"
to Java to point me in the right direction :-)

---------------------------------------------------------
In Delphi:
Create new app.
Drop TImage1 on Form.
Set Align = Left.
Drop TSplitter1 on Form.
Drop TImage2 on Form.
Set Align = Client.
Code the Formcreate as:

procedure TForm1.FormCreate(Sender: TObject);

var
x,y:integer;

begin
image1.picture.bitmap.width:=image1.width;
image1.picture.bitmap.height:=image1.height;
image1.picture.bitmap.Pixelformat:=pf24bit;
for x:=0 to image1.width-1 do
for y:=0 to image1.height-1 do
image1.picture.bitmap.canvas.pixels[x,y]:=rgb(random(256),0,0);

image2.picture.bitmap.width:=image2.width;
image2.picture.bitmap.height:=image2.height;
image2.picture.bitmap.Pixelformat:=pf24bit;
with image2.Picture.bitmap.canvas do
begin
brush.color:=clWhite;
FillRect(rect(0,0,image2.width,image2.height));
textout(100,100,'This is a test');
end;
end;

--------------------------------------------------------
In Java:
???


thanks a lot,
First off, let me point you to:
java.sun.com/docs/books/tutorial/2d/
There are really two things the above code is doing:
1) Manipulating pixels
2) drawing on the component
In current JDK's, manipulation is done by overriding the
paintComponent() method of the component (generally a JPanel instance)
that you are working with in order to manipulate the graphics context:
import java.util.Random;
*
*
*
JPanel firstPanel = new JPanel() {
public void paintComponent( Graphics g ) {
super.paintComponent( g ); // Fills according to default rules
for (int w = 0; w < getWidth() ; w++ ) {
for (int h = 0; h < getHeight(); h++ ) {
g.setColor( new Color( (int)(generator.nextDouble() *
255.0), 0, 0 ) );
g.fillRect(w,h,1,1);
}
}
}
};
Now, this isn't terribly performant. What you want to be doing is
manipulating a BufferedImage object associated with an Image - even if
you create that image at runtime - then paint that image into the
appropriate component. That way you don't have to be creating a
bazillion Color objects that just have to get garbage collected.
It is more in line with the method in your Delphi of manipulating the
array of pixels.
Check out the references I showed you, and do a Google on BufferedImage.
--
Tad Frysinger [TeamB]