for(int i=0;i<60;i++) { int
xStart=(int)(xCenter+0.95*clockRadius*Math.sin(Math.PI/30*i)); int
yStart=(int)(yCenter-0.95*clockRadius*Math.cos(Math.PI/30*i)); int
xEnd=(int)(xCenter+clockRadius*Math.sin(Math.PI/30*i)); int
yEnd=(int)(yCenter-clockRadius*Math.cos(Math.PI/30*i));
g.setColor(Color.black);
g.drawLine(xStart,yStart,xEnd,yEnd); }
int sLength=(int)(clockRadius*0.8); int
xSecond=(int)(xCenter+sLength*Math.sin(second*(2*Math.PI/60)));
int
ySecond=(int)(yCenter-sLength*Math.cos(second*(2*Math.PI/60)));
g.setColor(Color.red);
g.drawLine(xCenter,yCenter,xSecond,ySecond); int mLength = (int)(clockRadius * 0.60);
int xMinute = (int)(xCenter + mLength *Math.sin(minute * (2 * Math.PI / 60)));
int yMinute = (int)(yCenter - mLength *Math.cos(minute * (2 * Math.PI / 60)));
g.setColor(Color.blue);
g.drawLine(xCenter, yCenter, xMinute, yMinute); int hLength = (int)(clockRadius * 0.45);
int xHour = (int)(xCenter + hLength *Math.sin((hour % 12 + minute / 60.0) * (2 * Math.PI / 12)));
int yHour = (int)(yCenter - hLength *Math.cos((hour % 12 + minute / 60.0) * (2 * Math.PI / 12)));
g.setColor(Color.green);
g.drawLine(xCenter, yCenter, xHour, yHour); g.setColor(Color.blue);
g.setFont(new Font(\ g.drawString(hour+\,getHeight()-10); }
public void setCurrentTime() {
Calendar calendar=new GregorianCalendar();
this.hour=calendar.get(Calendar.HOUR_OF_DAY); this.minute=calendar.get(Calendar.MINUTE); this.second=calendar.get(Calendar.SECOND); }
public Dimension getPreferredSize() {
return new Dimension(300,300); } }
//KeyListenerDemo类 import java.awt.*;
import java.awt.event.*; import javax.swing.*;
public class KeyListenerDemo extends JFrame implements KeyListener {
Image playerImage[]=new Image[4]; Image player; Toolkit tool;
int x=200,y=200;
public KeyListenerDemo() {
super(\ tool=getToolkit();
playerImage[0]=tool.getImage(\ playerImage[1]=tool.getImage(\ playerImage[2]=tool.getImage(\ playerImage[3]=tool.getImage(\ player=playerImage[0]; addKeyListener(this); this.setSize(600,600); this.setVisible(true); }
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.WHITE); g.fillRect(0,0,650,650);
g.drawImage(player,x,y,150,150,this); }
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_UP: player=playerImage[0]; if(y-32<0) break; else y-=10; repaint(); break;
case KeyEvent.VK_DOWN: player=playerImage[1]; if(y+48>400) break; else y+=10; repaint(); break;
case KeyEvent.VK_LEFT:
player=playerImage[2]; if(x-2<0) break; else x-=10; repaint(); break;
case KeyEvent.VK_RIGHT: player=playerImage[3]; if(x+48>400) break; else x+=10; repaint();
break; } }
public void keyTyped(KeyEvent e){} ; public void keyReleased(KeyEvent e){}; }
// AnimationDemo类 import javax.swing.*; import java.awt.event.*; import java.awt.*;
public class AnimationDemo extends JFrame { public AnimationDemo(){
ImageIcon imageIcon =new ImageIcon(\国旗.jpg\ Image image =imageIcon.getImage(); add(new MovingImagePanel(image)); }
static class MovingImagePanel extends JPanel{ ImageIcon imageIcon =new ImageIcon(); Image image =imageIcon.getImage(); private int xCoordinate=0; private int yCoordinate=500;
public MovingImagePanel(Image image){
this.image=image; }
public void paintComponent(Graphics g){ super.paintComponent(g);
ImageIcon imageIcon =new ImageIcon(\罪人.gif\ Image image2 =imageIcon.getImage();
g.drawImage(image2,xCoordinate+380,600-yCoordinate,200,160,this);
g.setColor(Color.GREEN);
g.setFont(new Font(\华文行楷\ g.drawString(\打倒小日本\ if(image!=null) {
if(yCoordinate<-150){ yCoordinate=610; }
yCoordinate-=1;
g.drawImage(image,xCoordinate,yCoordinate,210,180,this); if(yCoordinate<580&&yCoordinate>500) {
g.setColor(Color.RED); g.setFont(new Font(\方正舒体\
g.drawString(\中国万岁\ }
if(yCoordinate<430&&yCoordinate>340) {
g.setColor(Color.BLUE); g.setFont(new Font(\华文彩云\
g.drawString(\中国万岁\
百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库java多功能选择器(5)在线全文阅读。
相关推荐: