2014-03-19 2 views
0

이 코드는 사용자가 "Num"에 입력하는 원의 수를 인쇄하고 임의의 반지름과 중심점을 지정하지만 아무 것도 그려지지 않습니다. 애플릿 자체는 "애플릿 시작됨"을 표시합니다. 어떤 해결책?"Num"서클이 인쇄되지 않는 이유는 무엇입니까?

편집 : 또한 열리는 애플릿은 Eclipse 자체가 닫힐 때까지 닫히지 않습니다. EDIT : 코드가 업데이트되었습니다.

import java.applet.Applet; 
import java.awt.*; 
import java.util.Random; 

public class project10 extends Applet { 

    ConsoleReader console = new ConsoleReader(System.in); 
    Random generator = new Random(); 

    private static final long serialVersionUID = -3660618513445557612L; 


    public void drawCenteredCircle(Graphics g, int x, int y, int r) { 
      g.fillOval(x,y,r,r); 
    } 


    public void paint(Graphics g){ 

     int num = 5; 

     try{ 
     System.out.println("How many circles would you like to draw?"); 
     //num = console.readInt(); 

     for(int i = 1; num >= i; i++){ 
      g.setColor(Color.BLACK); 
      int one = generator.nextInt(100); 
      int two = generator.nextInt(100); 
      int three = generator.nextInt(100); 
      drawCenteredCircle(g,one,two,three); 
     } 
     }catch(NumberFormatException e){ 
      System.out.println("Input was not a valid number, please try again."); 
     }finally{ 
      System.out.println("You printed " + num + " circles"); 
     } 
    } 

} 

콘솔 리더 클래스 :

import java.io.BufferedReader; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.io.IOException; 

/** 
    A class to read strings and numbers from an input stream. 
    This class is suitable for beginning Java programmers. 
    It constructs the necessary buffered reader, 
    handles I/O exceptions, and converts strings to numbers. 
*/ 

public class ConsoleReader 
{ /** 
     Constructs a console reader from an input stream 
     such as System.in 
     @param inStream an input stream 
    */ 
    public ConsoleReader(InputStream inStream) 
    { reader = new BufferedReader 
     (new InputStreamReader(inStream)); 
    } 

    /** 
     Reads a line of input and converts it into an integer. 
     The input line must contain nothing but an integer. 
     Not even added white space is allowed. 
     @return the integer that the user typed 
    */ 
    public int readInt() 
    { String inputString = readLine(); 
     int n = Integer.parseInt(inputString); 
     return n; 
    } 

    /** 
     Reads a line of input and converts it into a floating- 
     point number. The input line must contain nothing but 
     a nunber. Not even added white space is allowed. 
     @return the number that the user typed 
    */ 
    public double readDouble() 
    { String inputString = readLine(); 
     double x = Double.parseDouble(inputString); 
     return x; 
    } 

    /** 
     Reads a line of input. In the (unlikely) event 
     of an IOException, the program terminates. 
     @return the line of input that the user typed, null 
     at the end of input 
    */ 
    public String readLine() 
    { String inputLine = ""; 

     try 
     { inputLine = reader.readLine(); 
     } 
     catch(IOException e) 
     { System.out.println(e); 
     System.exit(1); 
     } 

     return inputLine; 
    } 

    private BufferedReader reader; 
} 
+0

코드가 작동하는데 HTML 파일에서 오류가 발생했을 수 있습니까? Eclipse에서 애플릿 뷰어로 실행할 수 있습니까? – spydon

+0

그것이 처음에 @spydon을 실행하는 곳입니다. – sirnomnomz

+1

num = 5로 설정하고 ConsoleReader 클래스에 대한 호출을 수행하지 않으면 작동합니까? – spydon

답변

1

귀하의 ConsoleReader 클래스는 어떻게 든 차단하거나 기본적인 하나의 문제를 해결해야한다, 결함이있을 수 있습니다.

교체 :

ConsoleReader console = new ConsoleReader(System.in); 

Scanner console = new Scanner(System.in); 

과 또한 당신이하지에, 콘솔에 입력해야한다는 당신이 번호를 입력 한 후 Enter 키를 누릅니다한다는 것을 기억하고 애플릿 창.

편집 : 애플릿 뷰어가 초점을 맞추기 때문에 콘솔을 누른 다음 입력하여 글을 쓸 수 있어야합니다. (다른 콘솔 입력 클래스에서도 작동 할 수 있습니다.)

+0

여전히 작동하지 않습니다. – sirnomnomz

+0

검토를 위해 콘솔 판독기 클래스를 추가했습니다. @spydon – sirnomnomz

+1

입력 할 수 있습니까? 콘솔에 번호가 표시되고 "# 개의 서클을 인쇄했습니다"라는 텍스트가 보입니까? – spydon

관련 문제