| |
Cette classe dessinne simplement un dégradé noir blanc
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Degrade extends JPanel {
private static final long serialVersionUID = 6899161860200488093L ;
public Degrade (){
super ();
setOpaque (false );
setBackground (Color.white);
setPreferredSize (new Dimension (800 ,255 ));
}
public static void main (String[] args) {
JFrame frame = new JFrame (" Dégradé " );
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.setContentPane (new Degrade ());
frame.pack ();
frame.setVisible (true );
}
public void paint (Graphics g) {
g.setColor (Color.white);
g.fillRect (0 , 0 , getWidth (), getHeight ());
for (int i = 0 ; i < 255 ; i+ + ){
g.setColor (new Color (i, i, i));
g.drawLine (0 , i, getWidth (), i);
}
super .paint (g);
}
}
|
|
| |
Cette classe permet de dessinner une simple fractale carrée
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Fractale extends JPanel {
private static final long serialVersionUID = 6899161860200488093L ;
public Fractale (){
super ();
setOpaque (false );
setBackground (Color.white);
setPreferredSize (new Dimension (255 ,255 ));
}
public static void main (String[] args) {
JFrame frame = new JFrame (" Fractale " );
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.setContentPane (new Fractale ());
frame.pack ();
frame.setVisible (true );
}
public void paint (Graphics g) {
g.setColor (Color.white);
g.fillRect (0 , 0 , getWidth (), getHeight ());
for (int x= 0 ; x<= 255 ; x+ + ){
for (int y= 0 ; y<= 255 ; y+ + ){
g.setColor (new Color (x& y, x& y, x& y));
g.drawLine (x, y, x, y);
}
}
super .paint (g);
}
}
|
|
| |
Cette classe dessinne une fractale obtenue un peu au hasard, mais le résultat est plutôt joli.
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Louche extends JPanel {
private static final long serialVersionUID = 6899161860200488093L ;
public Louche (){
super ();
setOpaque (false );
setBackground (Color.white);
setPreferredSize (new Dimension (512 ,512 ));
}
public static void main (String[] args) {
JFrame frame = new JFrame (" Louche " );
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.setContentPane (new Louche ());
frame.pack ();
frame.setVisible (true );
}
public void paint (Graphics g) {
g.setColor (Color.white);
g.fillRect (0 , 0 , getWidth (), getHeight ());
for (int y= 0 ; y< 512 ; y+ + ) {
for (int x= 0 ; x< 512 ; x+ + ) {
int mult = x * y % 256 ;
g.setColor (new Color (mult, mult, mult));
g.drawLine (x, y, x, y);
}
}
super .paint (g);
}
}
|
|
| |
Cette classe desinne un grand nombre de trait sur l'écran pour former un nuage.
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
@author
public class Nuage extends JPanel {
private static final long serialVersionUID = 6899161860200488093L ;
private final int maxRGB = 255 ;
private final int space = 3 ;
public Nuage (){
super ();
setOpaque (false );
setBackground (Color.white);
setPreferredSize (new Dimension (800 ,800 ));
}
public static void main (String[] args) {
JFrame frame = new JFrame (" Nuage " );
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.setContentPane (new Nuage ());
frame.pack ();
frame.setVisible (true );
}
public void paint (Graphics g) {
g.setColor (Color.black);
g.fillRect (0 , 0 , getWidth (), getHeight ());
int x = 0 ;
int y = 0 ;
Random random = new Random ();
while (x < getWidth ()){
while (y < getHeight ()){
g.setColor (new Color (random.nextInt (maxRGB), random.nextInt (maxRGB), random.nextInt (maxRGB)));
g.drawLine (x, y, x + random.nextInt (15 ) + 1 , y + random.nextInt (15 ) + 1 );
y + = (random.nextInt (space) + 1 );
}
y = 0 ;
x + = (random.nextInt (space) + 1 );
}
super .paint (g);
}
}
|
|
| |
Cette classe dessinne une sorte de soleils. Je l'ai obtenu au hasard, mais je trouvais que le résultat donnait bien.
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class SoleilLouche extends JPanel {
private static final long serialVersionUID = 6899161860200488093L ;
public SoleilLouche (){
super ();
setOpaque (false );
setBackground (Color.white);
setPreferredSize (new Dimension (512 ,512 ));
}
public static void main (String[] args) {
JFrame frame = new JFrame (" Soleil louche " );
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.setContentPane (new SoleilLouche ());
frame.pack ();
frame.setVisible (true );
}
public void paint (Graphics graphics) {
graphics.setColor (Color.white);
graphics.fillRect (0 , 0 , getWidth (), getHeight ());
int r = 0 ;
int g = 0 ;
int b = 0 ;
for (int i = 0 ; i < getWidth (); i+ + ){
if (r = = 255 ){
if (b >= 1 ){
b- - ;
} else if (g = = 255 ){
r- - ;
} else {
g+ + ;
}
} else if (g = = 255 ){
if (b = = 255 ){
g- - ;
} else if (r = = 0 ){
b+ + ;
} else {
r- - ;
}
} else if (b = = 255 ){
if (r = = 255 )
b- - ;
} else if (g = = 0 ){
r+ + ;
} else {
g- - ;
}
graphics.setColor (new Color (r, g, b));
graphics.drawOval (0 , 0 , i, i);
}
super .paint (graphics);
}
}
|
|
| |
Cette classe dessinne un grand nombre de carré qu'elle fait tourner pour former un treillis.
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
@author
public class Treillis extends JPanel {
private static final long serialVersionUID = 6899161860200488093L ;
public final int nombre = 250 ;
public Treillis (){
super ();
setOpaque (false );
setBackground (Color.white);
setPreferredSize (new Dimension (800 ,800 ));
}
public static void main (String[] args) {
JFrame frame = new JFrame (" Treillis " );
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.setContentPane (new Treillis ());
frame.pack ();
frame.setVisible (true );
}
public void paint (Graphics g) {
g.setColor (Color.black);
g.fillRect (0 , 0 , getWidth (), getHeight ());
Random random = new Random ();
for (int i = 0 ; i <= getWidth (); i + = (getWidth () / (nombre + 1 ))){
g.setColor (new Color (random.nextInt (255 ), random.nextInt (255 ), random.nextInt (255 )));
int [] x = { i, getWidth (), getWidth () - i, 0 } ;
int [] y = { 0 , i, getWidth (), getWidth () - i} ;
g.drawPolygon (x, y, 4 );
}
super .paint (g);
}
}
|
|
Consultez les autres pages sources
|