在grid-JavaSwing中突出显示单元格

在grid-JavaSwing中突出显示单元格,java,swing,grid,paintcomponent,repaint,Java,Swing,Grid,Paintcomponent,Repaint,我需要突出显示网格的不同单元格。在我的算法的每次迭代中,都会获得坐标(i,j)。我将这些值传递到网格到当前x和当前y变量。如何通过调用TestPane类的paintComponent()方法为相应的单元格着色 public class Grid { int current_x; int current_y; public static void main(String[] args) { new Grid(); } public Gr

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

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

  • 给出TestPane
    setGridX(intx)
    setGridY(inty)
    方法
  • 为需要调用这些方法的任何类提供对所显示的TestPane实例的引用
  • 当触发更改的事件发生时调用这些方法。然后在TestPane实例上调用
    repaint()

  • 编辑
    实际演示我的建议的新代码:

    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;
       }
    }
    
    import java.awt.*;
    导入java.awt.event.ActionEvent;
    导入java.awt.event.ActionListener;
    导入java.util.Random;
    导入javax.swing.*;
    公共类网格{
    专用静态最终int定时器_延迟=200;
    私有int当前_x;
    私人内部流动;
    私有TestPane TestPane=newtestpane();
    私有随机=新随机();
    公共电网(){
    invokeLater(新的Runnable(){
    @凌驾
    公开募捐{
    试一试{
    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    }catch(ClassNotFoundException |实例化Exception | IllegalacessException |不支持ookandfeelException ex){
    }
    JFrame=新JFrame(“测试”);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(新的BorderLayout());
    //!!frame.add(新测试窗格());
    frame.add(测试窗格);/!!
    frame.pack();
    frame.setLocationRelativeTo(空);
    frame.setVisible(true);
    }
    });
    新计时器(计时器延迟,新ActionListener(){
    @凌驾
    已执行的公共无效操作(操作事件evt){
    current_x=random.nextInt(TestPane.MAX_x);
    current_y=random.nextInt(TestPane.MAX_y);
    testPane.setGridX(当前_x);
    testPane.setGridY(当前值);
    testPane.repaint();
    }
    }).start();
    }
    公共无效GetCurrentValue(整数x,整数y){
    这个。当前_x=x;
    这个。当前_y=y;
    }
    公共静态void main(字符串[]args){
    新网格();
    }
    }
    类TestPane扩展了JPanel{
    公共静态最终int MAX_X=8;
    公共静态最终整数MAX_Y=MAX_X;
    专用静态最终整数单元格_宽度=50;
    私有静态最终int PREF_W=单元格宽度*最大值X;
    私有静态最终int PREF_H=单元格宽度*最大值;
    专用静态最终颜色填充颜色=Color.yellow;
    私有静态最终颜色网格_Color=Color.black;
    私有int gridX=-1;
    private int gridY=-1;
    @凌驾
    受保护组件(图形g){
    超级组件(g);
    对于(int y=0;y


    我目前正在网格类中创建一个TestPane对象

    是的,你肯定想这么做

    那么我应该在主驱动程序类中创建一个吗

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

    另外,我应该如何触发这个repaint()方法的调用?我还是不太清楚

    这一切都取决于什么会触发当前_x和当前_y值的变化。你可能需要某种类型的倾听者,也许是鼠标倾听者。在我的示例中,我使用Swing计时器持有的ActionListener随机移动选定的x和y值


    编辑2
    你说:


    我的触发器是——在算法的每次迭代中——我得到新的(I,j)值。。那么,我是如何将其合并的


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

    我目前正在Grid类中创建TestPane对象。那么我应该在主
    驱动程序
    类中创建一个吗?另外,我应该如何触发这个repaint()方法的调用?我还是不太清楚。@Dinesh:请检查我上面编辑的代码并运行程序。我试图在对我的答案的编辑中解决你上面的每一点。我的触发器是——在算法的每次迭代中——我得到新的(I,j)值。。因此,我如何合并..@Dinesh:对于这一点,关键是使用一个后台线程,例如可以通过Swing Worker获得的线程来进行迭代。我可以在算法的每次迭代中调用getCurrentValues()方法,更新testpane的值并调用testpane.repaint吗?