Ecran de veille (ballet de ligne)

Description

ce programme sert a faire une petite ecran de veille avec javax.swing, il sera fermer lorsqu'il y a un mouvement de la souris ou un clique de clavier

Source / Exemple :


/* deux class : ThreadFrame.java et Main.java */

/**

  • @(#)ThreadFrame.java
* *
  • @author
  • @version 1.00 2007/10/23
  • /
import javax.swing.*; public class ThreadFrame extends Thread{ private JFrame cadre; private int[] x,y; private int width, height; private boolean[] incX,incY; public ThreadFrame(JFrame cadre, int[] x, int[] y) { this.cadre = cadre; this.x = x; this.y = y; width = cadre.getWidth(); height = cadre.getHeight(); //System.out.println (width); //System.out.println (height); incX = new boolean[4]; incY = new boolean[4]; incX[0] = true; incY[0] = false; incX[1] = false; incY[1] = true; incX[2] = true; incY[2] = false; incX[3] = false; incY[3] = true; /*for (int i = 0; i<4; i++) { if(Math.random()<=(double)1/2) { incX[i] = true; }else incX[i] = false; if(Math.random()<=(double)1/2) { incY[i] = true; }else incY[i] = false; System.out.println (incX[i]+" "+incY[i]); }*/ } public void run(){ while (true) { for(int i = 0; i<4; i++) { //System.out.println (x[i]+" "+y[i]); if(x[i]==width && incX[i]) { incX[i] = false; } if(x[i]==0 && !incX[i]) { incX[i] = true; } if(y[i]==height && incY[i]) { incY[i] = false; } if(y[i]==0 && !incY[i]) { incY[i] = true; } } for(int i = 0; i<4; i++) { if(incX[i]) { x[i]++; }else x[i]--; if(incY[i]) { y[i]++; }else y[i]--; } cadre.repaint(); try { this.sleep(1); }catch (Exception ex) { } } } } /******************************/ /**
  • @(#)Main.java
* *
  • @author AlphaCafe
  • @version 1.00 2007/10/23
  • /
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.math.*; public class Main extends JFrame implements KeyListener, MouseMotionListener, MouseListener { private Dimension sizeScreen = Toolkit.getDefaultToolkit().getScreenSize(); private Container con; private int Xmouse = -1,Ymouse = -1; private int screenWidth, screenHeight; private int[] x,y; public Main() { super(); screenWidth = sizeScreen.width; screenHeight = sizeScreen.height; x = new int[4]; y = new int[4]; x[0] = Math.round((float)(screenWidth*Math.random())); x[1] = Math.round((float)(screenWidth*Math.random())); x[2] = Math.round((float)(screenWidth*Math.random())); x[3] = Math.round((float)(screenWidth*Math.random())); y[0] = Math.round((float)(screenHeight*Math.random())); y[1] = Math.round((float)(screenHeight*Math.random())); y[2] = Math.round((float)(screenHeight*Math.random())); y[3] = Math.round((float)(screenHeight*Math.random())); //System.out.println (x1); con = this.getContentPane(); con.setLayout(null); setBackground(Color.black); setSize(sizeScreen.width, sizeScreen.height); setUndecorated(true); setVisible(true); addKeyListener(this); addMouseMotionListener(this); addMouseListener(this); ThreadFrame th = new ThreadFrame(this,x,y); th.start(); } public void paint(Graphics g) { g.setColor(Color.black); g.fillRect(0,0,screenWidth,screenHeight); g.setColor(Color.red); g.drawPolygon(x,y,4); } public void keyReleased(KeyEvent evt) { } public void keyPressed(KeyEvent evt) { System.exit(-1); } public void keyTyped(KeyEvent evt) { } public void mouseDragged(MouseEvent e) { } public void mouseMoved(MouseEvent e) { if(Xmouse == -1 && Ymouse == -1) { Xmouse = e.getX(); Ymouse = e.getY(); } else if(Xmouse != e.getX() || Ymouse!=e.getY()) System.exit(-1); } public void mouseClicked(MouseEvent e) { System.exit(-1); } public void mousePressed(MouseEvent e) { } public void mouseReleased(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public static void main (String[] args) { Main object = new Main(); } }

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.