Thread View: borland.public.jbuilder.java.swing
2 messages
2 total messages
Started by Andreas Koch
Thu, 20 Nov 2003 21:05
Newbie: TImage equivalent?
Author: Andreas Koch
Date: Thu, 20 Nov 2003 21:05
Date: Thu, 20 Nov 2003 21:05
50 lines
2279 bytes
2279 bytes
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?
Author: Tad Frysinger [TeamB]
Date: Fri, 21 Nov 2003 19:19
Date: Fri, 21 Nov 2003 19:19
93 lines
3875 bytes
3875 bytes
Andreas Koch wrote:
> 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:
http://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]
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads