如何用java写一个屏保程序

如何用java写一个屏保程序,第1张

看看这个试试!

import java.text.*

import java.awt.*

import javax.swing.*

import java.awt.geom.*

import java.util.*

import java.awt.event.*

/**

*

*

*/

public class ScreenTest extends JPanel implements Runnable{

/** Creates a new instance of ScreenTest */

private String time

private Rectangle2D rd

private int x,y,a,b//座标值和方向值

private int rgb=0//颜色的值

private Color color//字体的颜色

private int width,height//屏幕的大小

public ScreenTest(int width,int height) {

this.width=width

this.height=height

initWindow()

}

private void initWindow(){

x=(int)(Math.random()*300)

y=(int)(Math.random()*500)

a=1

b=1

this.setBackground(Color.BLACK)

this.setOpaque(true)

rd=new Rectangle2D.Double(10,10,101,10)

color=Color.YELLOW

}

private void doColor(){

rgb=(int)(Math.random()*0xFFFFFF)

color=new Color(rgb)

}

public void paintComponent(Graphics g){

g.setColor(Color.BLACK)

g.fillRect(0,0,width,height)

g.setColor(color)

g.setFont(new Font("楷书",Font.BOLD,150))

FontMetrics fm=g.getFontMetrics()

rd=fm.getStringBounds(time,g)

g.drawString(time,x,(int)(y+rd.getHeight()))

}

private void doTime(){

Calendar cal=Calendar.getInstance()

DateFormat df=DateFormat.getTimeInstance(DateFormat.MEDIUM)

Date date=cal.getTime()

time=df.format(date)

x+=a

y+=b

double width1=rd.getWidth()

double height1=rd.getHeight()

rd.setRect(x,y,width1,height1)

if(rd.intersectsLine(width,0,width,height)){

doColor()

a=-1

}

else if(rd.intersectsLine(0,0,0,height)){

doColor()

a=1

}

else if(rd.getY()<=-80){

doColor()

b=1

}

else if(rd.intersectsLine(0,height,width,height)){

doColor()

b=-1

}

}

public void run(){

while(true){

try{

Thread.sleep(2)

doTime()

repaint()

}

catch(InterruptedException ie){

ie.printStackTrace()

}

}

}

public static void main(String arsg[]){

GraphicsDevice gd=GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice()

DisplayMode dm=gd.getDisplayMode()

int width=dm.getWidth()

int height=dm.getHeight()

System.out.println("width="+width+"\nheight="+height)

ScreenTest st=new ScreenTest(width,height)

final JFrame jf=new JFrame()

jf.getContentPane().add(st,BorderLayout.CENTER)

jf.setUndecorated(true)

gd.setFullScreenWindow(jf)

new Thread(st).start()

jf.addKeyListener(new KeyAdapter(){

public void keyReleased(KeyEvent ke){

if(ke.getKeyCode()==KeyEvent.VK_NUMPAD0)

System.exit(0)

}

})

}

}

import java.awt.Color

import java.awt.Graphics

import javax.swing.JFrame

public class app extends JFrame{

public void paint(Graphics g){

for (int i=0i<100i++){

Color c = new Color((int) (Math.random() * 255), (int) (Math

.random() * 255), (int) (Math.random() * 255))

g.setColor(c)

int srcX=(int)(Math.random() * 800)

int srcY=(int)(Math.random() * 600)

int destX=(int)(Math.random() * 800)

int destY = (int)(Math.random() * 600)

g.drawLine(srcX, srcY, destX, destY)

try {

Thread.sleep(1000)

} catch (InterruptedException e) {

// TODO 自动生成 catch 块

e.printStackTrace()

}

}

}

public app(){

this.setSize(800,600)

this.setVisible(true)

}

public static void main(String[] args) {

new app()

}

import java.awt.Color

import java.awt.Graphics

import javax.swing.JFrame

public class app extends JFrame{

public void paint(Graphics g){

for (int i=0i<100i++){

Color c = new Color((int) (Math.random() * 255), (int) (Math

.random() * 255), (int) (Math.random() * 255))

g.setColor(c)

int srcX=(int)(Math.random() * 800)

int srcY=(int)(Math.random() * 600)

int destX=(int)(Math.random() * 800)

int destY = (int)(Math.random() * 600)

g.drawLine(srcX, srcY, destX, destY)

try {

Thread.sleep(1000)

} catch (InterruptedException e) {

//册庆 TODO 自动生轿雀成 catch 块州帆握

e.printStackTrace()

}

}

}

public app(){

this.setSize(800,600)

this.setVisible(true)

}

public static void main(String[] args) {

new app()

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/yw/12266944.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-22
下一篇2023-05-22

发表评论

登录后才能评论

评论列表(0条)

    保存