• java中image显示图片,随上下左右键移动


     1 import java.awt.*;
     2 import java.awt.event.*;
     3 
     4 class FTest {
     5   public static void main(String[] args){
     6      final MyFrame form = new MyFrame();
     7     // form.setBounds(100, 100, 500, 500);
     8      
     9      form.setMyFullScreen();
    10      form.addWindowListener(new Wclose());
    11      form.addKeyListener(new KeyAdapter(){
    12         public void keyPressed(KeyEvent e){
    13             
    14             form.w=form.getWidth();//获取当前窗口的大小
    15             form.h=form.getHeight();
    16             if(e.getKeyCode()==KeyEvent.VK_DOWN){
    17                 if(form.p.y < form.h-form.imH){
    18                     form.repaint(form.p.x,form.p.y,form.imW, form.StepLength);
    19                     form.p.y=form.p.y+form.StepLength;
    20                     
    21                 }
    22             }
    23             else if(e.getKeyCode()==KeyEvent.VK_UP){
    24                 if(form.p.y>form.getX()){
    25                     form.repaint(form.p.x,form.p.y+form.imH-form.StepLength,form.imW, form.StepLength);
    26                     form.p.y-=form.StepLength;
    27                 }
    28             }
    29             else if(e.getKeyCode()==KeyEvent.VK_LEFT){
    30                 if(form.p.x>form.getY()){
    31                     form.repaint(form.p.x+form.imW-form.StepLength,form.p.y,form.StepLength, form.imH);
    32                     form.p.x-=form.StepLength;
    33                 }
    34             }
    35             else if(e.getKeyCode()==KeyEvent.VK_RIGHT){
    36                 if(form.p.x<form.w-form.imW){
    37                     form.repaint(form.p.x,form.p.y,form.StepLength, form.imH);
    38                     form.p.x+=form.StepLength;
    39                 }
    40             }
    41             form.paint(form.getGraphics());//重绘,调用的是重写的paint
    42         }
    43     });
    44      form.setVisible(true);
    45   }
    46 }
    47 
    48 class MyFrame extends Frame {
    49   Image mImages;//定义了两张图片对象
    50   Image mImages2;
    51   int StepLength=10;
    52   int imH=100;//图像的大小
    53   int imW=100;
    54   int h=300;//当前的窗口的大小
    55   int w=400;
    56   Point p=new Point(20,20);//用来记录图像左上方定点的位置
    57  // Point pF=new Point(0,0);
    58   MyFrame(){
    59     setSize(h,w);
    60     
    61     mImages = getToolkit().getImage("ap1.jpg");//创建图片对象
    62     mImages2 = getToolkit().getImage("ap2.jpg");
    63     
    64     //java加载图象是异步的。也就是说,在你执行到getwidth时,它还不一定读完图象。所以当然是-1了。 
    65     //可以用下面的程序等待加载 
    66     MediaTracker   media   =   new   MediaTracker(this); 
    67     media.addImage(mImages2,1); 
    68     try{media.waitForID(1);}
    69     catch(InterruptedException e){
    70     }
    71     //如果没有上面这段程序,imH,和imW就会是-1
    72     imH=mImages2.getHeight(null);
    73     imW=mImages2.getWidth(null);
    74   }
    75   public void paint(Graphics g){
    76     g.drawImage(mImages2,p.x,p.y,imW,imH,this);
    77   }
    78   
    79   public void setMyFullScreen(){
    80       //把当前frame设置成全屏
    81       Dimension   screenSize=this.getToolkit().getScreenSize(); 
    82       this.setSize(screenSize.width,screenSize.height); 
    83       this.setUndecorated(true); 
    84       this.setExtendedState(Frame.MAXIMIZED_BOTH);
    85   }
    86 }
    87 
    88 class Wclose extends WindowAdapter {
    89     public void windowClosing(WindowEvent e) {
    90     System.exit(0);
    91     }
    92 }
  • 相关阅读:
    jquery validate --转载
    领域驱动设计之领域模型--转载
    为system对象添加扩展方法
    DDD开源框架
    浅谈命令查询职责分离(CQRS)模式---转载
    AutoMapper小结
    执行后台任务的利器——Hangfire
    单元测试框架
    内存中的堆和栈
    ++*p,(*p)++,*p++与*++p四者的区别
  • 原文地址:https://www.cnblogs.com/redlight/p/2542942.html
Copyright © 2020-2023  润新知