博主是大三的在校大学生,学习计算机的路上,有些自己的感悟,写下来互相讨论下,欢迎吐槽
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("打地鼠");
//private JLabel scoreLabel;
public static long a=0;
public Thread t;
public MouseFrame(){
background=new ImageIcon("./img/back.png"); //获取图片
/*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("分数:"+a);
cp.add(scoreLabel);
cp.add(backpanel);
backpanel.setSize(600, 600);
backpanel.add(new JButton("aaaa"));
//cp.add(new mouse());
addMouseListener(new MouseAdapter() {
@Override
/*public void mouseClicked(MouseEvent e) {
if(current==null && (e.getModifiers() & InputEvent.BUTTON1_MASK)!=0 ) //处理鼠标的左键
{
System.out.println("11111");
add(e.getPoint());
}
if(current!=null && (e.getModifiers() & 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("./img/mouse.jpg").getImage();
}
}
class ifmouse implements Runnable{
public ArrayList 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
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 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 mices(){
return mice;
}
public static void submice() {
fali+=1;
fal.setText("失败"+fali);
}
public mouseCom(int hi,int we){
this.backhig=hi;
this.backwei=we;
score=new JLabel();
score.setText("分数"+MouseFrame.a);
score.setForeground(Color.RED);
score.setBounds(400, 50, 50, 50);
succ=new JLabel();
succ.setText("成功"+succi);
succ.setForeground(Color.RED);
succ.setBounds(400, 80, 50, 50);
fal=new JLabel();
fal.setText("失败"+fali);
fal.setForeground(Color.RED);
fal.setBounds(400, 100, 50, 50);
background=new ImageIcon("./img/back.png").getImage(); //获取图片
img=new ImageIcon("./img/mouse.jpg");
tk=Toolkit.getDefaultToolkit();
chuizi=tk.getImage("./img/chuizi.jpg");
mice=new ArrayList();
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 &&(e.getModifiers() & InputEvent.BUTTON1_MASK)!=0 ) //处理鼠标的左键
{
add(e.getPoint().x,e.getPoint().y);
}
*/
if(currmou!=null && (e.getModifiers() & 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), "dy stick");
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)
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("分数"+MouseFrame.a);
succ.setText("成功"+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
}
}
}