게시판 즐겨찾기
편집
드래그 앤 드롭으로
즐겨찾기 아이콘 위치 수정이 가능합니다.
자바 애플릿 질문
게시물ID : computer_117027짧은주소 복사하기
작성자 : 블루코크와인
추천 : 0
조회수 : 605회
댓글수 : 0개
등록시간 : 2013/10/06 20:01:14
프로그램 내용 : 한점 (x,y)를 난수로 발생. 이후 case함수를 이용하여 한 방향으로 도형을 이동시킴 지정해놓은 위치까지 가면 다른 방향으로 튕기도록
다른 case 함수를 불러옴.
 
문제점 : 도형이 랜덤한 지점에 생성이 되나 도형들이 움직이지 않음.
다른 어떤 문법 오류는 없었음...
 
컴게 고수 님들 제가 뭘 잘못한걸까요 ㅠㅠ
 
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JApplet;
import java.util.Random;
 
public class TestClass2 extends JApplet
{
    private static final long serialVersionUID = 1L;
    private static final int WIDTH = 220, HEIGHT = 220;
    private static final int STEPS = 180;
    private static final int DELAY = 10;
    private static final int SIZE = 20;
   
    Random ran = new Random();
    @Override
    public void init()
    {
 resize(WIDTH, HEIGHT);
    }
   
    @Override
    public void paint(Graphics g)
    {
        int x, y;
        x = 1 + ran.nextInt(198);
        y = 1 + ran.nextInt(198);
        case1(g,x,y);
    }
   
    public void case1(Graphics g, int x, int y)
    {
        while(true)
        {
            if(x>=199)  case4(g,x,y);
            if(y>=199)  case2(g,x,y);
            x++;
            y++;
            drawBall(g, x, y);
            sleep(DELAY);
        }
    }
   
    public void case2(Graphics g, int x, int y)
    {
        while(true)
        {
            if(x>=199)  case3(g,x,y);
            if(y<=1)    case1(g,x,y);
            x++;
            y--;
            drawBall(g, x, y);
            sleep(DELAY);
        }
    }
   
    public void case3(Graphics g, int x, int y)
    {
        while(true)
        {
            if(x<=1)    case2(g,x,y);
            if(y<=1)    case4(g,x,y);
            x--;
            y--;
            drawBall(g, x, y);
            sleep(DELAY);
        }
    }
   
    public void case4(Graphics g, int x, int y)
    {
        while(true)
        {
            if(x<=1)    case1(g,x,y);
            if(y>=199)  case3(g,x,y);
            x--;
            y++;
            drawBall(g, x, y);
            sleep(DELAY);
        }
    }
    private void sleep(int delay)
    {
        try{Thread.sleep(delay);}
        catch(Exception e){e.printStackTrace();}
    }
    private void drawBall(Graphics g, int x, int y)
    {
        g.setColor(Color.gray);
        g.fillRect(0, 0, WIDTH, HEIGHT);
        g.setColor(Color.black);
        g.fillRect(x, y, SIZE, SIZE);
    }
}
전체 추천리스트 보기
새로운 댓글이 없습니다.
새로운 댓글 확인하기
글쓰기
◀뒤로가기
PC버전
맨위로▲
공지 운영 자료창고 청소년보호