2014-01-10 2 views
1

내 프레임이 회색으로 표시되지만 몇 초 후 my for 루프가 완료됨에 따라 이미지가 표시되는 문제가 있습니다. 루프를 표시하기 위해 일단 열면 이미지가 올바르게 표시되도록해야합니다. 전체 코드는 다음과 같습니다. 길이에 대해 사과드립니다.JFrame이 회색으로 표시됩니다. blank

import java.awt.*; 
import javax.swing.*; 

public class MainFrame extends JFrame{ 
    public void MainFrame() { 
     setTitle("Game"); 
     setSize(1300, 650); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setVisible(true); 
     setBackground(Color.WHITE); 
     Dice diceObject = new Dice(); 
     add(diceObject); 
    } 
} 
import java.awt.*; 
import java.util.Random; 
import javax.swing.*; 


public class Dice extends JPanel { 

public static int pause(int n) 
    { 
     try { 
      Thread.sleep(n); 
      } catch(InterruptedException e) { 
     } 
     return n; 
    } 

public void Dice() { 
    setDoubleBuffered(true); 
} 

@Override 
public void paintComponent(Graphics g) { 
    super.paintComponent(g); 
    int width = getWidth();  
    int height = getHeight(); 

    int num = 0; 
    for (int i = 0; i < 7; i++) { 
     Random generator= new Random(); 
     int number = generator.nextInt(6)+1; 
     g.setColor(Color.WHITE); 
     g.fillRect(0, 0, width, height); 
     g.setColor(Color.BLACK); 
     g.drawRoundRect(550, 150, 200, 200, 50, 50); 
     System.out.println("Test"); 
     if (number == 1) {     //Roll one 
      num = 1; 
      g.setColor(new Color (0, 0, 0)); 
      g.fillOval(640, 240, 20, 20); 
      pause(100); 
     } if (number == 2) {    //Roll two 
      num = 2; 
      g.setColor(new Color (0, 0, 0)); 
      g.fillOval(590, 290, 20, 20); 
      g.fillOval(690, 190, 20, 20); 
      pause(100); 
     } if (number == 3) {    //Roll three 
      num = 3; 
      g.setColor(new Color (0, 0, 0)); 
      g.fillOval(590, 290, 20, 20); 
      g.fillOval(640, 240, 20, 20); 
      g.fillOval(690, 190, 20, 20); 
      pause(100); 
     } if (number == 4) {    //Roll four 
      num = 4; 
      g.setColor(new Color (0, 0, 0)); 
      g.fillOval(590, 290, 20, 20); 
      g.fillOval(590, 190, 20, 20); 
      g.fillOval(690, 290, 20, 20); 
      g.fillOval(690, 190, 20, 20); 
      pause(100); 
     } if (number == 5) {    //Roll five 
      num = 5; 
      g.setColor(new Color (0, 0, 0)); 
      g.fillOval(590, 290, 20, 20); 
      g.fillOval(590, 190, 20, 20); 
      g.fillOval(640, 240, 20, 20); 
      g.fillOval(690, 290, 20, 20); 
      g.fillOval(690, 190, 20, 20); 
      pause(100); 
     } if (number == 6) {    //Roll six 
      num = 6; 
      g.setColor(new Color (0, 0, 0)); 
      g.fillOval(590, 190, 20, 20); 
      g.fillOval(590, 240, 20, 20); 
      g.fillOval(590, 290, 20, 20); 
      g.fillOval(690, 190, 20, 20); 
      g.fillOval(690, 240, 20, 20); 
      g.fillOval(690, 290, 20, 20); 
      pause(100); 
     } 
    } 
    g.setFont(new Font("TimesRoman", Font.PLAIN, 20)); 
    g.drawString("You rolled a " + num, 590, 100); 
    pause(1000); 
} 
} 

미리 감사드립니다.

+4

... 숫자의 많은 동일한 위치에 표시하기 때문에 내가 이런 짓을 [우리가 이미이 대화를하지 않았다?] (https://stackoverflow.com/questions/21033199 /도 -I-stop-my-paint-method-form-repeating-twice) – MadProgrammer

+0

@MadProgrammer와 동의 ... – ccjmne

+0

@MadProgrammer 그래,하지만 나는 다른 방법을 시도했다. 내가 이걸 작동 시키면이 방법이 더 좋다. – jcole30

답변

2

첫 번째 문제는 당신 대신 당신이 아무것도를 추가하기 전에 눈에 보이는 프레임 ...

를 설정하고 있다는 사실이다,

public class MainFrame extends JFrame{ 
    public void MainFrame() { 
     setTitle("Game"); 
     setSize(1300, 650); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setBackground(Color.WHITE); 
     Dice diceObject = new Dice(); 
     add(diceObject); 
     setVisible(true); 
    } 
} 

또한 직접 확장하지 않도록해야합니다 마지막 setVisible를 호출 시도 JFrame 클래스에 새로운 기능을 추가하지 않으므로 Initial Threads을 고려해야하며 Event Dispatching Thread 컨텍스트에서 기본 UI를 시작해야합니다.

문제의 나머지는 작업 예제를 업데이트하여 previous question

관련

Snake Eyes

내가 게으른거야 왜냐하면, 나는 점을 그릴 수있는 그래픽 2D API를 활용 한 . 도트의 많은

import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.EventQueue; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.Shape; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.geom.Ellipse2D; 
import java.util.ArrayList; 
import java.util.List; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.Timer; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class DiceRoller { 

    public static void main(String[] args) { 
     new DiceRoller(); 
    } 

    public DiceRoller() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
       } 

       JFrame frame = new JFrame("Testing"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.add(new Die()); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public class Die extends JPanel { 

     private int number = 1; 

     public Die() { 
      Timer timer = new Timer(500, new ActionListener() { 
       @Override 
       public void actionPerformed(ActionEvent e) { 
        number = (int) (Math.round((Math.random() * 5) + 1)); 
        repaint(); 
       } 
      }); 
      timer.setRepeats(true); 
      timer.setInitialDelay(0); 
      timer.start(); 
     } 

     @Override 
     public Dimension getPreferredSize() { 
      return new Dimension(220, 220); 
     } 

     @Override 
     public void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      Graphics2D g2d = (Graphics2D) g.create(); 
      int width = getWidth(); 
      int height = getHeight(); 

      g2d.setColor(Color.WHITE); 
      g2d.fillRect(0, 0, width, height); 
      g2d.setColor(Color.BLACK); 
      g2d.drawRoundRect(10, 10, width - 20, height - 20, 50, 50); 

      List<Shape> dots = new ArrayList<>(6); 

      if (number == 1 || number == 3 || number == 5) { 
       int x = (width - 20)/2; 
       int y = (height - 20)/2; 
       dots.add(new Ellipse2D.Float(x, y, 20, 20)); 
      } 

      if (number == 2 || number == 3 || number == 4 || number == 5 || number == 6) { 

       int x = ((width/2) - 20)/2; 
       int y = ((height/2) - 20)/2; 
       dots.add(new Ellipse2D.Float(x, y, 20, 20)); 
       dots.add(new Ellipse2D.Float(x + (width/2), y + (height/2), 20, 20)); 

      } 

      if (number == 4 || number == 5 || number == 6) { 

       int x = (width/2) + (((width/2) - 20)/2); 
       int y = ((height/2) - 20)/2; 
       dots.add(new Ellipse2D.Float(x, y, 20, 20)); 
       dots.add(new Ellipse2D.Float(x - (width/2), y + (height/2), 20, 20)); 

      } 

      if (number == 6) { 

       int x = (((width/2) - 20)/2); 
       int y = (height - 20)/2; 
       dots.add(new Ellipse2D.Float(x, y, 20, 20)); 
       dots.add(new Ellipse2D.Float(x + (width/2), y, 20, 20)); 

      } 

      for (Shape dot : dots) { 
       g2d.fill(dot); 
      } 

      g2d.dispose(); 
     } 
    } 
} 
관련 문제