디시인사이드 갤러리

갤러리 이슈박스, 최근방문 갤러리

갤러리 본문 영역

안녕하세여 자바 과제중인데 해결이 안돼서 ㅜㅜ..

초심자(211.211) 2011.12.19 10:52:22
조회 92 추천 0 댓글 1

블락스케이프라고 비슷한거 만드는데요

막 박스 이동시켜서 노란거 탈출 시키는거거든여

박스끼리 서로 막 통과하고 난리났어요

이거 어떻게 해결할수있나여?

박스는 2d로그렸어여
클래스하나에 다때려넣엇거든요..

으으.. 박스끼리만 안겹치면 좋겠는데 어떻게 못하는건가요? 새로 다 다시짜야하나여?


 import java.awt.*;
 import javax.swing.*;
 import java.awt.event.*;
 import java.awt.image.BufferedImage;


 public class GameStart extends JPanel implements MouseListener,
    MouseMotionListener {
  Rectangle rect = new Rectangle(0, 0, 314, 314);
  Rectangle vbox1 = new Rectangle(2,54,100,50);
  Rectangle Tbox = new Rectangle(106,106,100,50);
  Rectangle vbox2 = new Rectangle(2,260,100,50);
  Rectangle vbox3 = new Rectangle(2,210,100,50); 
  Rectangle vbox4 = new Rectangle(158,2,100,50);
  Rectangle vbox5 = new Rectangle(158,262,150,50);
  Rectangle vbox6 = new Rectangle(2,2,150,50);
  Rectangle hbox1 = new Rectangle(106,158,50,150);
  Rectangle hbox2 = new Rectangle(158,158,50,100);
  Rectangle hbox3 = new Rectangle(262,2 ,50,150);

 
 
  BufferedImage bi = new BufferedImage(5, 5, BufferedImage.TYPE_INT_RGB);  //이해안됨

  Graphics2D big;

  int v1x,  v2x,v3x,v4x,v5x,v6x;
  int tx;
  int h1y, h2y ,h3y;
  static boolean i = false;

 
  boolean firstTime = true;

  Rectangle area;

  boolean pressOut1 = false;
  boolean pressOut2 = false;
  boolean pressOut3 = false;
  boolean pressOut4 = false;
  boolean pressOut5 = false;
  boolean pressOut6 = false;
  boolean pressOut7 = false;
  boolean pressOut8 = false;
  boolean pressOut9 = false;
  boolean pressOut10 = false;
 

  public GameStart() {
    setBackground(Color.black);
    addMouseMotionListener(this);
    addMouseListener(this);
  }

 
  public void mousePressed(MouseEvent e) {

 v1x = vbox1.x - e.getX();
 v2x = vbox2.x - e.getX();
 v3x = vbox3.x - e.getX();
 v4x = vbox4.x - e.getX();
 v5x = vbox5.x - e.getX();
 v6x = vbox6.x - e.getX();
    tx = Tbox.x - e.getX();
    h1y = hbox1.y - e.getY();
    h2y = hbox2.y - e.getY();
    h3y = hbox3.y - e.getY();
   
  
    if (vbox1.contains(e.getX(), e.getY())) {
      updateLocation2(e);
    } else {
     pressOut1 = true;
    }
    if (vbox2.contains(e.getX(), e.getY())) {
        updateLocation3(e);
      } else {
       pressOut3 = true;
      }
    if (vbox3.contains(e.getX(), e.getY())) {
        updateLocation4(e);
      } else {
       pressOut4 = true;
      }
    if (vbox4.contains(e.getX(), e.getY())) {
        updateLocation5(e);
      } else {
       pressOut5 = true;
      }
    if (vbox5.contains(e.getX(), e.getY())) {
        updateLocation6(e);
      } else {
       pressOut6 = true;
      }
    if (vbox6.contains(e.getX(), e.getY())) {
        updateLocation7(e);
      } else {
       pressOut7 = true;
      }
    if (hbox1.contains(e.getX(), e.getY())) {
        updateLocation1(e);
      } else {
        pressOut10 = true;
      }
    if (hbox2.contains(e.getX(), e.getY())) {
        updateLocation8(e);
      } else {
        pressOut8 = true;
      }
    if (hbox3.contains(e.getX(), e.getY())) {
        updateLocation8(e);
      } else {
        pressOut9 = true;
      }
    if (Tbox.contains(e.getX(), e.getY())) {
        updateLocation(e);
      } else {
        pressOut2 = true;
      }
  }
 
  public void mouseDragged(MouseEvent e) {

    if (!pressOut1) {
      updateLocation2(e);
    }
    if (!pressOut3) {
        updateLocation3(e);
      }
    if (!pressOut4) {
        updateLocation4(e);
      }
    if (!pressOut5) {
        updateLocation5(e);
      }
    if (!pressOut6) {
        updateLocation6(e);
      }
    if (!pressOut7) {
        updateLocation7(e);
      }
    if (!pressOut8) {
        updateLocation8(e);
      }
    if (!pressOut9) {
        updateLocation9(e);
      }
    if (!pressOut10) {
        updateLocation1(e);
      }
   
    if (!pressOut2) {
      updateLocation(e);
      }
  }

 
  public void mouseReleased(MouseEvent e) {
    if (vbox1.contains(e.getX(), e.getY())) {
      updateLocation2(e);
    }
    else if (Tbox.contains(e.getX(), e.getY())) {
        updateLocation(e);
      }
    else if (vbox2.contains(e.getX(), e.getY())) {
        updateLocation3(e);
      }
    else if (vbox3.contains(e.getX(), e.getY())) {
        updateLocation4(e);
      }
    else if (vbox4.contains(e.getX(), e.getY())) {
        updateLocation5(e);
      }
    else if (vbox5.contains(e.getX(), e.getY())) {
        updateLocation6(e);
      }
    else if (vbox6.contains(e.getX(), e.getY())) {
        updateLocation7(e);
      }
    else if (hbox1.contains(e.getX(), e.getY())) {
        updateLocation1(e);
      }
    else if (hbox2.contains(e.getX(), e.getY())) {
        updateLocation8(e);
      }
    else if (hbox3.contains(e.getX(), e.getY())) {
        updateLocation9(e);
      }
   
  }

  public void mouseMoved(MouseEvent e) {
    pressOut1 = false;
     pressOut2 = false;
     pressOut3 = false;
     pressOut4 = false;
     pressOut5 = false;
     pressOut6 = false;
     pressOut7 = false;
     pressOut8 = false;
     pressOut9 = false;
     pressOut10 = false;
  }

  public void mouseClicked(MouseEvent e) {
  }

  public void mouseExited(MouseEvent e) {
 
  }

  public void mouseEntered(MouseEvent e) {
 
  }
 
  public void updateLocation1(MouseEvent e) {

    hbox1.setLocation(hbox1.x , h1y + e.getY());
    repaint();
  }
  public void updateLocation8(MouseEvent e) {

     hbox2.setLocation(hbox2.x , h2y + e.getY());
     repaint();
   }
  public void updateLocation9(MouseEvent e) {

     hbox3.setLocation(hbox3.x , h3y + e.getY());
     repaint();
   }
  public void updateLocation(MouseEvent e) {

     Tbox.setLocation(tx + e.getX(), Tbox.y) ;
     if( Tbox.x > 260)
      i = true;
     repaint();
   
   }

  public void updateLocation2(MouseEvent e) {

     vbox1.setLocation(v1x + e.getX()  , vbox1.y) ;
     repaint();
   }
  public void updateLocation3(MouseEvent e) {

     vbox2.setLocation(v2x + e.getX()  , vbox2.y) ;
     repaint();
   }
  public void updateLocation4(MouseEvent e) {

     vbox3.setLocation(v3x + e.getX()  , vbox3.y) ;
     repaint();
   }
  public void updateLocation5(MouseEvent e) {

     vbox4.setLocation(v4x + e.getX()  , vbox4.y) ;
     repaint();
   }
  public void updateLocation6(MouseEvent e) {

     vbox5.setLocation(v5x + e.getX()  , vbox5.y) ;
     repaint();
   }
  public void updateLocation7(MouseEvent e) {

     vbox6.setLocation(v6x + e.getX()  , vbox6.y) ;
     repaint();
   }
 
 

  public void paint(Graphics g) {
    update(g);
  
  }

  public void update(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;

    if (firstTime) {
     int x= 15; int y=10;
      Dimension dim = getSize();
      int w = dim.width;
      int h = dim.height;
      area = new Rectangle(dim);
      bi = (BufferedImage) createImage(w, h);
      big = bi.createGraphics();
      rect.setLocation(x, y);
      vbox1.setLocation(x+2, y+54);
      vbox2.setLocation(x+2, y+262);
      vbox3.setLocation(x+2, y+210);
      vbox4.setLocation(x+158, y+2);
      vbox5.setLocation(x+158, y+262);
      vbox6.setLocation(x+2, y+2);
      hbox1.setLocation(x+106, y+158);
      hbox2.setLocation(x+158, y+158);
      hbox3.setLocation(x+262, y+2);
      Tbox.setLocation(x+106, y+106);
      big.setStroke(new BasicStroke(8.0f));
      firstTime = false;
    }

    big.setColor(Color.black);
    big.clearRect(0, 0, area.width, area.height);

    big.setPaint(Color.white);
    big.draw(rect);
    big.setPaint(Color.gray);
    big.fill(rect);

    big.setPaint(Color.blue);
    big.fill(vbox1);
    big.setPaint(Color.blue);
    big.fill(vbox2);
    big.setPaint(Color.blue);
    big.fill(vbox3);
    big.setPaint(Color.blue);
    big.fill(vbox4);
    big.setPaint(Color.blue);
    big.fill(vbox5);
    big.setPaint(Color.blue);
    big.fill(vbox6);
    big.setPaint(Color.blue);
    big.fill(hbox1);
    big.setPaint(Color.blue);
    big.fill(hbox2);
    big.setPaint(Color.blue);
    big.fill(hbox3);
    big.setPaint(Color.yellow);
    big.fill(Tbox);
   
    g2.drawImage(bi, 0, 0, this);
  }

  public static void main(String s[]) {

    JFrame f = new JFrame("BoxEscape");
    JLabel label;
    f.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });
    f.getContentPane().setLayout(new BorderLayout());
    f.getContentPane().add(new GameStart(), "Center");
  
   
    label = new JLabel("Gaming");
    f.getContentPane().add(BorderLayout.SOUTH, label);
   
    if( i == true ){
   
    label.setText("Clear!");
     
    }
    f.pack();
    f.setSize(new Dimension(400, 400));
    f.show();
  }

}

추천 비추천

0

고정닉 0

0

댓글 영역

전체 댓글 0
본문 보기

하단 갤러리 리스트 영역

왼쪽 컨텐츠 영역

갤러리 리스트 영역

갤러리 리스트
번호 제목 글쓴이 작성일 조회 추천
설문 이성 보는 눈 없어서 걱정되는 스타는? 운영자 25/11/10 - -
AD 겨울가전 SALE! 쿨한 겨울 HOT세일 운영자 25/11/12 - -
295458 팥죽 먹었더니 속좀 풀리는거 같네.. 쿄스케갤로그로 이동합니다. 11.12.22 49 0
295455 니들이 생각하는 좋은 회사의 기준은 뭐냐? [22] 줫밥(122.49) 11.12.22 377 0
295452 2012년 다이어리랑 달력 받았다.. [11] 줫밥(122.49) 11.12.22 98 0
295451 도대체 페북은 어떻게 친구를 찾아주는걸까?! [6] 캐페북닭(115.92) 11.12.22 211 0
295450 더러운 사법부 [4] 씨발라드세요갤로그로 이동합니다. 11.12.22 115 0
295449 후..북마크 정리 완료.... [4] 캐북마크닭(115.92) 11.12.22 101 0
295447 내복이 더 두꺼운데? [3] elwlwlwk갤로그로 이동합니다. 11.12.22 106 0
295446 해장 할때 점심은 짱뽕국이 좋아여? 순대국이 좋아여? [5] 막장갤신학생갤로그로 이동합니다. 11.12.22 166 0
295445 시발 어제 1시에 캐닭에게 전화가왔다.. [11] 줫밥(122.49) 11.12.22 119 0
295444 기모 레깅스 다들 안입어 봤나보네.. [2] McHello갤로그로 이동합니다. 11.12.22 117 0
295442 아.. 지랄같다.. [2] ㅇㅇ(58.102) 11.12.22 54 0
295439 분노의 파워코딩 4일차, [2] [성대아싸]갤로그로 이동합니다. 11.12.22 126 0
295438 프게이들 래깅스 안입어 봐뜸? [2] 42(125.7) 11.12.22 78 0
295437 캐롤조까 [1] 싴보이갤로그로 이동합니다. 11.12.22 62 0
295436 야잌 던파 점검시간 늘어났어 ㅠㅠ [3] Lover♥갤로그로 이동합니다. 11.12.22 65 0
295435 프갤에서의 질문법 [1] ㅁㅁ(211.208) 11.12.22 75 0
295433 형들아 질문좀 하자 Adelposs갤로그로 이동합니다. 11.12.22 51 0
295431 아 졸라 속쓰려.. 쿄스케갤로그로 이동합니다. 11.12.22 63 0
295427 너님들 어떤 선택을 할래?? [8] 쿄스케갤로그로 이동합니다. 11.12.22 157 0
295426 IT 쪽에서도 정치인이 나와야 한다.! [4] ㅁㅁㅁ(116.125) 11.12.22 119 0
295425 C언어 입문자인데 질문점~ [2] 해피너스갤로그로 이동합니다. 11.12.22 146 0
295424 군포로 4호선타고 출근중 앗살라무알라이쿰갤로그로 이동합니다. 11.12.22 56 0
295423 질문 몇가지만... [2] 헬로우쳇(222.96) 11.12.22 70 0
295422 퇴사하려니까 부장님이 딜을 거시네 [20] 9m(203.236) 11.12.22 290 0
295421 출쵝! 오늘 쉬펄 수요일인줄 알았는데 목요일 왠지 기분이 좋다. [2] 개심심(61.73) 11.12.22 63 0
295419 선택의 기로에 놓여있습니다.. 조언좀 부탁드립니다 [2] Witch Doctor갤로그로 이동합니다. 11.12.22 148 0
295416 나이25살인데 프로그래밍입문해도 됨? [9] 가리온(175.210) 11.12.22 290 0
295415 당근과 채찍 [1] Lumberjack갤로그로 이동합니다. 11.12.22 111 0
295414 횽들 비주얼스튜디오 2010에서 가로로 길어진 소스 어떻게 줄바꿈하나요? [5] 빈센트.갤로그로 이동합니다. 11.12.22 176 0
295412 초보자용 예문에 두시간 걸렸네.. [2] EvilDragon갤로그로 이동합니다. 11.12.22 84 0
295411 자바 gui에서 textarea 에서....... [7] 스백십갤로그로 이동합니다. 11.12.22 144 0
295407 sql 비주얼c++로해야징.. Religioner갤로그로 이동합니다. 11.12.22 46 0
295406 아 잘생겨지고 싶다라는 생각밖에 안든다... [2] Lover♥갤로그로 이동합니다. 11.12.22 137 0
295405 smarty 를 window에 설치하는 방법잘아는분없나요~? 니 코 가 뭐갤로그로 이동합니다. 11.12.22 48 0
295404 여기 프로그래밍배워서 [4] 신비의섬죶도갤로그로 이동합니다. 11.12.21 158 0
295403 SQL게이트 쓰고싶다. Religioner갤로그로 이동합니다. 11.12.21 85 0
295401 디씨 디도스 공격이라도 받고 있나 [1] 새알(183.102) 11.12.21 87 0
295400 형들 PostgreSQL 이거 실무에서 많이 쓰나요? [1] ㅂㅈㄷ(210.183) 11.12.21 97 0
295399 코딩 유사도 검사하는 프로그램 있음?? [3] 마타버터갤로그로 이동합니다. 11.12.21 161 0
295398 요즘 kth괜춘한듯? ㅂㅈㄷㄱ(183.101) 11.12.21 62 0
295396 질문입니다 알려주세요ㅠㅠ ㅇㅇㅇㅇㅇ(165.132) 11.12.21 45 0
295395 매일 기도한다 +어게인갤로그로 이동합니다. 11.12.21 64 0
295394 C언어 학원말야 [1] (175.120) 11.12.21 108 0
295393 분노의 파워코딩 3일차, 작업내용 [3] [성대아싸]갤로그로 이동합니다. 11.12.21 205 0
295392 뭐좀 물어볼께 있는디 [1] Adelposs갤로그로 이동합니다. 11.12.21 68 0
295391 방금 그만둔다고 말했다 ㅅㅂ 므해?갤로그로 이동합니다. 11.12.21 109 0
295390 나 지금 진짜 절실함 과제 도와줘... 살려줘 ㅠ(59.15) 11.12.21 74 0
295389 회기 ㄱ ㄱ ㄱ [1] 헝그리박갤로그로 이동합니다. 11.12.21 79 0
295388 자바 GUI 간단한 질문인데 아는형없나 Adinforest갤로그로 이동합니다. 11.12.21 65 0
295387 소켓 파일전송에 대한 질문 좀 드리겠습니다 학교에서뭘배(203.250) 11.12.21 102 0
갤러리 내부 검색
제목+내용게시물 정렬 옵션

오른쪽 컨텐츠 영역

실시간 베스트

1/8

디시미디어

디시이슈

1/2