?? catchthecreaturepanel.java
字號:
/*
@author Fei Deng
8 September 2005
*/
import java.util.Random;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.Timer;
public class CatchTheCreaturePanel extends JPanel
{
private Dimension area;
private Image image;
private Timer timer;
private final int PANEL_WIDTH = 800, PANEL_HEIGHT = 600;
private int positionX, positionY;
private Creature c;
public CatchTheCreaturePanel(Timer time, Image i)
{
//image = new ImageIcon("creature.gif");
//image = getImage("creature.gif");
//c = new Creature(image.getImage());
//image = i;
//setBackground(Color.WHITE);
c = new Creature(i);
timer = time;
timer.addActionListener(new RandomMover());
area = new Dimension(PANEL_WIDTH, PANEL_HEIGHT);
setPreferredSize(area);
setBackground(Color.PINK);
addMouseListener(new Catcher());
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
//image.paintIcon(this,g,c.creatureX,c.creatureY);
c.draw(this, g);
g.drawString(Integer.toString(c.getCatchCount()).toString(),PANEL_WIDTH/2, 10);
}
private class RandomMover implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
c.move(area);
repaint();
}
}
private class Catcher implements MouseListener
{
public void mousePressed(MouseEvent event)
{
if (c.pointInMe((int)(event.getPoint()).getX(),(int)(event.getPoint()).getY()))
{
c.move(area);
repaint();
}
}
public void mouseClicked (MouseEvent event){}
public void mouseReleased (MouseEvent event) {}
public void mouseEntered (MouseEvent event) {}
public void mouseExited (MouseEvent event) {}
}
public static void main(String[] args)
{
Timer timer;
timer = new Timer(500,null);
timer.start();
ImageIcon image = new ImageIcon("T1.gif");
//image = getImage("creature.gif");
JFrame frame = new JFrame("Catch The Creature");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new CatchTheCreaturePanel(timer,image.getImage()));
frame.pack();
// position the frame in the middle of the screen
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension screenDimension = tk.getScreenSize();
Dimension frameDimension = frame.getSize();
frame.setLocation((screenDimension.width-frameDimension.width)/2,
(screenDimension.height-frameDimension.height)/2);
frame.show();
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -