Article View: borland.public.jbuilder.java.swing
Article #1598Re: Changing the square?
From: Valentino Kyriakides
Date: Tue, 21 Oct 2003 18:44
Date: Tue, 21 Oct 2003 18:44
184 lines
6661 bytes
6661 bytes
"Lori M Olson (TeamB)" <javadragon@techie.com> schrieb im Newsbeitrag
news:3f957217@newsgroups.borland.com...
> myjava33 wrote:
> > How can I get the gu� to look l�ke someth�ng other than a square or
> > rectangle? What if I wanted the gui to be round or triangular? Is this
> > possible?
> >
> >
>
> You know, I'm pretty sure I have seen an example of this. And I'm
> pretty sure it involved the use of GlassPane. But I can't find it now.
> It may have been a widget that you have to buy.
>
> Sorry my memory is so bad :-)
>
Yes, it can be done and is often done for java game programmings etc., even
it's not very responsive/fast for bigger window surfaces/areas. AFAIK there
are also some other different techniques for this available. However I only
tried myself out to do similar things with undecorated windows and partly
transparent windows. Mainly, since I use such techniques in an actually (yet
not publicly distributed/available) JBuilder OT, which is a sort of JBuilder
screen capture add-on tool . - I'am not sure if I ever will officially
publish that JB Screencapture OT, but maybe some day I will do so.
Ok, here is a much simpler code example for a transparent non-rectangular
movable window, for tryouts:
---------------------- start of code ---------------------
// Demo for an oval transparent "looking through the glass" window.
// When started the window can be moved around with the mouse,
// note that this demo app has to be killed the hard way, so I recommend
// to start and stop it from inside of a JB via a project runtime
configuration.
import java.awt.AWTException;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.Robot;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import javax.swing.JWindow;
public class OvalWindowDemo extends JWindow implements MouseMotionListener
{
private int ox = 0;
private int oy = 0;
private int W = 100;
private int H = 100;
private Robot robot;
private BufferedImage [] underneath = new BufferedImage[2];
private int flipflop = 0;
private BufferedImage canvas; // for flicker free drawing
private Rectangle strip = new Rectangle();
public OvalWindowDemo ()
{
ox = 100; oy = 100;
Rectangle bounds = new Rectangle(ox,oy,W,H);
setBackground( new Color(0,0,0,0) );
setForeground( Color.MAGENTA );
setBounds( bounds );
underneath[0] = new BufferedImage( W, H, BufferedImage.TYPE_INT_BGR );
underneath[1] = new BufferedImage( W, H, BufferedImage.TYPE_INT_BGR );
try
{
robot = new Robot();
BufferedImage tmp = robot.createScreenCapture( bounds );
underneath[0].getGraphics().drawImage(tmp,0,0,null);
underneath[1].getGraphics().drawImage(tmp,0,0,null);
}
catch( AWTException e )
{
e.printStackTrace();
}
canvas = new BufferedImage( W, H, BufferedImage.TYPE_INT_BGR );
addMouseMotionListener( this );
System.gc();
}
public static void main( String [] args )
{
OvalWindowDemo cs = new OvalWindowDemo();
cs.show();
}
public void initialize()
{
}
public void update( Graphics g ) { paint( g ); }
public void paint( Graphics g )
{
Graphics2D gc = (Graphics2D) canvas.getGraphics();
gc.drawImage( underneath[flipflop], 0 ,0 , null );
gc.setColor( Color.MAGENTA );
gc.setRenderingHint( RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON );
gc.drawOval(10,10,80,80);
setLocation( ox, oy );
g.drawImage( canvas, 0 ,0 , null );
}
// ***Interface implementations***
// MouseMotionListener for interactive movement
public void mouseDragged( MouseEvent e )
{
// Determine new window location
int nx = ox+e.getX()-W/2;
int ny = oy+e.getY()-H/2;
int dx = nx-ox;
int dy = ny-oy;
if( dx == 0 && dy ==0 )
return;
// Construct new 'underneath' image
int flop = 1-flipflop;
Graphics g = underneath[flop].getGraphics();
g.drawImage( underneath[flipflop], -dx, -dy, null);
// Grab new background strips
if( dx > 0 )
{
strip.x = nx+W-dx;
strip.y = ny;
strip.width = dx;
strip.height = H;
g.drawImage( robot.createScreenCapture( strip ), W-dx, 0, null );
}
else if ( dx < 0 )
{
strip.x = nx;
strip.y = ny;
strip.width = -dx;
strip.height = H;
g.drawImage( robot.createScreenCapture( strip ), 0, 0, null );
}
if( dy > 0 )
{
strip.x = dx > 0 ? nx : nx-dx;
strip.y = ny+H-dy;
strip.width = W-Math.abs(dx); // optimize
strip.height = dy;
g.drawImage( robot.createScreenCapture( strip ), dx>0?0:-dx, H-dy,
null );
}
else if ( dy < 0 )
{
strip.x = dx > 0 ? nx : nx-dx;
strip.y = ny;
strip.width = W-Math.abs(dx); // optimize it
strip.height = -dy;
g.drawImage( robot.createScreenCapture( strip ), dx>0?0:-dx, 0, null );
}
flipflop = flop;
ox = nx;
oy = ny;
paint( getGraphics() );
}
public void mouseMoved( MouseEvent e )
{
}
}
---------------------- end of code ---------------------
I hope that it at least points you into the desired direction...? I also
once had build up a different undecorated window based example (one with a
sort of selectable window like border, a close button and so on...), which
doesn't used such transparency levels at all, but I would have to search
through all of my Java/JB dev backup CDs first in order to find that
example. - No need to say, that I'am actually tooo lazy to do so.
-Valentino
Technical Details
Message-ID:
<3f957e20@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!in.100proofnews.com!in.100proofnews.com!news02.tsnz.net!newsfeed01.tsnz.net!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mailReferences:
<3f9051be@newsgroups.borland.com> <3f957217@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII X-Google-Thread: 111d45,114b20d23f5d4ea0 X-Google-Attributes: gid111d45,public X-Google-ArrivalTime: 2003-10-21 11:56:34 PST From: "Valentino Kyriakides" <vkyr@nospam-ision.net> Newsgroups: borland.public.jbuilder.java.swing References: <3f9051be@newsgroups.borland.com> <3f957217@newsgroups.borland.com> Subject: Re: Changing the square? Date: Tue, 21 Oct 2003 20:44:57 +0200 Lines: 186 Organization: Kyriakides X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1106 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106 NNTP-Posting-Host: 213.39.212.19 Message-ID: <3f957e20@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1066761760 213.39.212.19 (21 Oct 2003 11:42:40 -0700) Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!in.100proofnews.com!in.100proofnews.com!news02.tsnz.net!newsfeed01.tsnz.net!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail