如何在java中通过更改纹理和颜色来创建交互式面板?

如何在java中通过更改纹理和颜色来创建交互式面板?,java,swing,graphics,awt,2d-games,Java,Swing,Graphics,Awt,2d Games,我是新来的,但在发布之前我做了一些研究。我的目标是使用一些有趣的想法创建一个简单的塔防游戏,并且使用javax.swing和java.awt训练我的开发技能。据我所知,开发人员大多是懒惰的家伙,他们尽一切努力使他们的生活更简单 有一个网格地图和地图加载我的游戏使用一个布尔矩阵和加载方法来定位面板上的地形。我认为这将是一个非常简单的解决方案。因为矩阵是12 x 12,所以我希望使用其他应用程序创建它,而不是输入一行144个数字 这里有一个想法,首先创建一个地图编辑器应用程序,然后在其中为标高绘制地

我是新来的,但在发布之前我做了一些研究。我的目标是使用一些有趣的想法创建一个简单的塔防游戏,并且使用javax.swing和java.awt训练我的开发技能。据我所知,开发人员大多是懒惰的家伙,他们尽一切努力使他们的生活更简单

有一个网格地图和地图加载我的游戏使用一个布尔矩阵和加载方法来定位面板上的地形。我认为这将是一个非常简单的解决方案。因为矩阵是12 x 12,所以我希望使用其他应用程序创建它,而不是输入一行144个数字

这里有一个想法,首先创建一个地图编辑器应用程序,然后在其中为标高绘制地图。当我有了这样一个工具,我可以直观地制作地图,然后将其布尔矩阵保存到一个文件中,稍后可以通过加载方法读取该文件并在游戏中重新创建。下一步是制作图形和面板,以便对用户的操作做出正确的反应。在左边有一个带有按钮的面板-用户单击其中一个按钮后,字段currentColor发生变化

此字段由实现actionListener并更改其构造函数中声明的面板颜色的方法使用。我想在单击某个面板时更改其颜色。我使用颜色是因为现在更容易使它工作,稍后我想用纹理替换颜色-显然,我知道我必须使用paintComponent方法,但我认为这也适用于它,对吗?当我将光标移到面板上时,如果面板边框的颜色发生变化,当鼠标移到其他地方时,面板边框的颜色又恢复到正常状态,这也会很好

这里的要点是,我在使面板具有交互性方面遇到了一些困难。第一个问题是面板是在for循环中创建的,这使得鼠标在面板上时很难引用某个面板。另一个是,我想改变面板的外观后,我点击它

据我所知,鼠标听写器应该做这项工作,但如何真正编写它以在屏幕上产生效果呢?我找到了一些关于这个的帖子,但对我来说,它不起作用。以下是链接:

我的代码:

import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class Editor extends JFrame
{
    private JButton towers = new JButton(new ImageIcon("pu.gif"));
    private JButton road = new JButton(new ImageIcon("pu.gif"));
    private JButton start = new JButton(new ImageIcon("pu.gif"));
    private JButton finish = new JButton(new ImageIcon("pu.gif"));
    private String mapTitle = "testmap";
    private Color currentColor;
    private int width = Toolkit.getDefaultToolkit().getScreenSize().width;
    private int height = Toolkit.getDefaultToolkit().getScreenSize().height;
    private String currentMapType = "Standard";
    private static final int currentHeight = 12;
    private static final int currentWidth = 12;
    private JPanel[][] currentMapPanel;
    private int[][] currentMapField;

    //Toolbar - a panel with buttons

    private JPanel panel = new JPanel(new GridLayout(10,3));

    //Container for map - a panel with map

    private Dimension containerSize = new Dimension(height, height);
    static JPanel container = new JPanel(new GridLayout(currentHeight, currentWidth), true);

    //Separator

    private JSplitPane separator = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, panel, container);

    public Editor()
    {
        initComponents();
    }

    public void initComponents()
    {
        this.setTitle(mapTitle + ".map" + " - " + "Game Map Editor");
        this.setSize(800, 600);

        int frameWidth = this.getSize().width;
        int frameHeight = this.getSize().height;
        this.setLocation((width - frameWidth) / 2, (height - frameHeight) / 2);

        this.setIconImage(Toolkit.getDefaultToolkit().getImage("pu.gif"));

        towers.addActionListener(e -> {
            currentColor = Color.CYAN;
            System.out.println(currentColor);
        });
        road.addActionListener(e -> {
            currentColor = Color.GRAY;
            System.out.println(currentColor);
        });
        start.addActionListener(e -> {
            currentColor = Color.LIGHT_GRAY;
            System.out.println(currentColor);
        });
        finish.addActionListener(e -> {
            currentColor = Color.BLACK;
            System.out.println(currentColor);
        });

        new Map(currentMapType, currentWidth, currentHeight, false);

        panel.add(towers);
        panel.add(road);
        panel.add(start);
        panel.add(finish);

        this.getContentPane().add(separator);

        this.setDefaultCloseOperation(EXIT_ON_CLOSE);

    }

    /**
     * Class that allows to load the graphic map and to view it in JFrame
     */
    public class Map
    {
        public Map(String mapType, int rows, int columns, boolean load)
        {
            if (!load)
            {
                currentMapPanel = mapPanel(rows, columns);
                currentMapField = new MapGenerator().mapFieldEmpty(rows, columns);
                mapLoader(currentMapField, currentMapPanel);
            }
            else
            {
                currentMapPanel = mapPanel(rows, columns);
                currentMapField = new MapGenerator().mapFieldGenerator(rows, columns);
                mapLoader(currentMapField, currentMapPanel);
            }
        }

        private JPanel[][] mapPanel(int rows, int columns)
        {
            JPanel[][] mapPanel = new JPanel[rows][columns];

            for (int i = 0; i < rows - 1; i++)
            {
                for (int j = 0; j < columns - 1; j++)
                {
                    mapPanel[i][j] = new JPanel(true);
                    mapPanel[i][j].setPreferredSize(new Dimension(height/12, height/12));
                    mapPanel[i][j].setBorder(new LineBorder(Color.BLACK));
                    mapPanel[i][j].addMouseListener(new MouseAdapter() {
                        @Override
                        public void mouseEntered(MouseEvent e) {
                            super.mouseEntered(e);
                            JPanel parent = (JPanel) e.getSource();
                            new colorListener(parent, Color.LIGHT_GRAY);
                            parent.revalidate();
                        }
                        @Override
                        public void mouseExited(MouseEvent e)
                        {
                            super.mouseExited(e);
                            JPanel parent = (JPanel) e.getSource();
                            new colorListener(parent, Color.GREEN);
                            parent.revalidate();
                        }
                        @Override
                        public void mouseClicked(MouseEvent e)
                        {
                            super.mouseClicked(e);
                            JPanel parent = (JPanel) e.getSource();
                            new colorListener(parent, currentColor);
                            parent.revalidate();
                        }
                    });
                }
            }
            return mapPanel;
        }

        private void mapLoader(int[][] mapField, JPanel[][] mapPanel)
        {
            for (int i = 0; i < mapField.length - 1; i++)
            {
                for (int j = 0; j < mapField.length - 1; j++)
                {
                    if (mapField[i][j] == 0)
                    {
                        mapPanel[i][j].setBackground(Color.GREEN);
                        container.add(mapPanel[i][j]);
                    }
                    else if (mapField[i][j] == 1)
                    {
                        mapPanel[i][j].setBackground(Color.GRAY);
                        container.add(mapPanel[i][j]);
                    }
                    else if (mapField[i][j] == 2)
                    {
                        mapPanel[i][j].setBackground(Color.LIGHT_GRAY);
                        container.add(mapPanel[i][j]);
                    }
                    else if (mapField[i][j] == 3)
                    {
                        mapPanel[i][j].setBackground(Color.BLACK);
                        container.add(mapPanel[i][j]);
                    }
                    else
                    {
                        System.out.println("An error occurred...");
                    }
                }
            }
        }
        private JPanel mapContainer(int rows, int columns)
        {
            container = new JPanel();
            container.setLayout(createLayout(rows, columns));
            container.setPreferredSize(containerSize);
            container.setBounds(height/4, height/4, containerSize.width, containerSize.height);
            return container;
        }
        private GridLayout createLayout(int rows, int columns){
            GridLayout layout = new GridLayout(rows, columns);
            return layout;
        }
    }

    private class colorListener implements ActionListener
    {
        public colorListener(JPanel p, Color c)
        {
            this.panel = p;
            this.color = c;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            panel.setBackground(color);
        }

        JPanel panel;
        Color color;
    }

    public static void main(String[] args) {
        new Editor().setVisible(true);
    }
}
import javax.swing.*;
导入javax.swing.border.LineBorder;
导入java.awt.*;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.awt.event.MouseAdapter;
导入java.awt.event.MouseEvent;
公共类编辑器扩展了JFrame
{
私有JButton towers=新JButton(新图像图标(“pu.gif”);
私人JButton路=新JButton(新图像图标(“pu.gif”);
私有JButton start=newjbutton(newimageicon(“pu.gif”);
私有JButton finish=新JButton(新图像图标(“pu.gif”);
私有字符串mapTitle=“testmap”;
私人色彩;
private int width=Toolkit.getDefaultToolkit().getScreenSize().width;
private int height=Toolkit.getDefaultToolkit().getScreenSize().height;
私有字符串currentMapType=“标准”;
专用静态最终int currentHeight=12;
专用静态最终int currentWidth=12;
私有JPanel[][]currentMapPanel;
私有int[]currentMapField;
//工具栏-带有按钮的面板
私有JPanel面板=新JPanel(新网格布局(10,3));
//地图容器-带有地图的面板
专用标注容器化=新标注(高度、高度);
静态JPanel容器=新JPanel(新网格布局(currentHeight,currentWidth),true);
//分离器
专用JSplitPane分离器=新JSplitPane(JSplitPane.HORIZONTAL_SPLIT,panel,container);
公共编辑()
{
初始化组件();
}
公共组件()
{
这个.setTitle(mapTitle+”.map“+”-“+”游戏地图编辑器);
这个。设置大小(800600);
int frameWidth=this.getSize().width;
int frameHeight=this.getSize().height;
此.setLocation((宽度-帧宽度)/2,(高度-帧高度)/2);
这个.setIconImage(Toolkit.getDefaultToolkit().getImage(“pu.gif”);
towers.addActionListener(e->{
currentColor=Color.CYAN;
System.out.println(当前颜色);
});
road.addActionListener(e->{
currentColor=Color.GRAY;
System.out.println(当前颜色);
});
start.addActionListener(e->{
currentColor=Color.LIGHT\u GRAY;
System.out.println(当前颜色);
});
finish.addActionListener(e->{
currentColor=Color.BLACK;
System.out.println(当前颜色);
});
新映射(currentMapType、currentWidth、currentHeight、false);
面板。添加(塔楼);
增补(道路);
面板。添加(开始);
面板。添加(饰面);
this.getContentPane().add(分隔符);
此.setDefaultCloseOperation(关闭时退出);
}
/**
*类,该类允许加载图形地图并在JFrame中查看它
*/
公共类地图
{
公共映射(字符串映射类型、整数行、整数列、布尔加载)
{
如果(!加载)
{
currentMapPanel=mapPanel(行、列);
currentMapField=new MapGenerator().mapFieldEmpty(行、列);
映射加载器(currentMapField、currentMapPanel);
}
其他的
{
currentMapPanel=mapPanel(行、列);
currentMapField=new MapGenerator().mapFieldGenerator(行、列);
映射加载器(currentMapField、currentMapPanel);
}
}
专用JPanel[][]映射面板(int行,int列)
{
JPanel[][]映射面板=新的JPanel[行][列];
对于(int i=0;istatic JPanel container = new JPanel(new GridLayout(currentHeight, currentWidth), true);
this.setSize(800, 600);

int frameWidth = this.getSize().width;
int frameHeight = this.getSize().height;
this.setLocation((width - frameWidth) / 2, (height - frameHeight) / 2);
new Map(currentMapType, currentWidth, currentHeight, false);
mapPanel[i][j].setPreferredSize(new Dimension(height / 12, height / 12));
@Override
public void mouseEntered(MouseEvent e) {
    super.mouseEntered(e);
    JPanel parent = (JPanel) e.getSource();
    new colorListener(parent, Color.LIGHT_GRAY);
    parent.revalidate();
}
container.setPreferredSize(containerSize);
container.setBounds(height / 4, height / 4, containerSize.width, containerSize.height);