import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//import java.awt.Graphics2D;
import java.awt.*;
import javax.swing.*;
import java.awt.geom.*;
import java.awt.image.BufferedImage;
import java.io.*;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
public class gui extends JFrame implements ActionListener {
environment env;
JTextArea text_area;
JLabel lblchart;
public gui(environment envr) {
env=envr;
setTitle("Simple Hunter Prey Simulator");
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.X_AXIS));
JPanel SimulationPanel = new JPanel();
SimulationPanel.setLayout(new BorderLayout(10,10));
JPanel ResultsPanel = new JPanel();
mainPanel.add(SimulationPanel);
mainPanel.add(ResultsPanel);
this.add(mainPanel);
lblchart=new JLabel();
lblchart.setVisible(false);
ResultsPanel.add(lblchart);
JMenuBar MyMenuBar=new JMenuBar();
JMenu menuFile=new JMenu("File");
JMenuItem itemQuit=new JMenuItem("Quit");
menuFile.add(itemQuit);
MyMenuBar.add(menuFile);
JMenu menuWeights=new JMenu("Weights");
JMenuItem itemSaveQ=new JMenuItem("Save Q table");
menuWeights.add(itemSaveQ);
JMenuItem itemLoadQ=new JMenuItem("Load Q table");
menuWeights.add(itemLoadQ);
MyMenuBar.add(menuWeights);
SimulationPanel.add(MyMenuBar,BorderLayout.PAGE_START);
MyPanel panel1=new MyPanel(env);
panel1.setPreferredSize(new Dimension(300, 300));
SimulationPanel.add(panel1,BorderLayout.CENTER);
JPanel panel2=new JPanel();
panel2.setLayout(new BoxLayout(panel2, BoxLayout.Y_AXIS));
JPanel panel2_1=new JPanel();
panel2_1.setLayout(new BoxLayout(panel2_1, BoxLayout.X_AXIS));
JButton buton1=new JButton("One Step");
buton1.addActionListener(this);
panel2_1.add(buton1);
buton1=new JButton("One Episode");
buton1.addActionListener(this);
panel2_1.add(buton1);
buton1=new JButton("200 Episodes");
buton1.addActionListener(this);
panel2_1.add(buton1);
panel2.add(panel2_1);
text_area = new JTextArea("Output area if needed");
text_area.setPreferredSize(new Dimension(100, 70));
panel2.add(text_area);
SimulationPanel.add(panel2,BorderLayout.PAGE_END);
setSize(new Dimension(400,500));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
//System.out.println("One Step Button Pressed!"+e.getSource().toString());
if (e.getActionCommand().equals("One Step"))
{
env.oneStep(true);
System.out.println("One Step Button Pressed!");
text_area.setText("One step Applied");
repaint();
for(int ag=0;ag<env.agents.length;ag++)
{
text_area.setText(text_area.getText()+"\n"+env.agents[ag].text_area);
}
}
if (e.getActionCommand().equals("One Episode"))
{
System.out.println("One Episode Button Pressed!");
env.oneEpisode();
text_area.setText("One episode applied, took "+env.numberLastEpisodeSteps+" steps.");
repaint();
}
if (e.getActionCommand().equals("200 Episodes"))
{
System.out.println("200 Episodes Button Pressed!");
do
{
env.oneEpisode();
}
while((env.numberEpisodes % 200 !=0) || (env.numberEpisodeSteps != 0));
for(agent ag:env.agents)
{
ag.epsilon=1.0;
}
text_area.setText("200 episode applied, Epsilon set to 1, ready to apply learned");
exportGraph();
repaint();
}
}
void exportGraph()
{
XYSeriesCollection dataset = new XYSeriesCollection();
dataset.addSeries(env.xydata);
// Generate the graph
JFreeChart chart = ChartFactory.createXYLineChart("XY Chart", // Title
"x-axis", // x-axis Label
"y-axis", // y-axis Label
dataset, // Dataset
PlotOrientation.VERTICAL, // Plot Orientation
true, // Show Legend
true, // Use tooltips
false // Configure chart to generate URLs?
);
BufferedImage chartImage = chart.createBufferedImage(300,200);
if(!lblchart.isVisible())
{
this.setSize(this.getWidth()+300,this.getHeight());
lblchart.setVisible(true);
}
lblchart.setIcon(new ImageIcon(chartImage));
try {
ChartUtilities.saveChartAsJPEG(new File("chart.jpg"), chart, 500,
300);
} catch (Exception e) {
System.out.println("Problem occurred creating chart.");
}
}
public static void main(String[] args) {
environment env=new environment();
new gui(env);
}
}
class MyPanel extends JPanel {
Grid theGrid;
public MyPanel(environment env) {
int rows=env.height;
int columns=env.width;
//setBorder(BorderFactory.createLineBorder(Color.black));
theGrid= new Grid(env,rows,columns,30*rows,30*columns);
addComponentListener(new ComponentAdapter(){
@Override
public void componentResized(ComponentEvent e) {
theGrid.cell_height=e.getComponent().getHeight()/theGrid.row;
theGrid.cell_width=e.getComponent().getWidth()/theGrid.column;
//System.out.println("Height:"+theGrid.cell_height);
repaint();
//super.componentResized(e);
}
}
);
}
public void paintComponent(Graphics g) {
//super.paintComponent(g);
//g.drawString("This is my custom Panel!",10,20);
//System.out.println("Height: Printing the grid!");
theGrid.paintGrid(g);
}
}
class Grid{
int cell_height;
int cell_width;
int row;
int column;
environment env;
public Grid(environment envr,int i, int j,int height, int width)
{
env=envr;
cell_height=height/i;
cell_width=width/j;
row=i;
column=j;
}
public void paintGrid(Graphics g){
g.setColor(Color.LIGHT_GRAY);
g.fillRect(0,0,column*cell_width,row*cell_height);
for(int i=0;i<column;i++)
for(int j=0;j<row;j++)
{
g.setColor(Color.BLACK);
if(env.obstacles[j*env.width+i]==1)
g.fillRect(i*cell_width,j*cell_height,cell_width,cell_height);
else
g.drawRect(i*cell_width,j*cell_height,cell_width,cell_height);
}
drawPrey(g);
drawHunters(g);
drawWalls(g);
}
void drawWalls(Graphics g)
{
g.setColor(Color.black);
for(int i=0;i<env.walls.length;i++)
{
int posx=i%env.width;
int posy=i/env.width;
if(env.walls[i][1]==1)
g.fillRect(posx*cell_width,posy*cell_height,cell_width/4,cell_height);
if(env.walls[i][2]==1)
g.fillRect(posx*cell_width,posy*cell_height,cell_width,cell_height/4);
if(env.walls[i][3]==1)
g.fillRect((posx+1)*cell_width-cell_width/4,posy*cell_height,cell_width/4,cell_height);
if(env.walls[i][4]==1)
g.fillRect(posx*cell_width,(posy+1)*cell_height-cell_height/4,cell_width,cell_height/4);
}
}
void drawPrey(Graphics g)
{
g.setColor(Color.red);
for (int i=0;i<env.preys.length;i++)
{
if (! env.preys[i].captured)
g.drawRect(env.preys[i].posx*cell_width+cell_width/4,env.preys[i].posy*cell_height+cell_height/4,cell_width/2,cell_height/2);
}
}
void drawHunters(Graphics g)
{
g.setColor(Color.blue);
for (int i=0;i<env.agents.length;i++)
{
int centerx=env.agents[i].posx*cell_width+cell_width/2;
int centery=env.agents[i].posy*cell_height+cell_height/2;
int square_width=cell_width/3;
int square_height=cell_height/3;
g.drawRect(centerx-square_width/2,centery-square_height/2,square_width,square_height);
int tox=centerx;
int toy=centery;
if(env.allActions[i]==1)
tox-=cell_width/2;
if(env.allActions[i]==2)
toy-=cell_height/2;
if(env.allActions[i]==3)
tox+=cell_width/2;
if(env.allActions[i]==4)
toy+=cell_height/2;
paintarrow(g, centerx, centery, tox, toy);
g.drawString("A"+Integer.toString(env.agents[i].agentId), centerx-square_width/2, centery+square_height/2);
}
}
protected void paintarrow(Graphics g,int fromx,int fromy,int tox,int toy)
{
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
Point from = new Point(fromx, fromy);
Point to = new Point(tox, toy);
g2.draw(new Line2D.Double(from, to));
if (tox!=fromx || toy!=fromy)
drawArrowHead(g2, to, from, Color.blue);
//drawArrowHead(g2, ne, sw, Color.blue);
}
private void drawArrowHead(Graphics2D g2, Point tip, Point tail, Color color)
{
double phi = Math.toRadians(40);
int barb = 20;
g2.setPaint(color);
double dy = tip.y - tail.y;
double dx = tip.x - tail.x;
double theta = Math.atan2(dy, dx);
//System.out.println("theta = " + Math.toDegrees(theta));
double x, y, rho = theta + phi;
for(int j = 0; j < 2; j++)
{
x = tip.x - barb * Math.cos(rho);
y = tip.y - barb * Math.sin(rho);
g2.draw(new Line2D.Double(tip.x, tip.y, x, y));
rho = theta - phi;
}
}
// // Create a simple pie chart
// DefaultPieDataset pieDataset = new DefaultPieDataset();
// pieDataset.setValue("A", new Integer(75));
// pieDataset.setValue("B", new Integer(10));
// pieDataset.setValue("C", new Integer(10));
// pieDataset.setValue("D", new Integer(5));
//
//
//
// chart = ChartFactory.createPieChart(
// "CSC408 Mark Distribution",
// pieDataset,
// true,
// true,
// false);
//
//
// try {
// ChartUtilities.saveChartAsJPEG(new File("chart.jpg"), chart, 500,
// 300);
// } catch (Exception e) {
// System.out.println("Problem occurred creating chart.");
// }
}