- // resize image + make imagebutton
- JPanel panel = new JPanel();
- panel.setLayout(null); // set 了layout是null後必需調用setBounds
- panel.setBounds(0, 0, 200 , 200); // 參數1: x座標; 2:y座標; 3:width; 4:height
- ImageIcon img = new ImageIcon(getClass().getResource("/lam/images/1.jpg"));
- // resize img width and height to 100,100
- Image image = img.getImage();
- Image newimg = image.getScaledInstance(100, 100, java.awt.Image.SCALE_SMOOTH);
- img = new ImageIcon(newimg);
- // add image to button
- JButton btn = new JButton();
- btn.setBorder(LineBorder.createGrayLineBorder());
- btn.setIcon(img);
- btn.setBounds(0, 0, 150, 120);
- panel.add(btn);
2015年2月19日 星期四
[Java] resize image + imagebutton
2015年2月9日 星期一
[java] 字母貪食蛇小遊戲 - Object、GUI、keylistener的應用
字母貪食蛇小遊戲 - Object、GUI、KeyListener的應用。
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package snakegui;
- /**
- *
- * @author kongyinlam
- */
- import java.awt.*;
- import javax.swing.*;
- import java.awt.event.*;
- import java.util.*;
- public class SnakeGUI {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- SnakeGame sg = new SnakeGame(20,30);
- sg.play('w');
- }
- }
- class SnakeGame implements KeyListener{
- Board b;
- ArrayList<Token> tokens;
- int py, px;
- boolean isEaten=false;
- public SnakeGame(int h, int w){
- b = new Board(h, w, this);
- tokens = new ArrayList<>();
- for(int i=0; i<4; i++){
- Token token = new Token(this, b, tokens.size());
- token.setInitPos();
- tokens.add(token);
- }
- }
- public void play(char key){
- b.clearBoard();
- if(isEaten) eaten();
- for(Token tokenss: tokens)
- tokenss.MoveToken(key);
- b.paintBoard();
- }
- public void eaten(){
- isEaten = false;
- Token token = new Token(this, b, tokens.size());
- token.y = tokens.get(tokens.size()-1).y;
- token.x = tokens.get(tokens.size()-1).x;
- token.c = tokens.get(0).c;
- tokens.add(token);
- tokens.get(0).y = new Random().nextInt(b.h-3)+1;
- tokens.get(0).x = new Random().nextInt(b.w-3)+1;
- tokens.get(0).c = (char)(new Random().nextInt(26)+97);
- }
- @Override
- public void keyTyped(KeyEvent e) {
- try{
- char key = (""+e.getKeyChar()).toLowerCase().charAt(0);
- if(key=='w'||key=='s'||key=='a'||key=='d')
- play(key);
- }
- catch(Exception ee){}
- }
- @Override
- public void keyPressed(KeyEvent e) {
- throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
- }
- @Override
- public void keyReleased(KeyEvent e) {
- throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
- }
- }
- class Token{
- SnakeGame sg;
- Board b;
- int id;
- int y,x;
- char c;
- public Token(SnakeGame sg, Board b, int id){
- this.sg = sg;
- this.b = b;
- this.id = id;
- c = (char)(new Random().nextInt(26)+97);
- }
- public void setInitPos(){
- if(id==0){
- y = 10;
- x = 10;
- }
- else {
- y = b.h-4+id;
- x = b.w-4;
- }
- }
- public void MoveToken(char k){
- if(id!=0){
- if(id==1){
- sg.py = y;
- sg.px = x;
- switch(k){
- case 'w' : y-=1; break;
- case 's' : y+=1; break;
- case 'd' : x+=1; break;
- case 'a' : x-=1; break;
- }
- if(b.layout[y][x]=='*') JOptionPane.showMessageDialog(null, "GameOver", "Result", JOptionPane.INFORMATION_MESSAGE);
- else if(y==sg.tokens.get(0).y&&x==sg.tokens.get(0).x){
- sg.isEaten=true;
- }
- }
- else{
- int ty = y;
- int tx = x;
- y = sg.py;
- x = sg.px;
- sg.py = ty;
- sg.px = tx;
- }
- }
- b.layout[y][x] = c;
- }
- }
- class Board{
- JFrame f;
- int h,w;
- char[][] layout;
- public Board(int h, int w, SnakeGame sg){
- this.h = h;
- this.w = w;
- f = new JFrame();
- f.setSize(w*20, h*30);
- Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
- f.setLocation(dim.width/2-f.size().width/2, dim.height/2-f.size().height/2);
- f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- f.setLayout(new GridBagLayout());
- f.addKeyListener(sg);
- layout = new char [h][w];
- }
- public void clearBoard(){
- f.getContentPane().removeAll();
- for(int i=0; i<h; i++){
- for(int j=0; j<w; j++){
- if(i==0||i==h-1||j==0||j==w-1) layout[i][j]='*';
- else layout[i][j]=' ';
- }
- }
- }
- public void paintBoard(){
- for(int i=0; i<h; i++){
- for(int j=0; j<w; j++){
- GridBagConstraints gbc = new GridBagConstraints();
- JLabel lb = new JLabel(""+layout[i][j]);
- gbc.gridx = j;
- gbc.gridy = i;
- gbc.ipadx = 10;
- gbc.ipady = 2;
- gbc.anchor = GridBagConstraints.CENTER;
- f.add(lb, gbc);
- }
- }
- f.getContentPane().repaint();
- f.setVisible(true);
- }
- }
訂閱:
文章 (Atom)