码迷,mamicode.com
首页 > 编程语言 > 详细

awt frame - java 窗口

时间:2017-03-28 15:47:31      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:title   net   plane   exception   java   pen   err   listen   open   

  1 package com.law.frame;
  2 
  3 import java.awt.Color;
  4 import java.awt.Font;
  5 import java.awt.Frame;
  6 import java.awt.Graphics;
  7 import java.awt.Image;
  8 import java.awt.event.WindowAdapter;
  9 import java.awt.event.WindowEvent;
 10 import java.net.URL;
 11 
 12 public class MyWindow extends Frame{
 13 
 14     /**
 15      * 
 16      */
 17     private static final long serialVersionUID = 2490169038285678148L;
 18     
 19     private int x;
 20     private int y;
 21     private boolean flag;
 22     
 23 
 24     public MyWindow(){
 25         
 26     }
 27     public MyWindow(String title){
 28         super(title);
 29     }
 30 
 31     public void openMyWindow(){
 32         setBackground(new Color(0, 0, 0));
 33         setSize(500, 500);
 34         setLocation(250, 250);
 35         setVisible(true);
 36         this.addWindowListener(new WindowAdapter() {
 37             public void windowClosing(WindowEvent e){
 38                 System.exit(0);
 39                 }
 40         });
 41         new RepaintThread().start();
 42     }
 43     
 44 
 45 
 46     @Override
 47     public void paint(Graphics g) {
 48         g.setColor(Color.white);
 49         Font font = new Font("Candara", Font.BOLD, 30);
 50         g.setFont(font);
 51         g.drawString("Java        Concurrency", 130, 150);
 52         URL u = this.getClass().getClassLoader().getResource("images/Plane_32px_1194817_easyicon.net.png");
 53         Image img = Common.loadPic(u);
 54         g.drawImage(img, x, 350, null);
 55     }
 56     
 57     class RepaintThread extends Thread{
 58 
 59         @Override
 60         public void run() {
 61             while(true){
 62                 changePositionX();
 63                 repaint();
 64                 try {
 65                     Thread.sleep(60);
 66                 } catch (InterruptedException e) {
 67                     e.printStackTrace();
 68                 }
 69             }
 70         }
 71         
 72     }
 73     public void changePositionX(){
 74         if(x>=500-32){
 75             flag = false;
 76         }
 77         if(x<=0){
 78             flag = true;
 79         }
 80         if(flag){
 81             x += 5;
 82         }else{
 83             x -= 5;
 84         }
 85     }
 86     public void changePositionY(){
 87         if(y>=500-32){
 88             flag = false;
 89         }
 90         if(y<=0){
 91             flag = true;
 92         }
 93         if(flag){
 94             y += 5;
 95         }else{
 96             y -= 5;
 97         }
 98     }
 99     public static void main(String[] args) {
100         MyWindow window = new MyWindow("open open");
101         window.openMyWindow();
102     }
103     
104     public int getX() {
105         return x;
106     }
107     public void setX(int x) {
108         this.x = x;
109     }
110     public int getY() {
111         return y;
112     }
113     public void setY(int y) {
114         this.y = y;
115     }
116     public boolean isFlag() {
117         return flag;
118     }
119     public void setFlag(boolean flag) {
120         this.flag = flag;
121     }
122     
123 }

加载图片的工具类:

 1 package com.law.frame;
 2 
 3 import java.awt.Image;
 4 import java.io.IOException;
 5 import java.net.URL;
 6 
 7 import javax.imageio.ImageIO;
 8 
 9 
10 public class Common {
11     
12     
13     public static Image loadPic(URL u){
14         Image img = null;
15         try {
16             img = ImageIO.read(u);
17         } catch (IOException e) {
18             e.printStackTrace();
19         }
20         return img;
21     }
22 }

 

awt frame - java 窗口

标签:title   net   plane   exception   java   pen   err   listen   open   

原文地址:http://www.cnblogs.com/xiao-fy/p/6634941.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!