2015-10-20 50 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