奇怪的刷新行为

时间:2012-05-25 10:24:43

标签: java user-interface

我制作了一个可以放置标记的网格。我的问题是,当我调整窗口大小时,这些标记有时会出现,有时则不会出现。而且,当我遮挡窗户的一部分时,我失去了该区域的标记。

代码非常简单:

import javax.swing.*;
import java.lang.Math;
import java.awt.*;
import java.awt.event.*;

enum markerColor {
    red, green, none
};

public class tictactoe extends JFrame {
    private class Plansza extends JPanel {
    private int x0,y0,gridSize,step;
    private Graphics graph;
    private markerColor[][] grid;
    public Plansza() {
        grid = new markerColor[29][29];
        for(int i = 0; i<29;i++) {
        for(int j = 0; j<29;j++) {
            grid[i][j] = markerColor.none;
            }
        }

        addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
            int x = e.getX(), y = e.getY();
            x-=x0; y-=y0;
            if(x>=gridSize || y>=gridSize) return;
            int X = x/step, Y = y/step;
            placeMarker(X,Y, markerColor.green);
            grid[X][Y] = markerColor.green;
            }
        });
    }
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        int width = getWidth();
        int height = getHeight();
        gridSize = Math.min(width,height);
        step = gridSize/29;
        gridSize = step*29;
        x0 = (width-gridSize)/2;
        y0 = (height-gridSize)/2;
        graph = getGraphics();

        g.setColor(Color.black);
        for(int i = 1, pointer=step; i<=28; i++, pointer+=step) {
        g.drawLine(x0+pointer,y0,x0+pointer,y0+gridSize);
        g.drawLine(x0,y0+pointer,x0+gridSize,y0+pointer);
        }

        for(int i = 0; i<29;i++) {
        for(int j = 0; j<29;j++) {
            if(grid[i][j] != markerColor.none) {
            placeMarker(i,j,grid[i][j]);
                }
            }
        }
        }
    public void placeMarker(int X, int Y, markerColor m) {
        int x = X*step+2*step/10, y = Y*step+2*step/10;
        switch(m) {
        case red:
        graph.setColor(Color.red);
        break;
        case green:
        graph.setColor(Color.green);
        break;
        }
        graph.fillOval(x0+x,y0+y, step*6/10, step*6/10);
    }
    }
    private Plansza plansza = new Plansza();
    public tictactoe() {
    add(plansza);
    }
    public static void main(String[] args) {
    tictactoe t = new tictactoe();
    t.setTitle("tictactoe");
    t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    t.setSize(500,500);
    t.setVisible(true);
    }
}

我在paintComponent中尝试了validate()但没有成功。我怎样才能改善这个?

1 个答案:

答案 0 :(得分:0)

我试图用它制作一个SSCCE并且工作正常

public class SSCCE extends JPanel {

    enum markerColor {
        red, green, none
    };

    markerColor grid [][];
    int gridSize = 29;
    int x0 = 0;
    int y0=0;
    int step = 1;
    Graphics graph;
    public SSCCE() {
        grid = new markerColor[29][29];
        for(int i = 0; i<29;i++)
            for(int j = 0; j<29;j++)
                grid[i][j] = markerColor.none;

        addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                int x = e.getX(), y = e.getY();
                x-=x0; y-=y0;

                int X = x/step, Y = y/step;

                //Changed here
                if(X>=gridSize || Y>=gridSize || X < 0 || Y < 0) return;

                placeMarker(X,Y, markerColor.green);
                grid[X][Y] = markerColor.green;

                //Call repaint in the mouse click 
                repaint();
            }
        });
    }
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        int width = getWidth();
        int height = getHeight();
        int gridSize = Math.min(width,height);
        step = gridSize/29;
        gridSize = step*29;
        x0 = (width-gridSize)/2;
        y0 = (height-gridSize)/2;
        graph = g;

        graph.setPaintMode();
        g.setColor(Color.black);
        for(int i = 1, pointer=step; i<=28; i++, pointer+=step) {
            g.drawLine(x0+pointer,y0,x0+pointer,y0+gridSize);
            g.drawLine(x0,y0+pointer,x0+gridSize,y0+pointer);
        }

        g.fillOval(10, 10, 40, 50);
        for(int i = 0; i<29;i++)
            for(int j = 0; j<29;j++)
                if(grid[i][j] != markerColor.none)
                    placeMarker(i,j,grid[i][j]);
    }
    public void placeMarker(int X, int Y, markerColor m) {
        int x = X*step+2*step/10, y = Y*step+2*step/10;
        switch(m) {
        case red:
            graph.setColor(Color.red);
            break;
        case green:
            graph.setColor(Color.green);
            break;
        default :
            graph.setColor(Color.blue);
        }
        graph.fillOval(x0+x,y0+y, step*6/10, step*6/10);

    }


    public static void main(String arg[]){
        JFrame frame = new JFrame();
        frame.add(new SSCCE(), BorderLayout.CENTER);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

    }
}

你不应该做的2件事

  1. 请勿检查鼠标单击以获取(x,y)组件,而是首先将x,y对齐grid(X,Y)
  2. 请勿revalidate因为您没有更改面板的结构,但只添加了新绘制的组件,请更好地使用repaint