主要的功能界面:就是定义地图绘制的位置,按钮等控件的位置,然后吧界面稍微调的和谐一点
package AStarAlgo;
import QHHelper.SAbsolutePathHelper;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
/**
* <p> TODU </P>
*
* @author yeager
* @Version V1.1.1.0
* @date 2023/11/24 13:45
*/
public class JAStarPanel extends JPanel {
JAGridPanel gridPanel;
JSplitPane centerPane;
int width;
int height;
public JAStarPanel()
{
this.setLayout(new BorderLayout());
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int screenWidth = screenSize.width;
int screenHeight = screenSize.height;
width = screenWidth * 2/3;
height = screenHeight/2;
setSize(width,height);
centerPane = new JSplitPane();
centerPane.setPreferredSize(new Dimension(width *2 / 3 ,height));
centerPane.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
centerPane.setDividerLocation(width *2 /3 *5/6);
}
});
JAlgoBKPanel rrightPane;
JAlgoBKPanel rleftPane;
try {
rrightPane = new JAlgoBKPanel ("preview1.png");
rleftPane = new JAlgoBKPanel ("preview2.png");
rrightPane.setPreferredSize(new Dimension(width /6,height));
rleftPane.setPreferredSize(new Dimension(width /6,height));
this.add(rrightPane,BorderLayout.WEST);
this.add(rleftPane,BorderLayout.EAST);
} catch (FileNotFoundException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
this.add(centerPane,BorderLayout.CENTER);
centerPane.setOrientation(JSplitPane.HORIZONTAL_SPLIT);
centerPane.setDividerLocation(width *2 /3 *5/6);
centerPane.setDividerSize(3);
gridPanel = new JAGridPanel();
gridPanel.setPreferredSize(new Dimension(width *2 / 3*5/6 ,height));
centerPane.setLeftComponent(gridPanel);
JPanel rightPane = new JPanel(new GridLayout(6, 1));
rightPane.setPreferredSize(new Dimension(width *2 / 3*1/6 ,height));
centerPane.setRightComponent(rightPane);
JLabel instruLabel = new JLabel()
{
public void paintComponent(Graphics g)
{
super.paintComponents(g);
String imgPath = SAbsolutePathHelper.getImagePath()
+ File.separator+"img"+ File.separator+"AStar.png";
String imgPath2 =SAbsolutePathHelper.getImagePath()
+ File.separator+"img"+File.separator+"btStartYellow.png";
BufferedImage image;
try {
image = ImageIO.read(new FileInputStream(imgPath));
Graphics2D graph = (Graphics2D) g;
if (image != null) {
graph.drawImage(image,0,0,this.getWidth(),this.getHeight(),this);
}
image = ImageIO.read(new FileInputStream(imgPath2));
graph.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 0.3f));
graph.drawImage( image, 0, 0, this.getWidth(), this.getHeight(), null);
} catch (FileNotFoundException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
//g.drawImage(im,0,0,this.getWidth(),this.getHeight(),this);
}
};
instruLabel.setText("绿色代表起点,红色代表终点,黑色代表障碍物,蓝色代表路径,灰色代表闭点集,橘色代表开点集");
instruLabel.setPreferredSize(new Dimension(width *2 / 3*5/6 ,height/6));
rightPane.add(instruLabel);
MyButton bt1 = new MyButton("选择起点","btStartGreen.png");
bt1.setPreferredSize(new Dimension(width *2 / 3*5/6 ,height/6));
bt1.setOpaque(false);
bt1.setMargin(new Insets(0,0,0,0));//设置边距
bt1.setBorderPainted(false);//不绘制边框
bt1.setFocusPainted(false);//选中后不绘制边框
bt1.setContentAreaFilled(false);//不绘制按钮区域
bt1.setHorizontalAlignment(JButton.CENTER);
bt1.setVerticalAlignment(JButton.CENTER);
rightPane.add(bt1);
bt1.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO 自动生成的方法存根
gridPanel.setStart();
}
});
MyButton bt2 = new MyButton("选择终点","btStartRed.png");
bt2.setPreferredSize(new Dimension(width *2 / 3*5/6 ,height/6));
bt2.setOpaque(false);
bt2.setMargin(new Insets(0,0,0,0));//设置边距
bt2.setBorderPainted(false);//不绘制边框
bt2.setFocusPainted(false);//选中后不绘制边框
bt2.setContentAreaFilled(false);//不绘制按钮区域
bt2.setHorizontalAlignment(JButton.CENTER);
bt2.setVerticalAlignment(JButton.CENTER);
rightPane.add(bt2);
bt2.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO 自动生成的方法存根
gridPanel.setEnd();
}
});
MyButton bt3 = new MyButton("选择障碍","btStartBlack.png");
bt3.setPreferredSize(new Dimension(width *2 / 3*5/6 ,height/6));
bt3.setOpaque(false);
bt3.setMargin(new Insets(0,0,0,0));//设置边距
bt3.setBorderPainted(false);//不绘制边框
bt3.setFocusPainted(false);//选中后不绘制边框
bt3.setContentAreaFilled(false);//不绘制按钮区域
bt3.setHorizontalAlignment(JButton.CENTER);
bt3.setVerticalAlignment(JButton.CENTER);
rightPane.add(bt3);
bt3.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO 自动生成的方法存根
gridPanel.setWall();
}
});
MyButton bt4 = new MyButton("开始搜索","btStartSearch.png");
bt4.setPreferredSize(new Dimension(width *2 / 3*5/6 ,height/6));
bt4.setOpaque(false);
bt4.setMargin(new Insets(0,0,0,0));//设置边距
bt4.setBorderPainted(false);//不绘制边框
bt4.setFocusPainted(false);//选中后不绘制边框
bt4.setContentAreaFilled(false);//不绘制按钮区域
bt4.setHorizontalAlignment(JButton.CENTER);
bt4.setVerticalAlignment(JButton.CENTER);
rightPane.add(bt4);
bt4.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO 自动生成的方法存根
gridPanel.setAfterStartSearch();
gridPanel.excuteAlgo();
}
});
MyButton bt5 = new MyButton("清空地图","btStartReset.png");
bt5.setPreferredSize(new Dimension(width *2 / 3*5/6 ,height/6));
bt5.setOpaque(false);
bt5.setMargin(new Insets(0,0,0,0));//设置边距
bt5.setBorderPainted(false);//不绘制边框
bt5.setFocusPainted(false);//选中后不绘制边框
bt5.setContentAreaFilled(false);//不绘制按钮区域
bt5.setHorizontalAlignment(JButton.CENTER);
bt5.setVerticalAlignment(JButton.CENTER);
rightPane.add(bt5);
bt5.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO 自动生成的方法存根
gridPanel.clear();
}
});
}
}