2013-11-15 2 views
0
import java.applet.Applet; 
import java.awt.Color; 
import java.awt.Graphics; 

class Ball { 

    int x, y, radius, dx, dy; 
    Color BallColor; 

    public Ball(int x, int y, int radius, int dx, int dy, Color bColor) { 
     this.x = x; 
     this.y = y; 
     this.radius = radius; 
     this.dx = dx; 
     this.dy = dy; 
     BallColor = bColor; 
    } 

} 

public class Bounce extends Applet implements Runnable { 

    Ball redBall; 

    public void init() { 
     redBall = new Ball(250, 80, 50, 2, 4, Color.red); 
     Thread t = new Thread(this); 
     t.start(); 
    } 

    public void paint(Graphics g) { 
     g.setColor(redBall.BallColor); 
     setBackground(Color.pink); 
     g.fillOval(redBall.x, redBall.y, redBall.radius, redBall.radius); 
     g.drawLine(150, 400, 50, 500); 
     g.drawLine(150, 400, 450, 400); 
     g.drawLine(50, 500, 350, 500); 
     g.drawLine(450, 400, 350, 500); 
     g.drawRect(50, 500, 20, 100); 
     g.drawRect(330, 500, 20, 100); 
     g.drawLine(450, 400, 450, 500); 
     g.drawLine(430, 500, 450, 500); 
     g.drawLine(430, 500, 430, 420); 
    } 

    public void run() { 
     while (true) { 
      try { 
       displacementOperation(redBall); 
       Thread.sleep(20); 
       repaint(); 
      } catch (Exception e) { 
      } 
     } 
    } 

    public void displacementOperation(Ball ball) { 
     if (ball.y >= 400 || ball.y <= 0) { 
      ball.dy = -ball.dy; 
     } 
     ball.y = ball.y + ball.dy; 
    } 

} 

코드를 컴파일하고 실행할 때 main 메서드가 Bounce 클래스에서 발견되지 않는다고 말하면 메서드를 public static void main (String [] args)로 정의하십시오. 나는 누군가가 잘못된 점을 지적 할 수 있다면 이것을 고치는 법을 많이 고맙게 생각할 것입니다. 감사합니다주 메서드가 클래스에서 찾을 수 없습니다.

+1

실제로 기본 방법이있는 수업이 있습니까? – david99world

+0

"시작하기"안내서 (http://docs.oracle.com/javase/tutorial/getStarted/index.html)를 읽으십시오. 기본적인 hello world 프로그램이 어떻게 생겼는지, 그리고 Java로 그래픽을하기 전에 어떻게 시작해야 하는지를 알아야합니다. –

+1

명령 줄에서 애플릿을 실행하려고합니다. 이 작업을 수행 할 수 있지만 main 메소드를 추가해야합니다. 애플릿에는 주요 메소드가있을 필요가 없습니다. – occulus

답변

0

이 애플릿은 애플릿 지원 브라우저에서 실행할 수 있습니다. 애플릿은 다음 메소드로 시작됩니다.

public void init() // This method works as like main. 

이 코드를 실행하려면 HTML 페이지에 애플릿 태그를 구성하기 만하면됩니다.

<applet code = "Bounce.class" 
    width = "500" 
    height = "300"> 
</applet> 
+0

위대한 답변을 주셔서 감사합니다 :) – user81883