Article View: borland.public.jbuilder.java.swing
Article #1447Re: Newbie: TImage equivalent?
From: 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]
Technical Details
Message-ID:
<3fbe657d@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed.media.kyoto-u.ac.jp!newsfeed.icl.net!newsfeed.fjserv.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<3fbd2c76@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 111d45,2bbf018dbaca818 X-Google-Attributes: gid111d45,public X-Google-ArrivalTime: 2003-11-21 11:40:28 PST Date: Fri, 21 Nov 2003 12:19:46 -0700 From: "Tad Frysinger [TeamB]" <tfrysinger@earthlink.net> User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.4) Gecko/20030624 X-Accept-Language: en-us, en MIME-Version: 1.0 Newsgroups: borland.public.jbuilder.java.swing Subject: Re: Newbie: TImage equivalent? References: <3fbd2c76@newsgroups.borland.com> In-Reply-To: <3fbd2c76@newsgroups.borland.com> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit NNTP-Posting-Host: 67.165.213.196 Message-ID: <3fbe657d@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1069442429 67.165.213.196 (21 Nov 2003 11:20:29 -0700) Lines: 94 Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed.media.kyoto-u.ac.jp!newsfeed.icl.net!newsfeed.fjserv.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail