컬렉션을 공유하면서 인덱스마저 공유하기 때문


- 해결법은 인덱스를 각자 주면됨.

'개발 > java' 카테고리의 다른 글

자바 try catch문  (0) 2013.10.29
자바 예외처리  (0) 2013.10.29
자바 awt 윈도우창에 메뉴바 만들기 연습  (0) 2013.10.18
자바 awt 연습  (0) 2013.10.17
자바 이미지 부분을 불러와 출력하기  (0) 2013.10.10

package shooting;


import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;


import javax.swing.JFrame;

import javax.swing.JMenu;

import javax.swing.JMenuBar;

import javax.swing.JMenuItem;


public class MainFrame extends JFrame {


//Frame 업글버전의 JFrame 사용

private SplashBoard splashBoard;  //메인프레임에 SplashBoard,GameBoard를 붙일 것이므로 필드선언

private GameBoard board;

//private RankingBoard rankingBoard;

private JMenuBar mainMenu;

private JMenu mnFile;

private JMenu mnWindow;

private JMenuItem miWindowSplash;

private JMenuItem miWindowGame;

private JMenuItem miFileExit;

// MenuBar아래 Menu 그안에 MenuItem이 담긴다 각각 필드선언.




public MainFrame()

{

//-------------------------------------

mainMenu = new JMenuBar();

mnFile = new JMenu("파일");

mnWindow = new JMenu("창");

miWindowSplash = new JMenuItem("스플래쉬");

miWindowGame = new JMenuItem("게임");

miFileExit = new JMenuItem("종료");

//---------------------------------------

mnWindow.add(miWindowSplash);

mnWindow.add(miWindowGame);

mnFile.add(miFileExit);

mainMenu.add(mnFile);

mainMenu.add(mnWindow);

// 위치.add(담을것) 식으로 선언해준다

this.setJMenuBar(mainMenu); //메뉴바인 메인메뉴는 add가아닌 set해준다

//----------------------------------------

setSize(500,700);

board = new GameBoard();

board.setFocusable(true);

this.add(board);

splashBoard = new SplashBoard();

this.add(splashBoard);

addWindowListener(new WindowAdapter() {


public void windowClosing(WindowEvent arg0) 

{

System.exit(0);

}

});

}

}



'개발 > java' 카테고리의 다른 글

자바 예외처리  (0) 2013.10.29
자바 멀티쓰레드 충돌 이유  (0) 2013.10.28
자바 awt 연습  (0) 2013.10.17
자바 이미지 부분을 불러와 출력하기  (0) 2013.10.10
자바 윈도우창에 출력해보기  (0) 2013.10.10

package shooting;


import java.awt.Canvas;

import java.awt.Graphics;

import java.awt.Image;

import java.awt.Toolkit;

import java.awt.event.KeyAdapter;

import java.awt.event.KeyEvent;

import java.util.ArrayList;

import java.util.Random;


public class GameBoard extends Canvas implements Runnable{

private UserFighter userfighter;

private ArrayList enemies;

private ArrayList missiles;

private StatusBar status;

private Image bg;

  private int key;

private Random rand = new Random();

private int timerToEnemyAppear; 

private int timerToFire; 


public GameBoard() // 게임보드 이미지와 객체생성

{

Toolkit tk = Toolkit.getDefaultToolkit();

bg = tk.getImage("res/22.jpg");

userfighter = new UserFighter();

status = new StatusBar();


//익명 클래스

addKeyListener(new KeyAdapter(){  //키아답터는 키이벤트 빈함수를 이미 포함하고있음 

public void keyPressed(KeyEvent e) 

{

int key=e.getKeyCode(); //코드값 정수형으로 반환 저장

switch(key)

{

case KeyEvent.VK_LEFT:

case KeyEvent.VK_RIGHT:

case KeyEvent.VK_UP:

case KeyEvent.VK_DOWN:

case KeyEvent.VK_SPACE:

GameBoard.this.key = key;

break;

}

}

@Override

public void keyReleased(KeyEvent e) {

super.keyReleased(e);


int key=e.getKeyCode(); 

switch(key)

{

case KeyEvent.VK_LEFT:

if(GameBoard.this.key== KeyEvent.VK_LEFT)

GameBoard.this.key = 0;

break;

case KeyEvent.VK_RIGHT:

if(GameBoard.this.key== KeyEvent.VK_RIGHT)

GameBoard.this.key = 0;

break;

case KeyEvent.VK_UP:

if(GameBoard.this.key== KeyEvent.VK_UP)

GameBoard.this.key = 0;

break;

case KeyEvent.VK_DOWN:

if(GameBoard.this.key== KeyEvent.VK_DOWN)

GameBoard.this.key = 0;

break;

case KeyEvent.VK_SPACE:

if(GameBoard.this.key== KeyEvent.VK_SPACE)

GameBoard.this.key = 0;

break;

}

}


});

enemies = new ArrayList();

missiles = new ArrayList();

new Thread(this).start();  //별도 변수명 저장안하고 실행

}

public void run()

{

while(true)

{

updateItems();

repaint();

try {

Thread.sleep(17);

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

private void updateItems() 

{

if(key != 0 )

userfighter.move(key);

for(int i=0; i<enemies.size(); i++)  // 적기 이동

((EnemyFighter)enemies.get(i)).move();

if(timerToEnemyAppear-- == 0) //적기출현 빈도수 설정

{

enemies.add(new EnemyFighter(this));

timerToEnemyAppear = (rand.nextInt(500))+500/17;

//파이터 미사일 발싸

UserMissile um = userfighter.fire();


     

    if(timerToFire>0)

timerToFire--;


if(key == KeyEvent.VK_SPACE)  // 스페이스키 눌렀을때 미사일을 일정간격으로 발싸@@

{

if(timerToFire == 0)

{

missiles.add(um);

timerToFire =7;

}

}

for(int i=0; i<missiles.size(); i++)  // 각 미사일 무브

((UserMissile)missiles.get(i)).move();

}


public void update(Graphics g) {

paint(g);

}


public void paint(Graphics g) 

{

Image buf = createImage(this.getWidth(), this.getHeight());

Graphics g1 = buf.getGraphics();


g1.drawImage(bg, 0, 0, this); //배경그리기 , 더블버퍼링

userfighter.paint(g1, this);

for(int i=0; i<enemies.size(); i++) // 적기 그리기

((EnemyFighter)enemies.get(i)).paint(g1);

for(int i=0; i<missiles.size(); i++) //미사일 그리기

((UserMissile)missiles.get(i)).paint(g1);


g.drawImage(buf, 0, 0, this);

}


}



import java.awt.Frame;

import java.awt.Graphics;

import java.awt.Image;

import java.awt.Toolkit;


public class MyFrame extends Frame{

private Image img;

public MyFrame(){

Toolkit tk = Toolkit.getDefaultToolkit();

img = tk.getImage("res/pg.jpg");

}

public void paint(Graphics g){

super.paint(g);

int offy=0;

try {

for(int i=0; i<500; i++)

{

g.drawImage(img, 10,10, 510, 60,     0,0,500,50+offy,  this);

Thread.sleep(200);

offy+=10;

}

catch (InterruptedException e) 

{

e.printStackTrace();

}

}

}



+ Recent posts