1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
| import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.MemoryImageSource;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main extends JPanel implements Runnable
{
public static void main( String[] arg )
{
new Main();
}
private JFrame frame;
private MemoryImageSource img;
private Image image;
private int[] pixels;
private int width, height;
private Thread thread;
public Main()
{
frame = new JFrame();
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
width = 800;
height = 600;
pixels = new int[ width * height ];
img = new MemoryImageSource( width, height, pixels, 0, width );
img.setAnimated( true );
image = createImage( img );
setDoubleBuffered( false );
setPreferredSize( new Dimension( width, height ) );
frame.getContentPane().add( this );
thread = new Thread( this );
thread.start();
frame.pack();
frame.setVisible( true );
}
@Override
public void update( Graphics g )
{
paint( g );
}
@Override
public void paint( Graphics g )
{
g.drawImage( image, 0, 0, this );
}
@Override
public void run()
{
int red = ( 255 << 24 ) | ( 255 << 16 );
int green = ( 255 << 24 ) | ( 255 << 8 );
int iPixels = 0, iLigne = 0;
int taille = width * height;
int couleur = red;
int fond = ( 255 << 24 ) | ( 255 << 16 ) | ( 255 << 8 ) | 255;
int x1 = 0, y1 = 0;
long temps = System.currentTimeMillis();
while( true )
{
temps = System.currentTimeMillis();
for( iPixels = 0; iPixels < taille; ++iPixels )
{
pixels[ iPixels ] = fond;
}
for( iLigne = 0 ; iLigne < 45000 ; ++iLigne )
{
x1 = (int)(Math.random() * 700 ) + 50;
y1 = (int)( Math.random() * 500 ) + 50;
drawLine( x1, y1,
x1 + (int) ( Math.random() * 100 ) - 50,
y1 + (int) ( Math.random() * 100 ) - 50, couleur );
}
img.newPixels();
temps = System.currentTimeMillis() - temps;
if( couleur == red ) { couleur = green; }
else { couleur = red; }
System.out.println( temps );
try
{
Thread.sleep( 500 );
}
catch( Exception exc )
{
exc.printStackTrace();
}
}
}
int e = 0, inc1 = 0, inc2 = 0, dx = 0, dy = 0, incx = 0, incy = 0, x = 0, y = 0, iPoint = 0;
public void drawLine( int x1, int y1, int x2, int y2, int couleur )
{
dx = x2 - x1;
dy = y2 - y1;
if( dx < 0 ) dx = -dx;
if( dy < 0 ) dy = -dy;
incx = 1;
if( x2 < x1 ) incx = -1;
incy = 1;
if( y2 < y1 ) incy = -1;
x = x1;
y = y1;
if( dx > dy )
{
pixels[ x + y * width ] = couleur;
e = ( dy << 1 ) - dx;
inc1 = ( dy - dx ) << 1;
inc2 = dy << 1;
for( iPoint = 0; iPoint < dx; iPoint++ )
{
if( e >= 0 )
{
y += incy;
e += inc1;
}
else { e += inc2; }
x += incx;
pixels[ x + y * width ] = couleur;
}
}
else
{
pixels[ x + y * width ] = couleur;
e = ( dx << 1 ) - dy;
inc1 = ( dx - dy ) << 1;
inc2 = dx << 1;
for( iPoint = 0; iPoint < dy; iPoint++ )
{
if( e >= 0 )
{
x += incx;
e += inc1;
}
else e += inc2;
y += incy;
pixels[ x + y * width ] = couleur;
}
}
}
} |
Partager