2016-12-01 1 views
0

JPanel 및 JFrame에서 임의의 크기 및 색상의 난수를 무작위로 그린 응용 프로그램을 만들었습니다. 모든 것이 옳은 것처럼 보이지만 ... 코드를 실행하면 타원이 그려지지 않습니다. 난 그냥 빈 화면 (JFrame 나타납니다,하지만 빈) receaver.JPanel 타원 그리기 문제

MyOval, DrawPanel 및 DrawTest의 세 클래스가 있습니다.

MyOval 클래스

package drawingovals; 

import java.awt.Color; 
import java.awt.Graphics; 

public class MyOval { 
    private int x1; 
    private int y1; 
    private int x2; 
    private int y2; 
    private Color myColor; 
    private boolean isFilled; 

    public MyOval(int x1, int y1, int x2, int y2, Color color, boolean isFilled) { 
     this.x1 = x1; 
     this.y1 = y1; 
     this.x2 = x2; 
     this.x2 = x2; 
     myColor = color; 
     this.isFilled = isFilled; 
    } 

    public int getUpperLeftX(){ 
     return x1; 
    } 

    public int getUpperLeftY(){ 
     return y1; 
    } 

    public int getWidth(){ 
     return x2 - x1; 
    } 

    public int getHeight() { 
     return y2 - y1; 
    } 

    public String getColor() { 
     return myColor.toString(); 
    } 

    public boolean getFilling(){ 
     return isFilled; 
    } 

    public void setUpperLeftX(int value) { 
     x1 = value; 
    } 

    public void setUpperLeftY(int value) { 
     y1 = value; 
    } 

    public void setDownRightX(int value) { 
     x2 = value; 
    } 

    public void setDownRightY(int value) { 
     y2 = value; 
    } 

    public void drawNoFill(Graphics g) { 
     g.setColor(myColor); 
     g.drawOval(getUpperLeftX(), getUpperLeftY(), getWidth(), getHeight()); 
    } 

    public void drawFill(Graphics g) { 
     g.setColor(myColor); 
     g.fillOval(getUpperLeftX(), getUpperLeftY(), getWidth(), getHeight()); 
    } 
} 

DrawPanel 클래스

package drawingovals; 

import java.awt.Color; 
import java.awt.Graphics; 
import java.util.Random; 
import javax.swing.JPanel; 

public class DrawPanel extends JPanel{ 
    private Random randomNumbers = new Random(); 
    private MyOval[] ovals; 

    public DrawPanel() { 
     setBackground(Color.WHITE); 

     ovals = new MyOval[5 + randomNumbers.nextInt(5)]; 

     for (int count = 0; count < ovals.length; count++) { 
      int x1 = randomNumbers.nextInt(300); 
      int y1 = randomNumbers.nextInt(300); 
      int x2 = randomNumbers.nextInt(300); 
      int y2 = randomNumbers.nextInt(300); 

      Color color = new Color(randomNumbers.nextInt(256), 
        randomNumbers.nextInt(256), randomNumbers.nextInt(256)); 

      boolean fill = randomNumbers.nextBoolean(); 

      ovals[count] = new MyOval(x1, y1, x2, y2, color, fill); 
     } 
    } 

    @Override 
    public void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     for (int count = 0; count < ovals.length - 1; count++) { 
      ovals[count].drawFill(g); 
     } 
    } 
} 

DrawTest 클래스

package drawingovals; 

import javax.swing.JFrame; 

public class DrawTest { 

    public static void main(String[] args) { 
     DrawPanel panel = new DrawPanel(); 
     JFrame application = new JFrame(); 

     application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     application.add(panel); 
     application.setSize(400, 400); 
     application.setVisible(true); 
    } 

} 

답변

2

당신이 취할 필요 타원형 높이와 너비에는 양수 값만 포함되므로주의하십시오. 코드에서이를 수행하지 않습니다. Math.max, Math.min 또는 Math.abs를 사용하여이를 달성하는 데 도움이되도록 타원형 속성을 얻을 수 있습니다.

이 같은
this.x1 = x1; 
    this.y1 = y1; 
    this.x2 = x2; // *** these are the same 
    this.x2 = x2; // *** these are the same 

뭔가 작동합니다 : 당신이 무시 Y2 X2 버그 설정을 복제하고 같은

또한이 잘못 향후

public MyOval(int x1, int y1, int x2, int y2, Color color, boolean isFilled) { 
    this.x1 = Math.min(x1, x2); 
    this.y1 = Math.min(y1, y2); 
    this.x2 = Math.max(x1, x2); 
    this.y2 = Math.max(y1, y2);; 
    myColor = color; 
    this.isFilled = isFilled; 
} 

, 키 이러한 유형의 해결에 디버거를 사용하여 프로그램의 필드 (여기서는 타원 속성)를 확인하고 프로그램이 실행될 때 디버그를 사용하여 이해가 안되는지 확인한 다음 그 이유를 확인합니다.

+0

많은 감사! 내가 피곤해서.) –