안녕하세요

프로그램 과정에서 막혔던 문제들에 대한 해결책 정리


페이지 목록

2011년 8월 29일 월요일

[변신인간] Map.java (GUI 부분)


package com.jay.Change;

import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import com.jay.Character.*;
import com.jay.Control.*;

public class Map {
        Panel에 주인공과 몬스터들을 삽입하여 제어
CPanel cPanel = new CPanel();
boolean isUp, isDown, isLeft, isRight, isA, isChange = false;
static final String bPath = "D:/자바 프로그램/변신인간/하늘2.jpg";
ImageIcon img;

Control control = new Control(cPanel);
public static void main(String[] args) {
Map map = new Map();
map.go();
}

public void go() {
JFrame frame = new JFrame();
frame.setTitle("ChangeMan");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
img = new ImageIcon(bPath);

JPanel background = new JPanel();
background.setLayout(new BorderLayout());
JLabel label = new JLabel(new ImageIcon(bPath));
label.setHorizontalAlignment(JLabel.CENTER);
background.add(label); // default center section
// background.add(cPanel);
frame.getContentPane().add(cPanel);
cPanel.addKeyListener(new ListenKey());
frame.setFocusable(true);
frame.setSize(820, 650);
frame.setVisible(true);
cPanel.requestFocus();

control.Thread();
}

class ListenKey implements KeyListener {

@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
switch (e.getKeyCode()) {
case KeyEvent.VK_LEFT:
control.setIsLeft(true);
break;
case KeyEvent.VK_RIGHT:
control.setIsRight(true);
break;
case KeyEvent.VK_UP:
control.setIsUp(true);
break;
case KeyEvent.VK_DOWN:
control.setIsDown(true);
break;
case KeyEvent.VK_A:
control.setIsA(true);
break;
}
}

@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
switch (e.getKeyCode()) {
case KeyEvent.VK_LEFT:
control.setIsLeft(false);
break;
case KeyEvent.VK_RIGHT:
control.setIsRight(false);
break;
case KeyEvent.VK_UP:
control.setIsUp(false);
break;
case KeyEvent.VK_DOWN:
control.setIsDown(false);
break;
case KeyEvent.VK_A:
control.setIsA(false);
break;
}
}

@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
}

class CPanel extends JPanel {

/**
*
*/
private static final long serialVersionUID = 6652451537073754302L;

public void paintComponent(Graphics g) {
g.drawImage(img.getImage(), 0, 0, this);
control.Move(g);
}
}
}

KeyListener를 이용해서 컨트롤 제어

댓글 없음:

댓글 쓰기