2017-08-29 2 views
2

Ich mache gerade ein Programm, das ein Checker-Board mit GUI setzt. Was mein Problem ist, ist, dass ich die Boardfarben einstellen kann und das Programm mich benachrichtigen kann, wenn die Maus geklickt wird und wo, aber ich kann nicht herausfinden, wie man die Checker in der Mitte jeder Stelle an den richtigen Stellen erscheinen lässt.setzen Kreis in der Mitte von Jpanel

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent; 

public class Main { 
    public static int rows = 8; 
    public static int colums = 8; 
    public static Color Col1 = Color.BLACK; 
    public static Color Col2 = Color.RED; 

    public static void main(String [ ] args){ 
     System.out.println("Dbug3"); 
     JFrame boardGUI = new JFrame(); 
     boardGUI.setSize(800, 800); 
     boardGUI.setTitle("Checker Board"); 
     Container pane = boardGUI.getContentPane(); 
     pane.setLayout(new GridLayout(rows, colums)); 
     Color tmp; 
     ImageIcon image = new ImageIcon("C:\\Users\\Awesome\\Desktop\\CheckerP.jpg"); 

     JLabel label = new JLabel("", image, JLabel.CENTER); 

     boardGUI.addMouseListener(new MouseAdapter() { 

      public void mousePressed(MouseEvent e) { 
       //Board 
       System.out.println(e.getX() + "," + e.getY()); 
      } 
     }); 
     for (int i = 0; i < rows; i++) { 
      if (i % 2 == 0) { 
       tmp = Col1; 
      } else { 
       tmp = Col2; 
      } 
      for (int j = 0; j < colums; j++) { 
       JPanel panel = new JPanel(); 
       panel.setBackground(tmp); 
       panel.add(label, BorderLayout.CENTER); 
       if (tmp.equals(Col1)) tmp = Col2; 
       else tmp = Col1; 
       pane.add(panel); 
      } 


     } 
     boardGUI.setVisible(true); 

    } 
    public void paint(Graphics2D g) { 
     g.fillOval(480,480,200,200); 
     g.setColor(Color.BLUE); 
    } 
} 

Antwort

1

dieses Versuchen Sie, mit dem drawOval vor dem fillOval?

public void drawCircle(Graphics2D g) {   
     g.drawOval(480, 480, 200, 200); 
     g.setColor(Color.BLUE); 
     g.fillOval(480, 480, 200, 200); 
    } 
Verwandte Themen