BufferedImage in Java [] (BufferedImage in Java), Lektion, Seite 722839
https://www.purl.org/stefan_ram/pub/bufferedimage_java (Permalink) ist die kanonische URI dieser Seite.
Stefan Ram

BufferedImage in Java 

Main.java

class Main extends javax.swing.JComponent implements java.lang.Runnable
{ final int[] X ={ 800, 800 };

private static final long serialVersionUID = 0L;

final java.awt.image.BufferedImage onScreenImage
= new java.awt.image.BufferedImage
( X[ 1 ], X[ 0 ], java.awt.image.BufferedImage.TYPE_INT_RGB );

@java.lang.Override public void paintComponent( final java.awt.Graphics graphics )
{ graphics.drawImage( Main.this.onScreenImage, 0, 0, null ); }

final java.awt.image.BufferedImage offScreenImage
= new java.awt.image.BufferedImage
( X[ 1 ], X[ 0 ], java.awt.image.BufferedImage.TYPE_INT_RGB );

public static int get( final int color, final int shift )
{ return (( color &( 0x000000FF << shift ))>> shift ); }

public static int B( final int color ){ return get( color, 0 ); }
public static int G( final int color ){ return get( color, 8 ); }
public static int R( final int color ){ return get( color, 16 ); }

public static void limit( final double[] a, final int i, final int max )
{ if( a[ i ]< 0 ){ a[ i ]= 0; return; }else
if( a[ i ]> max ){ a[ i ]= max; return; }}

public static int color( final int r, final int g, final int b )
{ return ( r << 16 )|( g << 8 )| b; }

public static int color( final double[] C )
{ return color( ( int )C[ 0 ],( int )C[ 1 ],( int )C[ 2 ]); }

long l0 = -1L;
final long FINAL_PHASE = -1L;

@java.lang.Override public final void run()
{ final javax.swing.JFrame frame = new javax.swing.JFrame( this.getClass().getName() );
Main.this.setPreferredSize( new java.awt.Dimension( X[ 1 ], X[ 0 ]));
frame.setDefaultCloseOperation( javax.swing.JFrame.EXIT_ON_CLOSE );
frame.add( Main.this ); frame.pack(); frame.setVisible( true );

final java.lang.Runnable loop = new java.lang.Runnable()
{ double[] x ={ X[ 0 ]/2, X[ 1 ]/2 }; double[] C ={ 127, 127, 127 };
long t = 0; long b = 0; long c = 0;

public void drawRandomPictureToOnScreenImage()
{ /* draw a new random offScreenImage */
for( int i = 0; i < 100_000; ++i )
{ for( int j = 0; j < 2; ++j ){ x[ j ]+=( java.lang.Math.random() - 0.5 ); limit( x, j, X[ j ] - 1 ); }
for( int j = 0; j < 3; ++j ){ C[ j ]+=( java.lang.Math.random() - 0.5 ); limit( C, j, 255 ); }
Main.this.onScreenImage.setRGB(( int )x[ 1 ],( int )x[ 0 ], color( C )); }
final long l = java.lang.System.nanoTime();
if( l - t > 100_000L )
{ /* Paints the specified region in this component and all
of its descendants that overlap the region, immediately.
paintImmediately() needs to be called within EDT */
Main.this.paintImmediately
( 0, 0, java.lang.Integer.MAX_VALUE, java.lang.Integer.MAX_VALUE ); t = l; }}

public void mirrorPhase()
{ /* copy from off-screen Image to on-screen Image using a transform */
final long l = java.lang.System.nanoTime();
while( java.lang.System.nanoTime() - l < 100_000_000L )
{ final long l1 = java.lang.System.nanoTime(); if( l0 < 0 )l0 = l;
final double k = 0.5 - java.lang.Math.cos
( ( l1 - l0 )/ 1E9 %( 2 * java.lang.Math.PI )) / 2;
{ { /* Creates a Graphics2D, which can be used to draw into this BufferedImage. */
Main.this.onScreenImage.createGraphics(); }
final java.awt.Graphics2D currentGraphics =
( java.awt.Graphics2D )Main.this.onScreenImage.getGraphics();
currentGraphics.clearRect( 0, 0, X[ 1 ], X[ 0 ]);
currentGraphics.setTransform( new java.awt.geom.AffineTransform( 1 - 2 * k, 0, 0, 1, k * X[ 1 ], 0 ));
{ /* paint transform original into current */
currentGraphics.drawImage( Main.this.offScreenImage, 0, 0, null ); }}
/* Paints the specified region in this component and all
of its descendants that overlap the region, immediately.
paintImmediately() needs to be called within EDT */
Main.this.paintImmediately( 0, 0, java.lang.Integer.MAX_VALUE, java.lang.Integer.MAX_VALUE ); }}

public void run()
{ if( b == 0L )b = java.lang.System.nanoTime();
if( b != FINAL_PHASE )
{ if( java.lang.System.nanoTime() - b <= 10_000_000_000L )drawRandomPictureToOnScreenImage();
else
{ /* switch from initial phase to final phase */
{ /* copy from onScreenImage to offScreenImage */
onScreenImage.copyData( offScreenImage.getRaster() ); }
b = FINAL_PHASE; c = java.lang.System.nanoTime(); } }
else mirrorPhase();
java.awt.EventQueue.invokeLater( this ); }};

loop.run(); }

public static void main( final java.lang.String args[] )
{ java.awt.EventQueue.invokeLater( new Main() ); }}

Aussprachehinweis
close cloz

Seiteninformationen und Impressum   |   Mitteilungsformular  |   "ram@zedat.fu-berlin.de" (ohne die Anführungszeichen) ist die Netzpostadresse von Stefan Ram.   |   Eine Verbindung zur Stefan-Ram-Startseite befindet sich oben auf dieser Seite hinter dem Text "Stefan Ram".)  |   Der Urheber dieses Textes ist Stefan Ram. Alle Rechte sind vorbehalten. Diese Seite ist eine Veröffentlichung von Stefan Ram. Schlüsselwörter zu dieser Seite/relevant keywords describing this page: Stefan Ram Berlin slrprd slrprd stefanramberlin spellched stefanram722839 stefan_ram:722839 BufferedImage in Java Stefan Ram, Berlin, and, or, near, uni, online, slrprd, slrprdqxx, slrprddoc, slrprd722839, slrprddef722839, PbclevtugFgrsnaEnz Erklärung, Beschreibung, Info, Information, Hinweis,

Der Urheber dieses Textes ist Stefan Ram. Alle Rechte sind vorbehalten. Diese Seite ist eine Veröffentlichung von Stefan Ram.
https://www.purl.org/stefan_ram/pub/bufferedimage_java