java是一种可以撰写跨平台应用软件的面向对象的程序设计语言,是由Sun Microsystems公司于1995年5月推出的Java程序设计语言和Java平台(即JavaEE, JavaME, JavaSE)的总称。本站提供基于Java框架struts,spring,hibernate等的桌面应用、web交互及移动终端的开发技巧与资料

保持永久学习的心态,将成就一个优秀的你,来 继续搞起java知识。

博主是大三的在校大学生,学习计算机的路上,有些自己的感悟,写下来互相讨论下,欢迎吐槽

Java使用awt做的图形界面,使用多线程维护两个过程,一个是随机出现老鼠的事件,一个是判断规定时间内消失老鼠

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Cursor;
import java.awt.Event;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.InputEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.awt.im.InputContext;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import sun.awt.image.BufferedImageGraphicsConfig;
import sun.java2d.pipe.DrawImage;
import sun.print.BackgroundLookupListener;

class MouseFrame extends JFrame{
	//private JPanel imgpanel;
	//private JPanel backpanel;
	private ImageIcon background;
	private JFrame frame=new JFrame(&quot;<a href="http://www.cfei.net/archives/tag/%e6%89%93%e5%9c%b0%e9%bc%a0" title="浏览关于“打地鼠”的文章" target="_blank" class="tag_link">打地鼠</a>&quot;);
        //private JLabel scoreLabel;
	public static long a=0;
	
	public Thread t;
	public MouseFrame(){
		
		background=new ImageIcon(&quot;./img/back.png&quot;);  //获取图片
		/*JLabel label=new JLabel(background);         //背景图片显示在标签里面
		label.setBounds(0, 0, background.getIconWidth(), background.getIconHeight());  //标签填充
		
		backpanel =new JPanel();
		backpanel.add(new mouseCom());
		backpanel.setBounds(0, 0, background.getIconWidth(), background.getIconHeight());
		imgpanel=(JPanel)frame.getContentPane();     //把内容转换为JPanel
		imgpanel.setOpaque(false);                   //设置不透明  
		imgpanel.setLayout(new FlowLayout());        //设置流式布局
		frame.getLayeredPane().setLayout(null);      //把label 设置在最底层
		frame.getLayeredPane().add(label,new Integer(Integer.MIN_VALUE));
		frame.getLayeredPane().add(backpanel,new Integer(Integer.MAX_VALUE));
		//设置内容画板
		Container cp=frame.getContentPane();
		scoreLabel = new JLabel();                  // 显示分数的标签组件
        scoreLabel.setForeground(Color.RED);
        scoreLabel.setText(&quot;分数:&quot;+a);
        cp.add(scoreLabel);
		cp.add(backpanel);
		backpanel.setSize(600, 600);
        backpanel.add(new JButton(&quot;aaaa&quot;));
        //cp.add(new mouse());
        addMouseListener(new MouseAdapter() {

			@Override
			/*public void mouseClicked(MouseEvent e) {		
				
				if(current==null &amp;&amp; (e.getModifiers() &amp; InputEvent.BUTTON1_MASK)!=0 )          //处理鼠标的左键
					{
					System.out.println(&quot;11111&quot;);
					add(e.getPoint());
					}
				if(current!=null &amp;&amp; (e.getModifiers() &amp; InputEvent.BUTTON3_MASK)!=0)           //处理鼠标右键
			    	remove(current);
			}
		});*/
        //frame 其他设置
		
		
		
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(background.getIconWidth()+100,background.getIconHeight());
		frame.add(new mouseCom(background.getIconWidth(),background.getIconHeight()));
		t=new Thread(new ifmouse());
		t.start();
		frame.setResizable(false);
		frame.setVisible(true);
	    pack(); 
	}
	public static void main(String[] args) {
		new MouseFrame();	
	}
	
	
}
class mouse{
	public int imgx;
	public int imgy;
	public Image img;
	public int life=0;
	public mouse(int x,int y){
		this.imgx=x;
		this.imgy=y;
		img=new ImageIcon(&quot;./img/mouse.jpg&quot;).getImage();
	}
}
class ifmouse implements Runnable{
	public ArrayList&lt;mouse&gt; mice;
	public ifmouse(){
	    mice=mouseCom.mices();
	}
	public void addtime(int i){
            mice.get(i).life+=1;	  	
	}
	@Override
	public synchronized void run() {
			while(true){
				if(!mice.isEmpty()){
				   for(int i=0;i&lt;mice.size();i++){
					  if(mice.get(i).life==3){
						  mice.remove(i);
					      mouseCom.submice();
					  }
					  else
						  addtime(i);
				   }
				  }
				try {
					Thread.currentThread().sleep(500);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}	
			}
	}
}

	

class mouseCom extends JComponent implements Runnable{
	public static final int LENGTH=80;

    public static  ArrayList&lt;mouse&gt; mice;
    private mouse currmou;
 	private Image background;
 	private ImageIcon img;
 	private Image chuizi;
 	private int backhig;
 	private int backwei;
 	private Toolkit tk;
 	private Thread thread;
 	private JLabel score;
    private int succi=0;
 	private JLabel succ;
 	private static int fali=0;
  	private static JLabel fal;
 	
 	public static ArrayList&lt;mouse&gt; mices(){
 		return mice;
 	}
 	
 	
 	public static void submice() {
		
 		fali+=1;
		fal.setText(&quot;失败&quot;+fali);
	}

	public mouseCom(int hi,int we){
 		this.backhig=hi;
 		this.backwei=we;
 		
 		score=new JLabel();
 		score.setText(&quot;分数&quot;+MouseFrame.a);
 		score.setForeground(Color.RED);
 		score.setBounds(400, 50, 50, 50);
 		
 		succ=new JLabel();
 		succ.setText(&quot;成功&quot;+succi);
 		succ.setForeground(Color.RED);
 		succ.setBounds(400, 80, 50, 50);
 		
 		fal=new JLabel();
 		fal.setText(&quot;失败&quot;+fali);
 		fal.setForeground(Color.RED);
 		fal.setBounds(400, 100, 50, 50);
 		
 		background=new ImageIcon(&quot;./img/back.png&quot;).getImage();  //获取图片
 		img=new ImageIcon(&quot;./img/mouse.jpg&quot;);
 		tk=Toolkit.getDefaultToolkit();
 		chuizi=tk.getImage(&quot;./img/chuizi.jpg&quot;);		
 		mice=new ArrayList&lt;mouse&gt;();
 		add(score);
 		add(succ);
 		add(fal);
 		addComponentListener(new ComponentAction());
 		thread = new Thread(this);
 		
 		addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {		
				currmou=find(e.getPoint().x,e.getPoint().y);
				/*
				 * if(currmou==null &amp;&amp;(e.getModifiers() &amp; InputEvent.BUTTON1_MASK)!=0 )          //处理鼠标的左键
					{
					add(e.getPoint().x,e.getPoint().y);
					}
				*/
				if(currmou!=null &amp;&amp; (e.getModifiers() &amp; InputEvent.BUTTON3_MASK)!=0)           //处理鼠标右键
			    	remove(currmou);
			    	
			}
		});
		addMouseMotionListener(new MouseMotionListener(){
			@Override
			public void mouseMoved(MouseEvent e) {
				if(find(e.getPoint().x,e.getPoint().y)==null)
					setCursor(Cursor.getDefaultCursor());
				else{
					Cursor dy=tk.createCustomCursor(chuizi, new Point(10,10), &quot;dy stick&quot;);
				    setCursor(dy);
				}
			}

			@Override
			public void mouseDragged(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
		});
	}
 	
	public void paintComponent(Graphics g){ 
		g.drawImage(background, 0, 0, backhig,backwei,null);		
		for(mouse r:mice){
			g.drawImage(r.img, r.imgx,r.imgy,LENGTH,LENGTH,null);
            
		}
	}
	public mouse find(int fx,int fy){
		int x=fx-LENGTH/2;
		int y=fy-LENGTH/2;
		for(mouse r:mice){
			if((x-r.imgx)&lt;LENGTH/2 &amp;&amp; (y-r.imgy)&lt;LENGTH/2){
				return r;
			}
		}
		return null;
	}
	public void add(int fx,int fy){
		int x=fx;
		int y=fy;
		currmou=new mouse(x-LENGTH/2,y-LENGTH/2);
		mice.add(currmou);
		
		repaint();
	}
	public void remove(mouse s){
		if(s==null)
			return ;
		if(s==currmou)
			currmou=null;
		mice.remove(s);
		addscore();
		repaint();
	}
	public void addscore(){
		MouseFrame.a+=10;
		succi+=1;
		score.setText(&quot;分数&quot;+MouseFrame.a);
		succ.setText(&quot;成功&quot;+succi);
	}
	private final class ComponentAction extends ComponentAdapter {
		public void componentResized(final ComponentEvent e) {
			thread.start();// 线程启动
		}
	}
	@Override
	public synchronized void run() {
		try {
			while(true){
				Random rand=new Random();
				int i=rand.nextInt(6);
				if(i==0){
					if(find(80,50)==null)
						add(80,50);
				}
				else if(i==1){
					if(find(200,50)==null)
						add(200,50);
				}
				else if(i==2){
					if(find(320,50)==null)
						add(320,50);
				}
				else if(i==3){
					if(find(80,150)==null)
						add(80,150);
				}
				else if(i==4){
					if(find(200,150)==null)
						add(200,150);
				}
				else if(i==5){
					if(find(320,150)==null)
						add(320,150);
				}
				Thread.currentThread().sleep(500);	
			}
			
		} catch (InterruptedException e) {
			// TODO: handle exception
		}
	}
}

java多线程打地鼠

因为水平有限,难免有疏忽或者不准确的地方,希望大家能够直接指出来,我会及时改正。一切为了知识的分享。

后续会有更多的精彩的内容分享给大家。