突出网格中的单元格 - java swing

时间:2014-02-16 22:57:32

标签: java swing grid paintcomponent repaint

我需要突出显示网格的不同单元格。在我的算法的每次迭代中,获得坐标(i,j)。我将这些值传递给Gridcurrent_xcurrent_y变量。如何通过调用paintComponent()类的TestPane方法为相应的单元格着色。

public class Grid {

    int current_x;
    int current_y;

    public static void main(String[] args) {
        new Grid();
    }

    public Grid() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                   UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
             }
        });
    }

    public void getCurrentValues( int x, int y ){

        this.current_x = x;
        this.current_y = y;
    }

    public class TestPane extends JPanel {

        // code to create grid using rectangles

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);

            // more code
        }

    }
}

我不清楚如何将(i,j)值传递给TestPane类以及如何调用paintComponent

1 个答案:

答案 0 :(得分:1)

  1. 提供TestPane setGridX(int x)setGridY(int y)方法。
  2. 为需要调用这些方法的任何类提供对显示的 TestPane实例的引用。
  3. 在发生触发更改的事件时调用方法。然后在TestPane实例上调用repaint()

  4. 修改
    新代码实际上证明了我的建议:

    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.Random;
    import javax.swing.*;
    
    public class Grid {
       private static final int TIMER_DELAY = 200;
       private int current_x;
       private int current_y;
       private TestPane testPane = new TestPane();
       private Random random = new Random();
    
       public Grid() {
          EventQueue.invokeLater(new Runnable() {
             @Override
             public void run() {
                try {
                   UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }
                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                // !! frame.add(new TestPane());
                frame.add(testPane); // !!
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
             }
          });
          new Timer(TIMER_DELAY, new ActionListener() {
             @Override
             public void actionPerformed(ActionEvent evt) {
                current_x = random.nextInt(TestPane.MAX_X);
                current_y = random.nextInt(TestPane.MAX_Y);
                testPane.setGridX(current_x);
                testPane.setGridY(current_y);
                testPane.repaint();
             }
          }).start();
       }
    
       public void getCurrentValues(int x, int y) {
          this.current_x = x;
          this.current_y = y;
       }
    
       public static void main(String[] args) {
          new Grid();
       }
    }
    
    class TestPane extends JPanel {
       public static final int MAX_X = 8;
       public static final int MAX_Y = MAX_X;
       private static final int CELL_WIDTH = 50;
       private static final int PREF_W = CELL_WIDTH * MAX_X;
       private static final int PREF_H = CELL_WIDTH * MAX_Y;
       private static final Color FILL_COLOR = Color.yellow;
       private static final Color GRID_COLOR = Color.black;
       private int gridX = -1;
       private int gridY = -1;
    
       @Override
       protected void paintComponent(Graphics g) {
          super.paintComponent(g);
          for (int y = 0; y < MAX_Y; y++) {
             for (int x = 0; x < MAX_X; x++) {
                if (x == gridX && y == gridY) {
                   g.setColor(FILL_COLOR);
                   g.fillRect(x * CELL_WIDTH, y * CELL_WIDTH, CELL_WIDTH, CELL_WIDTH);
                }
                g.setColor(GRID_COLOR);
                g.drawRect(x * CELL_WIDTH, y * CELL_WIDTH, CELL_WIDTH, CELL_WIDTH);
             }
          }
       }
    
       @Override
       public Dimension getPreferredSize() {
          return new Dimension(PREF_W, PREF_H);
       }
    
       public int getGridX() {
          return gridX;
       }
    
       public void setGridX(int gridX) {
          this.gridX = gridX;
       }
    
       public int getGridY() {
          return gridY;
       }
    
       public void setGridY(int gridY) {
          this.gridY = gridY;
       }
    }
    

      

    我目前正在Grid类中创建一个TestPane对象。

    是的,你绝对想要这样做。

      

    那么我应该在主要的Driver类中创建一个吗?

    不,应该只有一个TestPane实例。如果在两个位置需要它,则创建一次,然后通过方法或构造函数参数将其传递给另一个。

      

    另外,我应该如何触发调用此repaint()方法?我还不太清楚。

    这一切都取决于什么会触发current_x和current_y值的变化。您可能需要某种类型的侦听器,可能是MouseListener。在我的示例中,我使用Swing Timer持有的ActionListener随机移动选定的x和y值。


    编辑2
    你说:

      

    我的触发器是 - 在算法的每次迭代中 - 我得到新的(i,j)值..所以我如何合并...

    您可能会在后台线程中进行迭代,例如可以使用SwingWorker对象获取,然后调用TestPane的setGridX和setGridY方法,并重新绘制。