- import java.awt.BorderLayout;
- import java.awt.EventQueue;
- import java.awt.Image;
- import javax.swing.ImageIcon;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.border.EmptyBorder;
- import javax.swing.GroupLayout;
- import javax.swing.GroupLayout.Alignment;
- import javax.swing.JLabel;
- import java.awt.event.KeyAdapter;
- import java.awt.event.KeyEvent;
- public class CRAB extends JFrame {
- private JPanel contentPane;
- private int x=150;
- private int y=150;
- /**
- * Launch the application.
- */
- public static void main(String[] args) {
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- try {
- CRAB frame = new CRAB();
- frame.setVisible(true);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
- /**
- * Create the frame.
- */
- public CRAB() {
- setTitle("\u6309\u65B9\u5411\u9375\u79FB\u52D5\u5716\u5F62");
- ImageIcon icon = new ImageIcon(CRAB.class.getResource("icon.png"));
- Image img = icon.getImage().getScaledInstance(100,100,Image.SCALE_FAST);
- setIconImage(img);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setBounds(100, 100, 400, 400);
- contentPane = new JPanel();
- contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
- setContentPane(contentPane);
- contentPane.setLayout(null);
- ImageIcon imgbaby = new ImageIcon(CRAB.class.getResource("crab.png"));
- Image baby = imgbaby.getImage().getScaledInstance(100,100,Image.SCALE_FAST);
- ImageIcon babyicon = new ImageIcon(baby);
- JLabel label = new JLabel(babyicon);
- label.setBounds(150, 150, 100, 100);
- contentPane.add(label);
-
- addKeyListener(new KeyAdapter()
- {
- @Override
- public void keyPressed(KeyEvent arg0)
- {
- if(arg0.isShiftDown())
- {
- if(arg0.getKeyCode() == KeyEvent.VK_UP)
- {
- y+=5;
- ImageIcon imgbaby = new ImageIcon(CRAB.class.getResource("crab-2.png"));
- Image baby = imgbaby.getImage().getScaledInstance(100,100,Image.SCALE_FAST);
- ImageIcon babyicon = new ImageIcon(baby);
-
- label.setIcon(babyicon);
- }
- if(arg0.getKeyCode() == KeyEvent.VK_DOWN)
- {
- y-=5;
- ImageIcon imgbaby = new ImageIcon(CRAB.class.getResource("crab.png"));
- Image baby = imgbaby.getImage().getScaledInstance(100,100,Image.SCALE_FAST);
- ImageIcon babyicon = new ImageIcon(baby);
- label.setIcon(babyicon);
-
- }
- if(arg0.getKeyCode() == KeyEvent.VK_LEFT)
- {
- x+=5;
- ImageIcon imgbaby = new ImageIcon(CRAB.class.getResource("crab-1.png"));
- Image baby = imgbaby.getImage().getScaledInstance(100,100,Image.SCALE_FAST);
- ImageIcon babyicon = new ImageIcon(baby);
-
- label.setIcon(babyicon);
- }
- if(arg0.getKeyCode() == KeyEvent.VK_RIGHT)
- {
- x-=5;
- ImageIcon imgbaby = new ImageIcon(CRAB.class.getResource("crab-3.png"));
- Image baby = imgbaby.getImage().getScaledInstance(100,100,Image.SCALE_FAST);
- ImageIcon babyicon = new ImageIcon(baby);
-
- label.setIcon(babyicon);
- }
- }
- else
- {
- if(arg0.getKeyCode() == KeyEvent.VK_UP)
- {
- y-=5;
- ImageIcon imgbaby = new ImageIcon(CRAB.class.getResource("crab.png"));
- Image baby = imgbaby.getImage().getScaledInstance(100,100,Image.SCALE_FAST);
- ImageIcon babyicon = new ImageIcon(baby);
-
- label.setIcon(babyicon);
-
- }
- if(arg0.getKeyCode() == KeyEvent.VK_DOWN)
- {
- y+=5;
- ImageIcon imgbaby = new ImageIcon(CRAB.class.getResource("crab-2.png"));
- Image baby = imgbaby.getImage().getScaledInstance(100,100,Image.SCALE_FAST);
- ImageIcon babyicon = new ImageIcon(baby);
-
- label.setIcon(babyicon);
-
- }
- if(arg0.getKeyCode() == KeyEvent.VK_LEFT)
- {
- x-=5;
- ImageIcon imgbaby = new ImageIcon(CRAB.class.getResource("crab-3.png"));
- Image baby = imgbaby.getImage().getScaledInstance(100,100,Image.SCALE_FAST);
- ImageIcon babyicon = new ImageIcon(baby);
-
- label.setIcon(babyicon);
-
- }
- if(arg0.getKeyCode() == KeyEvent.VK_RIGHT)
- {
- x+=5;
- ImageIcon imgbaby = new ImageIcon(CRAB.class.getResource("crab-1.png"));
- Image baby = imgbaby.getImage().getScaledInstance(100,100,Image.SCALE_FAST);
- ImageIcon babyicon = new ImageIcon(baby);
-
- label.setIcon(babyicon);
- }
- }
-
- label.setBounds(x,y,100,100);
- }
- });
- }
- }
複製代碼 |