10x10棋盘与paintComponent

时间:2015-10-20 11:08:19

标签: java swing

我的任务是通过paintComponent方法创建一个10x10可调整大小的棋盘   我的问题是我得到了第一行,但下一行没有显示,我根本不知道我的错误在哪里

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 个答案:

答案 0 :(得分: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。

希望这会有所帮助,

祝你好运。

相关问题