import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
class Sciezka extends JFrame implements MouseListener {
private JLabel status;
private JPanel panelR, panelL;
private JDialog[] dialog;
private JTextField wspPoczX, wspPoczY, wspKoncX, wspKoncY, iloscEtapow,
karaW, karaZ;
private String karaC;
MainMSID operator = new MainMSID();
private Point[] recznaS;
private BufferedImage teren;
private JLabel kara;
public Sciezka() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(50, 50, 800, 700);
setResizable(false);
setVisible(true);
setTitle("MSiD");
addMouseListener(this);
status = new JLabel("Start");
add(status);
getContentPane().add(BorderLayout.SOUTH, status);
JMenuBar menu = new JMenuBar();
JMenu plik = new JMenu("Menu");
karaC = Integer.toString(0);
recznaS = new Point[0];
teren = null;
JMenuItem zakoncz = new JMenuItem();
zakoncz.setAction(new AbstractAction() {
public void actionPerformed(ActionEvent ev) {
for (Frame frame : JFrame.getFrames()) {
if (frame.isActive()) {
status.setText("Zamykanie...");
Toolkit.getDefaultToolkit().getSystemEventQueue()
.postEvent(
new WindowEvent(frame,
WindowEvent.WINDOW_CLOSING));
}
}
}
});
zakoncz.setText("Zakoncz");
JMenuItem reset = new JMenuItem();
reset.setAction(new AbstractAction() {
public void actionPerformed(ActionEvent ev) {
status.setText("Reset...");
SwingUtilities.invokeLater(new Runnable() {
public void run() {
// TODO Auto-generated method stub
}
});
}
});
reset.setText("Reset");
JMenuItem wczytajAlgorytm1 = new JMenuItem();
wczytajAlgorytm1.setAction(new AbstractAction() {
public void actionPerformed(ActionEvent ev) {
status.setText("Wczytywanie...");
SwingUtilities.invokeLater(new Runnable() {
public void run() {
dialog[3].setVisible(true);
operator.setStartPoint(Integer.parseInt(wspPoczX
.getText()), Integer.parseInt(wspPoczY
.getText()));
operator.setStopPoint(Integer.parseInt(wspKoncX
.getText()), Integer.parseInt(wspKoncY
.getText()));
operator.setSteps(Integer.parseInt(wspKoncX.getText()));
operator.setAPenalty(Integer.parseInt(karaW.getText()));
operator.setDPenalty(Integer.parseInt(karaZ.getText()));
operator.setAlgorithmMode(1);
}
});
}
});
wczytajAlgorytm1.setText("Algorytm 1");
JMenuItem wczytajAlgorytm2 = new JMenuItem();
wczytajAlgorytm2.setAction(new AbstractAction() {
public void actionPerformed(ActionEvent ev) {
status.setText("Wczytywanie...");
SwingUtilities.invokeLater(new Runnable() {
public void run() {
dialog[3].setVisible(true);
operator.setStartPoint(Integer.parseInt(wspPoczX
.getText()), Integer.parseInt(wspPoczY
.getText()));
operator.setStopPoint(Integer.parseInt(wspKoncX
.getText()), Integer.parseInt(wspKoncY
.getText()));
operator.setSteps(Integer.parseInt(wspKoncX.getText()));
operator.setAPenalty(Integer.parseInt(karaW.getText()));
operator.setDPenalty(Integer.parseInt(karaZ.getText()));
operator.setAlgorithmMode(2);
}
});
}
});
wczytajAlgorytm2.setText("Algorytm 2");
plik.add(wczytajAlgorytm1);
plik.add(wczytajAlgorytm2);
plik.add(new JSeparator());
plik.add(reset);
plik.add(zakoncz);
menu.add(plik);
add(menu);
getContentPane().add(BorderLayout.NORTH, menu);
panelR = new JPanel();
panelL = new JPanel();
dialog = new JDialog[4];
add(panelL);
add(panelR);
getContentPane().add(BorderLayout.EAST, panelR);
panelR.setLayout(new FlowLayout(FlowLayout.LEADING));
panelR.setPreferredSize(new Dimension(250, getHeight()));
getContentPane().add(BorderLayout.CENTER, panelL);
panelL.setLayout(new FlowLayout(FlowLayout.LEADING));
panelL.setPreferredSize(new Dimension(getWidth(), getHeight()));
JButton[] but = new JButton[8];
but[0] = new JButton("Wczytaj teren 1");
but[0].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ev) {
status.setText("Wczytywanie...");
SwingUtilities.invokeLater(new Runnable() {
public void run() {
// repaint();
operator.setTerrainMode(1);
teren = operator.getTerrain();
repaint();
status.setText("Gotowe");
}
});
}
});
but[0].setPreferredSize(new Dimension(165, 25));
but[1] = new JButton("Wczytaj teren 2");
but[1].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ev) {
status.setText("Wczytywanie...");
SwingUtilities.invokeLater(new Runnable() {
public void run() {
// repaint();
operator.setTerrainMode(2);
teren = operator.getTerrain();
repaint();
status.setText("Gotowe");
}
});
}
});
but[1].setPreferredSize(new Dimension(165, 25));
but[2] = new JButton("Wczytaj teren 3");
but[2].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ev) {
status.setText("Wczytywanie...");
SwingUtilities.invokeLater(new Runnable() {
public void run() {
// repaint();
operator.setTerrainMode(3);
teren = operator.getTerrain();
repaint();
status.setText("Gotowe");
}
});
}
});
but[2].setPreferredSize(new Dimension(165, 25));
but[3] = new JButton("Wczytaj dowolny teren");
but[3].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ev) {
status.setText("Wczytywanie...");
SwingUtilities.invokeLater(new Runnable() {
public void run() {
repaint();
final String nazwa = (String) JOptionPane
.showInputDialog(null, null, "12.jpg");
operator.setPath(nazwa);
operator.setTerrainMode(0);
teren = operator.getTerrain();
repaint();
status.setText("Gotowe");
}
});
}
});
but[3].setPreferredSize(new Dimension(165, 25));
wspPoczX = new JTextField("190", 5);
wspPoczY = new JTextField("185", 5);
wspKoncX = new JTextField("45", 5);
wspKoncY = new JTextField("95", 5);
iloscEtapow = new JTextField("4", 5);
karaW = new JTextField("20", 5);
karaZ = new JTextField("10", 5);
but[4] = new JButton("Algorytm 1");
but[4].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// dialog[1].setVisible(true);
operator.setStartPoint(Integer.parseInt(wspPoczX.getText()),
Integer.parseInt(wspPoczY.getText()));
operator.setStopPoint(Integer.parseInt(wspKoncX.getText()),
Integer.parseInt(wspKoncY.getText()));
operator.setSteps(Integer.parseInt(iloscEtapow.getText()));
operator.setAPenalty(Integer.parseInt(karaW.getText()));
operator.setDPenalty(Integer.parseInt(karaZ.getText()));
operator.setAlgorithmMode(1);
repaint();
}
});
but[4].setPreferredSize(new Dimension(165, 25));
but[5] = new JButton("Algorytm 2");
but[5].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// dialog[2].setVisible(true);
operator.setStartPoint(Integer.parseInt(wspPoczX.getText()),
Integer.parseInt(wspPoczY.getText()));
operator.setStopPoint(Integer.parseInt(wspKoncX.getText()),
Integer.parseInt(wspKoncY.getText()));
operator.setSteps(Integer.parseInt(wspKoncX.getText()));
operator.setAPenalty(Integer.parseInt(karaW.getText()));
operator.setDPenalty(Integer.parseInt(karaZ.getText()));
operator.setAlgorithmMode(2);
repaint();
}
});
but[5].setPreferredSize(new Dimension(165, 25));
but[6] = new JButton("Rysuj �cie�k�");
but[6].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// operator.getPath();
paint2();
//paint3();
// dialog[3].setVisible(true);
/*
* gdc.drawLine(Integer.parseInt(wspPoczX.getText()),
* Integer.parseInt(wspPoczY.getText()),
* Integer.parseInt(wspKoncX.getText()),
* Integer.parseInt(wspKoncY.getText())); operator.repaint();
*/
}
});
but[6].setPreferredSize(new Dimension(165, 25));
but[7] = new JButton("Oblicz kar�");
but[7].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// dialog[2].setVisible(true);
operator.setAPenalty(Integer.parseInt(karaW.getText()));
operator.setDPenalty(Integer.parseInt(karaZ.getText()));
operator.setAlgorithmMode(2);
karaC = Integer.toString(operator.calculatePenalty(recznaS));
kara.setText(karaC);
}
});
panelR.add(but[0]);
panelR.add(but[1]);
panelR.add(but[2]);
panelR.add(but[3]);
panelR.add(new JLabel("Podaj wsp�rz�dn� pocz�tkow� X"));
panelR.add(wspPoczX);
panelR.add(new JLabel("Podaj wsp�rz�dn� pocz�tkow� Y"));
panelR.add(wspPoczY);
panelR.add(new JLabel("Podaj wsp�rz�dn� ko�cow� X"));
panelR.add(wspKoncX);
panelR.add(new JLabel("Podaj wsp�rz�dn� ko�cow� Y"));
panelR.add(wspKoncY);
panelR.add(new JLabel("Podaj ilo�� etap�w"));
panelR.add(iloscEtapow);
panelR.add(new JLabel("Podaj wsp�czynnik kary za wspinanie"));
panelR.add(karaW);
panelR.add(new JLabel("Podaj wsp�czynnik kary za schodzenie"));
panelR.add(karaZ);
panelR.add(but[4]);
panelR.add(but[5]);
panelR.add(but[6]);
panelR.add(but[7]);
panelR.add(new JLabel("Ca�kowita kara za �cie�k�: "));
kara = new JLabel(karaC);
panelR.add(kara);
}
//metoda sluzy do rysowania sciezki
public void paint2() {
operator.getPath();
if (operator.getPath() != null) {
Graphics2D gdc = (Graphics2D) panelL.getGraphics();
gdc.setColor(Color.yellow);
gdc.drawLine(Integer.parseInt(wspPoczX.getText())*2+100, Integer
.parseInt(wspPoczY.getText())*2+100, Integer.parseInt(wspKoncX
.getText())*2+100, Integer.parseInt(wspKoncY.getText())*2+100);
} else {
System.out.println("Nie ma sciezki do wczytania");
}
}
public void paint(Graphics g) {
int it=1;
if (teren != null) {
Graphics2D g2d = (Graphics2D) g;
g2d.drawImage(teren, null, 100, 100);
}
if (operator.getPath() != null) {
Point[] sciezka = operator.getPath();
g.setColor(Color.yellow);
for(int i=1;i<sciezka.length;i++)
g.drawLine(sciezka[i-1].x*2+100,sciezka[i-1].y*2+100,sciezka[i].x*2+100, sciezka[i].y*2+100);
karaC = Integer.toString(operator.getPenalty());
kara.setText(karaC);
}
g.setColor(Color.green);
while(it<recznaS.length&&recznaS[it-1]!=null&&recznaS[it]!=null) {
g.drawLine(recznaS[it-1].x*2+100, recznaS[it-1].y*2+100, recznaS[it].x*2+100, recznaS[it].y*2+100);
it++;
}
}
public void init() {
panelL.addMouseListener(this);
}
public void mouseClicked(MouseEvent e) {
Point click = e.getPoint();
rysujSciezke(click);
}
public void mouseEntered(MouseEvent arg0) {
}
public void mouseExited(MouseEvent arg0) {
}
public void mousePressed(MouseEvent e) {
Point click = e.getPoint();
rysujSciezke(click);
}
public void mouseReleased(MouseEvent arg0) {
}
public void mouseDragged(MouseEvent arg0) {
}
public void mouseMoved(MouseEvent arg0) {
}
private void rysujSciezke(Point x) {
Point[] temp = new Point[recznaS.length+1];
for(int i=0;i<recznaS.length;i++) if(recznaS[i]!=null) temp[i]=recznaS[i];
recznaS = new Point[temp.length];
for(int i=0;i<recznaS.length-1;i++) recznaS[i]=temp[i];
recznaS[recznaS.length-1] = new Point((x.x-100)/2,(x.y-100)/2);
repaint();
}
}