JFrame的Oval被放置在Rectangles JAVA的后面

时间:2018-05-30 10:37:26

标签: java jframe jpanel rectangles

所以,我正在为我的计算机编程主题构建一个Tic Tac Toe游戏。我的问题是当我向我的JFrame添加一个椭圆形时,它被放置在我的游戏板的瓷砖后面(它们是矩形)。

(请注意,我已经在Rectangle和Oval中添加了一个额外的参数来选择它的颜色。)

所以我的问题: 有没有简单的方法来改变对象的颜色? 如何将椭圆放在矩形的顶部?

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

public class GameBoard{
   static JFrame f = new JFrame("Tic Tac Toe");
   static JPanel win = new JPanel();
   static Rectangle re[][] = new Rectangle[3][3];

   static Oval player1[][] = new Oval[9][9];
   static Oval player2[][] = new Oval[9][9];  

   public static void main(String args[]){
      f.setBounds(50, 50, 256, 279);
      f.setLayout(null);
      f.setVisible(true);
      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


      win.add(new Rectangle(0,0,80,80,1));
      win.add(new Rectangle(81,0,80,80,0));     
      win.add(new Rectangle(162,0,80,80,1));

      win.add(new Rectangle(0,81,80,80,0));
      win.add(new Rectangle(81,81,80,80,1));
      win.add(new Rectangle(162,81,80,80,0));

      win.add(new Rectangle(0,162,80,80,1));
      win.add(new Rectangle(81,162,80,80,0));
      win.add(new Rectangle(162,162,80,80,1));
      f.getContentPane().add(win);
      f.repaint();

      userInput();

   }   

   static int p1Count = 0;
   static int p2Count = 0;

   static void userInput(){
      int uImpCalc = 1; 
      Scanner s = new Scanner(System.in);
      while(uImpCalc <= 9){

         int x = s.nextInt();
         int y = s.nextInt();
         if(x <= 3 && x > 0){
            if(y <= 3 && y >0){
               int rX = x*80;
               int rY = y*80;
               if(uImpCalc%2 == 0){
                  player1[p1Count][p1Count] = new Oval(rY, rX, 80, 80, 2); 
                  win.add(player1[p1Count][p1Count]);
                  p1Count++;
               }else{
                  player2[p2Count][p2Count] = new Oval(rY, rX, 80, 80, 1); 
                  win.add(player2[p2Count][p2Count]);
                  p2Count++;
               }
               win.repaint();

               System.out.println("Entering '"+x+", "+y+"'");
               uImpCalc++;

            }else{
               System.out.println("Invalid Y: "+y);
            }

         }else{
            System.out.println("Invalid X: "+x);
         }

      }
     System.out.println("Game Over");
      System.exit(0); 
   }
}

非常感谢!

0 个答案:

没有答案