import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.awt.Point;
import java.awt.event.MouseEvent;
import javax.swing.event.MouseInputAdapter;
import javax.swing.event.MouseInputListener;
import java.lang.System;
import java.util.Timer;
import java.io.*;
import javax.sound.sampled.*;
public class Drawpath extends JFrame implements
Runnable,ActionListener//,MouseListener,MouseMotionListener
{
int show_antmove=0;
public static int count_chess=0;
public static String information="蚂蚁家族在准备!";
public static String information1=" ";
public static String information2="小蚂蚁们已经搬回"+count_chess+"块食物";
public static int count_ant=1;//设置蚂蚁数量可增加
int startAble=0;
int put_formicaryAbl=0;
int put_cheeseAbl=0;
int paint_struckAbl=0;
public static ArrayList<Point> block_start=new ArrayList<Point>();
public static ArrayList<Point> block_end=new ArrayList<Point>();
public static ArrayList<Rectangle> list=new ArrayList<Rectangle>();
Rectangle current;
Image ig_formicary = getToolkit().getImage("formicary.jpg");
Image ig_cheese = getToolkit().getImage("cheese.jpg");
Thread ko_Thread;
//Timer timer;
int X_1=400;
int Y_1=400;
int X_2=50;
int Y_2=100;
World world=new World();//定义环境
//初始化蚂蚁
public static Ant[] ant={new Ant(50,105),new Ant(50,105),new Ant(50,105),new Ant
(50,105),
new Ant(50,105),new Ant(50,105),new Ant(50,105),new Ant(50,105),
new Ant(50,105),new Ant(50,105),new Ant(50,105),new Ant(50,105),
new Ant(50,105),new Ant(50,105),new Ant(50,105),new Ant(50,105),
new Ant(50,105),new Ant(50,105),new Ant(50,105),new Ant(50,105)};
JPanel control,canvas;
JMenuBar m_MenuBar=new JMenuBar(); //创建菜单条
JMenu menuFile = new JMenu("帮助"); //创建菜单
JMenuItem[] file={new JMenuItem("开始"), //创建各菜单项
new JMenuItem("退出"),
new JMenuItem("信息") };
JButton startButton=new JButton ("开始");
JButton stopButton=new JButton ("停止");
JButton reset=new JButton ("重置");
JButton add_ant=new JButton ("增加蚂蚁");
JButton paint_struck=new JButton ("画障碍");
JButton exitButton=new JButton ("退出");
JButton Put_formicaryButton=new JButton ("放置蚁窝");
JButton Put_cheeseButton=new JButton ("放置食物");
JButton finish_struckButton=new JButton ("完成画障碍");
public final static Color blue=new Color(0,0,255);
public final static Color red=new Color(255,0,0);
public final static Color black=new Color(0,0,0);
public Drawpath()//实现画板类
{
super("蚁群算法");
//Container c1=getContentPane();
//c1.setLayout(new FlowLayout());
//jb=new JButton("显示蚂蚁行走路径");
//c1.add(jb);
/*timer = new Timer();
timer.schedule(new TimerTask(){
public void run() {
//Pheromone_changed();//改变环境中的信息素
for(int i=0;i<count_ant;i++){
if(ant[i].state==0){
find_bestway(ant[i]);//寻找最优路径
move(ant[i]);//移动一步
//Find_Food(ant[i]);//判断是否找到食物
}
else{
put_food_Pheromone(ant[i]);//散信息素
ant_return(ant[i]);//安远路经回窝
//repaint();
//ant[i].ant_x=50;ant[i].ant_y=100;
//ant[i].state=0;//状态改为找食物
}
}
repaint(); //重画
}},0, 200);*/
this.setSize(1000,600); //设置框架大小
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
list = new ArrayList<Rectangle>();
canvas= new JPanel();
addMouseMotionListener(mouseHandler);
addMouseListener(mouseHandler);
control = new JPanel();
m_MenuBar.add(menuFile); //把菜单加入菜单条
menuFile.add(file[0]); //把各菜单项加入菜单
menuFile.add(file[1]);
menuFile.add(file[2]);
setLayout(new BorderLayout());//定义布局样式
canvas.setSize(1000,500);
canvas.setBackground(Color.white);
//加按钮和初始化control panel
control.add( startButton );
control.add( stopButton );
control.add( reset );
control.add( add_ant);
control.add( paint_struck);
control.add( exitButton);
control.add( Put_formicaryButton);
control.add( Put_cheeseButton);
control.add( finish_struckButton);
control.setSize(1000,50);
control.setBackground(Color.black);
//加按钮监听器
exitButton.addActionListener(this);
startButton.addActionListener(this);
stopButton.addActionListener(this);
reset.addActionListener(this);
add_ant.addActionListener(this);
paint_struck.addActionListener(this);
Put_cheeseButton.addActionListener(this);
Put_formicaryButton.addActionListener(this);
finish_struckButton.addActionListener(this);
//加菜单监听器
file[0].addActionListener(this);
file[1].addActionListener(this);
file[2].addActionListener(this);
//布置框架
add(canvas,BorderLayout.CENTER);
add(control,BorderLayout.SOUTH);
setJMenuBar(m_MenuBar); //把菜单条加入Frame
setVisible(true);
}
public void paint(Graphics g)//画图函数
{
super.paint(g);
information2="小蚂蚁们已经搬回"+count_chess+"块食物";
g.drawString(information,200,45);
g.drawString(information1,400,45);
g.drawString(information2,600,45);
g.setColor(blue);
float lineWidth = 5.0f;
//判断是否可画障碍
if(paint_struckAbl==1){
for (Rectangle rect : list) {
g.fillRect(rect.x, rect.y, rect.width, rect.height);
}
}
if (current != null) {
g.drawRect(current.x, current.y, current.width, current.height);
}
g.setColor(red);
//画窝和食物
g.drawImage(ig_cheese, X_1,Y_1, 10,10,this);
g.drawImage(ig_formicary, X_2,Y_2, 10,10,this);
g.setColor(black);
((Graphics2D)g).setStroke(new BasicStroke(lineWidth));
//画蚂蚁跑步图没什么作用
Image ig = getToolkit().getImage("ant.jpg");
Image ig1 = getToolkit().getImage("ant1.jpg");
Image ig2 = getToolkit().getImage("ant2.jpg");
Image ig3 = getToolkit().getImage("ant3.jpg");
if( show_antmove==0){
g.drawImage(ig,50,560,70,40,this);
show_antmove=1;}
else if ( show_antmove==1){
g.drawImage(ig2,50,560,70,40,this);
show_antmove=2;}
else
{g.drawImage(ig3,50,560,70,40,this);
show_antmove=0;}
//画蚂蚁所在位置图
for(int i=0;i<count_ant;i++){
if(ant[i].state==0)
g.drawImage(ig,ant[i].ant_x,ant[i].ant_y,10,10,this);
else
g.drawImage(ig1,ant[i].ant_x,ant[i].ant_y,10,10,this);
}
//g.drawImage(ig,ant[0].ant_x,ant[0].ant_y,10,10,this);
//g.drawImage(ig2,ant[1].ant_x,ant[1].ant_y,10,10,this);
}
//线程开始
public void start(){
if(ko_Thread==null){
ko_Thread=new Thread(this);
ko_Thread.start();
}
}
//结束
public void stop(){
if(ko_Thread!=null){
ko_Thread=null;
}
}
//执行
public void run(){
Thread thisThread=Thread.currentThread();
while(ko_Thread==thisThread){
//Pheromone_changed();//改变环境中的信息素
for(int i=0;i<count_ant;i++){
if(ant[i].state==0){
//find_bestway(ant[i]);//寻找最优路径
if_repeat(ant[i]);
move(ant[i]);//移动一步
}
else{
ant_return(ant[i]);//安远路经回窝并释放信息素
put_food_Pheromone(ant[i]);
}
}
repaint(); //重画
try{
Thread.sleep(150);
}catch(InterruptedException e){}
}
}
//按钮事件响应
public void actionPerformed(ActionEvent e){
Object source=e.getSource();
if(source==startButton){//开始
/*Ant []ant=new Ant[20];
for(int i=0;i<20;i++){
ant[i]
- 1
- 2
前往页