2015-10-20 99 views
1

我的任務是通過paintComponent方法創建一個10x10可調整大小的棋盤。
我的問題是我得到的第一個行權,但接下來的行不顯示,我根本不知道我的錯誤是10x10棋盤與paintComponent

GrafikPanel類:

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

public class GrafikPanel extends JPanel { 
    @Override 
    public void paintComponent(Graphics g){ 
     super.paintComponent(g); 
     int width = g.getClipBounds().width; 
     int height = g.getClipBounds().height; 

     int lines = 0; 
     while(lines < 10){ 
      int posH = height; 
      int posW = width/10; 
      int squares = 0; 
      while(squares < 10){ 
       if(squares%2 == 0){ 
        if(lines%2 != 0){ 
         g.setColor(Color.BLACK); 
        } 
        else { 
         g.setColor(Color.WHITE); 
        } 
        g.fillRect(width-posW, height-posH, width/10, height/10); 
       } 
       if(squares%2 != 0){ 
        if(lines%2 != 0){ 
         g.setColor(Color.WHITE); 
        } 
        else { 
         g.setColor(Color.BLACK); 
        } 
        g.fillRect(width-posW, height-posH, width/10, height/10); 
       } 
       posW += width/10; 
       squares++; 
      } 
      posH -= height/10; 
      lines++; 
     } 
    } 
    @Override 
    public Dimension getPreferredSize(){ 
     return new Dimension(500, 500); 
    } 
} 

AUF1類:

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

public class Auf1 extends JFrame { 
    GrafikPanel panel; 
    public Auf1(){ 
     setTitle("Schachbrett"); 

     panel = new GrafikPanel(); 
     add(panel); 

     pack(); 
     setDefaultCloseOperation(EXIT_ON_CLOSE); 
     setVisible(true); 
    } 
    public static void main(String[] args){ 
     new Auf1(); 
    } 
} 

我的代碼格式不正確,因爲我無法習慣於在這裏輸入代碼的方式,因此, 如果有人能告訴我我在哪裏可以修復我的錯誤,那會很好。

+1

另請參閱此[相關示例](http://stackoverflow.com/a/10097538/230513)。 – trashgod

回答

3

我認爲你應該改變在板上放置方塊的態度。你的態度有點令人困惑,因爲它試圖從電路板的右側創建一個偏移量,並且每次在循環中縮小尺寸和偏移量。

我用另一種方式從左至右計算平方的地方,現在它變短,更容易理解:

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

import javax.swing.JPanel; 

public class GrafikPanel extends JPanel { 
    @Override 
    public void paintComponent(Graphics g){ 
     super.paintComponent(g); 
     int width = g.getClipBounds().width; 
     int height = g.getClipBounds().height; 
     // 
     int rowOffset = width/10; 
     int colOffset = height/10; 
     int squareWidth = width/10; 
     // 
     int lines = 0; 
     while(lines < 8){ 

      int squares = 0; 

      while(squares < 8){ 
       if(squares%2 == 0) 
       { 
        g.setColor(lines%2 != 0 ? Color.BLACK : Color.WHITE); 
       } 
       else 
       { 
        g.setColor(lines%2 != 0 ? Color.WHITE : Color.BLACK); 
       } 
       g.fillRect(rowOffset+(squares*squareWidth), colOffset+(lines*squareWidth), squareWidth, squareWidth); 
       // 
       squares++; 
      } 

      lines++; 
     } 
    } 
    @Override 
    public Dimension getPreferredSize(){ 
     return new Dimension(500, 500); 
    } 
} 

而且我已經改變行從10數和列8.

希望這將是有益的,

好運。

+0

你的計算方式比我的更有意義,只要我有空,就試試它。你能告訴我什麼「?Color.BLACK:Color.WHITE」這行應該是?你用它代替了我的一些ifs,它看起來好多了,該操作是如何調用的?在我的情況下,我也必須得到窗口邊界的正方形,因此我從0軸開始,並且如果可以用你的方法添加/子高度將不得不嘗試。 –

+2

@CharlesNough這裏是一個快速簡單的教程,解釋操作:) [點擊](http://www.cafeaulait.org/course/week2/43.html) – Phantomazi

+0

@Charles Nough,因爲Phantomazi說它是一個三元運算符檢查條件之前?如果它是真的,則返回:操作符左側的語句,否則返回右側。 – STaefi