Please join us at the new JavaWorld Q&A Forums. Your existing login will work there. The discussions here are now read-only.


JavaWorld Talkback >> 960204

Pages: 1
TimYates
Unregistered




Swing version
      #21942 - 09/19/05 10:13 AM

After the Swing based commets from your last article Jeff, I thought I'd have a go at a Swing version of this one...

I have added the code as well, to use AffineTransform to paint a 1/2 size "corner" image into the four corners, rather than draw the polygon four times. This is probably complete overkill, and the original "paint" routine is probably more understandable...

Still...Hope it proves interesting for someone

Code:
 // A Swing version of the kaleidoscope tutorial from JavaWorld
//
// http://www.javaworld.com/javaworld/jw-09-2005/jw-0919-funandgames.html
//
// This only draws one quater of the screen, then flips that image about to draw the other
// three quarters.
//
// The image is first drawn inside the KalSwing class, then, the KalPanel class paints this image
// to the screen.

import java.awt.* ;
import java.awt.geom.* ;
import java.awt.image.* ;
import javax.swing.* ;

public class KalSwing extends JApplet implements Runnable
{
class KalPanel extends JPanel
{
public void paintComponent( Graphics g )
{
Graphics2D g2 = (Graphics2D)g ;

// Draw the top left hand corner normally, as you would
g2.drawImage( imBuffer, 0, 0, this ) ;

// This is the bottom left hand corner.
// Flip it upside down
AffineTransform tx = AffineTransform.getScaleInstance( 1, -1 ) ;
// Then move it back into its original position
tx.translate( 0, -imBuffer.getHeight() ) ;
AffineTransformOp op = new AffineTransformOp( tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR ) ;
// And draw it in the bottom left hand corner
g2.drawImage( imBuffer, op, 0, halfHeight ) ;

// This is the top right hand corner.
// Flip it horizontally
tx = AffineTransform.getScaleInstance( -1, 1 ) ;
// Then move it back into its original position
tx.translate( -imBuffer.getWidth(), 0 ) ;
op = new AffineTransformOp( tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR ) ;
// And draw it in the top right hand corner
g2.drawImage( imBuffer, op, halfWidth, 0 ) ;

// This is the bottom right hand corner.
// Flip it horizontally and vertically (same as a 180deg rotation)
tx = AffineTransform.getScaleInstance( -1, -1 ) ;
// Then move it back into its original position
tx.translate( -imBuffer.getWidth(), -imBuffer.getHeight() ) ;
op = new AffineTransformOp( tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR ) ;
// And draw it in the bottom right hand corner
g2.drawImage( imBuffer, op, halfWidth, halfHeight ) ;
}
}

// Maximum iterations before background is cleared.
final static int MAXITER = 200 ;

// Maximum number of points that comprise polygon.
final static int MAXPOINTS = 6 ;

// Animation thread.
Thread runner ;

// Current number of iterations counter.
int counter ;

// Half the width of the applet drawing area.
int halfWidth ;

// Half the height of applet drawing area.
int halfHeight ;

// Image buffer.
BufferedImage imBuffer ;

// The panel contained by this applet which will do all the painting
KalPanel kalPanel ;

// Array of polygon points x-coordinates.
int[] xpoints = new int[ MAXPOINTS ] ;

// Array of polygon points y-coordinates.
int[] ypoints = new int[ MAXPOINTS ] ;

// Initialize the applet. Invoked by Web browser.
public void init()
{
// Acquire the applet's width and height.
halfWidth = getSize().width >> 1 ;
halfHeight = getSize().height >> 1 ;

// Create an image buffer.
imBuffer = new BufferedImage( halfWidth, halfHeight, BufferedImage.TYPE_INT_RGB ) ;

// Create a new KalPanel object, this will paint our kaleidoscope
kalPanel = new KalPanel() ;

// Add the panel to our jApplet...
this.getContentPane().setLayout( new BorderLayout() ) ;
this.getContentPane().add( kalPanel, BorderLayout.CENTER ) ;
}

// Start the applet. Invoked by Web browser.

public void start()
{
// If no thread exists, create a Thread object that associates with the
// current Kal object, and start a new thread that invokes the current
// Kal object's run() method. Before doing that, clear the counter so
// we don't draw only a few polygons before clearing the applet's
// drawing area.

if( runner == null )
{
counter = 0 ;

runner = new Thread( this ) ;
runner.start() ;
}
}

// Paint the image onto the 1/4 sized BufferedImage
public void paintPattern()
{
Graphics imG = imBuffer.getGraphics() ;

// Clear image buffer before it gets too crowded.
if( ++counter == MAXITER )
{
imG.setColor( Color.black ) ;
imG.fillRect( 0, 0, halfWidth, halfHeight ) ;
counter = 0 ;
}

// Generate a random color and establish that color as the image buffer
// Graphics context's drawing color.
Color c = new Color( rnd( 256 ), rnd( 256 ), rnd( 256 ) ) ;
imG.setColor( c ) ;

// Obtain number of points that compose the polygon: 1 through MAXPOINTS.
int npoints = rnd( MAXPOINTS ) + 1 ;

// Obtain coordinates for each point composing the polygon.
for( int i = 0 ; i < npoints ; i++ )
{
xpoints[ i ] = rnd( halfWidth ) ;
ypoints[ i ] = rnd( halfHeight ) ;
}

// Display initial polygon.
imG.fillPolygon( xpoints, ypoints, npoints ) ;
}

// Obtain a random integer.

static int rnd( int limit )
{
// Return a random integer that ranges from 0 through limit-1.

return( int ) ( Math.random() * limit ) ;
}

// Run the animation.

public void run()
{
// Obtain a reference to the thread that was started in the applet's
// start() method.

Thread current = Thread.currentThread() ;

// As long as runner contains the same reference, keep looping. The
// reference in runner becomes null when the applet's stop() method
// executes.

while( current == runner )
{
// Draw the new polygon to our buffered image
paintPattern() ;

// Then paint this image to the four corners of our KalPanel
kalPanel.repaint() ;

// Pause for 50 milliseconds to achieve an eye-pleasing display.
try
{
Thread.sleep( 50 ) ;
}
catch( InterruptedException e )
{
}
}
}

// Stop the applet. Invoked by Web browser.

public void stop()
{
// Tell the drawing thread to terminate.
runner = null ;
}
}



Hope my modifications are ok with you Jeff...

Cheers,

Tim


Post Extras: Print Post   Remind Me!   Notify Moderator  
Jeff Friesen
Unregistered




Re: Swing version [Re: TimYates]
      #21952 - 09/19/05 06:25 PM

Hi Tim,

Excellent work javascript:void(0) . The use of AffineTransformation for achieving symmetry is a great technique.

I'm going to start introducing Swing applets with the next installment of Java Fun and Games, but will also continue to include AWT applets from time to time.

Cheers.

Jeff

Edited by JavaWorld (09/19/05 11:28 PM)


Post Extras: Print Post   Remind Me!   Notify Moderator  
TimYates
Unregistered




Re: Swing version [Re: Jeff Friesen]
      #21955 - 09/20/05 05:44 AM

So glad you like it I always worry before posting stuff like that (I didn't want to get you riled, or post something incorrect)

I look forward to your Swing applets

Keep up the good work Jeff!

Tim Yates.


Post Extras: Print Post   Remind Me!   Notify Moderator  
Pages: 1



Extra information
0 registered and 1 anonymous users are browsing this forum.

Moderator:   

Print Topic

Forum Permissions
      You cannot start new topics
      You cannot reply to topics
      HTML is disabled
      UBBCode is enabled

Rating:
Topic views: 6346

Rate this topic

Jump to

Contact us JavaWorld

Powered by UBB.threads™ 6.5.5