2013-11-10 2 views
0
public static void board(){ //Create's my board 
    { 
     JFrame board = new JFrame(); 
     board.setSize(400, 200); 
     board.setTitle("Quiz Board Game"); 
     Container pane = board.getContentPane(); 
     pane.setLayout(new GridLayout(rows, columns)); 
     Color temp; 
     for (int i = 0; i < rows; i++) 
     { 
      if (i%2 == 0) 
      { 
       temp = col1; 
      } 
      else 
      { 
       temp = col2; 
      } 
      for (int j = 0; j < columns; j++) 
      { 
       JPanel panel = new JPanel(); 
       panel.setBackground(temp); 
       if (temp.equals(col1)) 
       { 
        temp = col2; 
       } 
       else 
       { 
        temp = col1; 
       } 
       pane.add(panel); 
      } 
     } 
     board.setVisible(true); 

У меня есть этот код, написанный на java, мне было интересно, как бы я добавил два круга, чтобы создать доску с двумя частями? Благодарю.Как добавить круг в метод в java?

P.S Я новичок в Java

ответ

0

переопределить метод paintComponent (Graphics г).

// create Image 
Image i = new ImageIcon("//filepath").getImage(); 
int pointXForFirstPiece = 0; // update for the x position of where your piece will be 
int pointYForFirstPiece = 0; // update for the y position 

public void paintComponent(Graphics g) { 
    // do the painting for the two objects here 
    // paint image 
    // create another image object as done above, and then 
    // rewrite the line below for the second piece 
    g.drawImage(i, pointXForFirstPiece, pointYForFirstPiece, null); 
} 

надеюсь, что это помогает

Смежные вопросы