디시인사이드 갤러리

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

갤러리 본문 영역

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

초심자(211.211) 2011.12.19 10:52:22
조회 80 추천 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/03 - -
AD 저녁 뭐먹지? 오늘의 메뉴 추천! 운영자 25/10/31 - -
공지 프로그래밍 갤러리 이용 안내 [97] 운영자 20.09.28 48512 65
2900961 듣자하니 프로덕션 배포를 스팟 인스턴스에 하는 병신들이 있는거 같던데 프갤러(223.38) 21:33 6 0
2900960 모기 한마리 후렷는데 시체 못 찾음.. ♥KiTTY냥덩♥갤로그로 이동합니다. 21:25 5 0
2900959 ㅆㅇㅆ야 노파심에 말하건데 [2] ㅇㅇ(124.48) 21:25 33 0
2900958 [AI와 에너지] 2번째 , 3번째 내용 프갤러(121.172) 21:22 6 0
2900957 오늘도 납품 한건 완료 10만 끝 ㅆㅇㅆ(124.216) 21:22 7 0
2900956 이 날씨에 모기약 존나 뿌려도 안 뒤지고 어디서 계속 니옴 ㅅㅂ [2] ♥KiTTY냥덩♥갤로그로 이동합니다. 21:16 13 0
2900955 아니 진심 모기 개미쳣음 여름에도 한번 못 본 새끼들이 ♥KiTTY냥덩♥갤로그로 이동합니다. 21:15 9 0
2900953 초봉5000중반 물경력 [4] 프갤러(112.157) 21:10 27 0
2900952 회사에서 국비출신 ai? 개발자 데려오려고 하는데 [2] 프갤러(222.108) 21:04 25 0
2900947 코드로 밥벌어먹기 어렵다 ㅆㅇㅆ(124.216) 20:49 21 0
2900946 코딩 독학하려는데 좋은 영상이나 강의 추천해줄 수 있나요 [1] 고사리(58.228) 20:47 23 0
2900944 이 세상은 도서관의 부분집합이다. 재현갤로그로 이동합니다. 20:39 13 0
2900943 vim이거 쓸 이유가 잇나여 [5] 르르곳사랑갤로그로 이동합니다. 20:34 33 0
2900942 싼게 비지떡이야. 값을 낮추지 말고 퀄리티만큼 받아라 [2] 프갤러(121.129) 20:29 18 0
2900940 감기 걸리면 기분 좋음? [2] ♥KiTTY냥덩♥갤로그로 이동합니다. 20:26 12 0
2900939 저장용 ♥KiTTY냥덩♥갤로그로 이동합니다. 20:21 13 0
2900938 표정 너무 무서웡 ㅠㅅ ㅠ ♥KiTTY냥덩♥갤로그로 이동합니다. 20:15 12 0
2900936 좌파 박지원 서해피격 은폐 관련 징역 구형 ♥KiTTY냥덩♥갤로그로 이동합니다. 19:49 10 0
2900935 웅.. ♥KiTTY냥덩♥갤로그로 이동합니다. 19:42 11 0
2900934 미쿡 뱅기 폭발한거 ㅇㅇ(223.39) 19:24 19 0
2900933 대학생이 프리랜서로 큰 건 따기는 어렵겠지? [1] ㅇㅇ(124.51) 19:15 26 0
2900932 나님 개피궁해서 애널은 일찍 씻준 할게양⭐+ ♥KiTTY냥덩♥갤로그로 이동합니다. 19:15 15 0
2900931 대학원생, 연구원들 상대로는 다 반값 받는중인데 입질이 잘 안오노 ㅆㅇㅆ(124.216) 18:50 24 0
2900930 나는내향적이야 나는내향적이란다 손발이시립디다갤로그로 이동합니다. 18:49 16 0
2900929 ❤✨☀⭐⚡☘⛩나님 시작합니당⛩☘⚡⭐☀✨❤ ♥KiTTY냥덩♥갤로그로 이동합니다. 18:34 14 0
2900928 내년에도 나라에서 백수짓하라고 함 타이밍뒷.통수한방(1.213) 18:26 29 0
2900927 9500억달러 vs 3500억달러...대미 투자액 진실은? 발명도둑잡기(118.216) 17:59 11 0
2900926 자바개발자 삼전 협력사로 라인들어가면 ide sts고정임? [2] ㅇㅇ(106.101) 17:53 32 0
2900925 이 세상은 복잡하당 By 나님 [3] ♥KiTTY냥덩♥갤로그로 이동합니다. 17:49 38 0
2900924 아이돌 노조 출범 예고 발명도둑잡기(118.216) 17:42 27 0
2900923 Linux on an 8-bit micro? (2012) 발명도둑잡기(118.216) 17:35 16 0
2900922 냥덩이류 극우 유튜버 근황 발명도둑잡기(118.216) 17:14 46 0
2900921 中, '토륨 기반 핵추진 상선' 설계 공개…"패러다임 전환" 발명도둑잡기(118.216) 17:10 17 0
2900920 구글, 에픽게임즈와 합의…인앱 결제 강제 둘러싼 5년 싸움 마무리 수순 발명도둑잡기(118.216) 17:05 14 0
2900918 자문은 많이 해봤는데 자문이라도 교수 자문이면 그래도 경력인정이겠지? ㅆㅇㅆ(124.216) 16:49 23 0
2900917 나도 여자랑 일 하다가 문제 생겼으면 좋겠다 발명도둑잡기(118.216) 16:44 30 0
2900916 운빨의 재분배 chironpractor갤로그로 이동합니다. 16:39 21 0
2900915 성과제로 해고자유화하고 10출3~4퇴가 맞당 [3] ♥KiTTY냥덩♥갤로그로 이동합니다. 16:39 47 0
2900914 나도 오늘 여자랑 일하다 문제생김 [4] 헬마스터갤로그로 이동합니다. 15:52 71 0
2900913 길건 - Real 발명도둑잡기(118.216) 15:45 19 0
2900912 어떤 사용자가 여러 갤러리에서 공격 받은 이유는 [7] 발명도둑잡기(118.216) 15:32 66 0
2900909 노력, 능력도 중요하긴 한데 그 이상으로 타이밍도 중요한 듯 [5] 프갤러(176.227) 15:09 63 1
2900905 인스타그램 추천 뜬 1호선 풍선껌 할머니 발명도둑잡기(118.216) 14:57 23 0
2900904 <언포게터블 듀엣> 한대서 생각나는 예전 글 발명도둑잡기(118.216) 14:52 18 0
2900903 나도 오늘 여자랑 일하다 문제 생김 [6] 에이도비갤로그로 이동합니다. 14:28 75 0
2900902 나님 그라파나 고수가되.. [2] 따당갤로그로 이동합니다. 14:14 58 0
2900901 여자들이랑 일하면 항상 문제 생기네 [1] 프갤러(121.184) 13:42 61 2
2900900 제가 주로 사용하는 특별한 기술은 배구공(119.202) 13:42 32 1
2900899 에헴.. 나는 세계최초, 국내최초 기술 보유자임 ㅋㅋ [6] 나르시갤로그로 이동합니다. 12:33 57 0
갤러리 내부 검색
제목+내용게시물 정렬 옵션

오른쪽 컨텐츠 영역

실시간 베스트

1/8

디시미디어

디시이슈

1/2