/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* EditorFrame.java
*
* Created on 19.05.2009, 08:00:37
*/
package MapEditor;
import java.awt.Color;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.Map;
import java.util.TreeMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JFileChooser;
import javax.swing.JPanel;
import javax.swing.filechooser.FileFilter;
/**
*
* @author AA
*/
public class EditorFrame extends javax.swing.JFrame {
final Map<Integer, Field> map = new TreeMap<Integer, Field>();
/** Creates new form EditorFrame */
public EditorFrame() {
initComponents();
int color = 1;
int index = 0;
for (int i = 0; i < 80; i++) {
for (int j = 0; j < 55; j++) {
map.put(index, new Field(color, new Point(i, j)));
index++;
}
}
jPanel1.setBounds(0, 0, 800, 550);
for (int i = 0; i < map.size(); i++) {
final int j = i;
Color colour = (map.get(i).getColor() == 0) ? Color.BLACK : Color.WHITE;
JPanel tmp = new JPanel();
map.get(i).setPanel(tmp);
tmp.addMouseListener(new MouseListener() {
int index = j;
public void mouseClicked(MouseEvent e) {
int color = map.get(j).getColor();
color = (color == 0) ? 1 : 0;
map.get(j).setColor(color);
Color colour = (color == 0) ? Color.BLACK : Color.WHITE;
e.getComponent().setBackground(colour);
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
if(e.isAltDown()) {
int color = 0;
map.get(j).setColor(color);
Color colour = (color == 0) ? Color.BLACK : Color.WHITE;
e.getComponent().setBackground(colour);
}
if(e.isControlDown()) {
int color = 1;
map.get(j).setColor(color);
Color colour = (color == 0) ? Color.BLACK : Color.WHITE;
e.getComponent().setBackground(colour);
}
}
public void mouseExited(MouseEvent e) {
}
});
tmp.addMouseMotionListener(new MouseMotionListener() {
public void mouseDragged(MouseEvent e) {
}
public void mouseMoved(MouseEvent e) {
}
});
tmp.setBounds(map.get(i).getX() * 10, map.get(i).getY() * 10, 10, 10);
tmp.setBackground(colour);
jPanel1.add(tmp);
}
pack();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jPanel2 = new javax.swing.JPanel();
jButton1 = new javax.swing.JButton();
jRadioButton1 = new javax.swing.JRadioButton();
jRadioButton2 = new javax.swing.JRadioButton();
jRadioButton3 = new javax.swing.JRadioButton();
jRadioButton4 = new javax.swing.JRadioButton();
jRadioButton5 = new javax.swing.JRadioButton();
jButton4 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setMinimumSize(new java.awt.Dimension(800, 550));
jPanel1.setPreferredSize(new java.awt.Dimension(800, 550));
jPanel1.setLayout(null);
getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);
jPanel2.setLayout(new javax.swing.BoxLayout(jPanel2, javax.swing.BoxLayout.LINE_AXIS));
jButton1.setText("OK");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jPanel2.add(jButton1);
jRadioButton1.setText("Red ");
jRadioButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jRadioButton1ActionPerformed(evt);
}
});
jPanel2.add(jRadioButton1);
jRadioButton2.setText("Brown ");
jPanel2.add(jRadioButton2);
jRadioButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jRadioButton3ActionPerformed(evt);
}
});
jPanel2.add(jRadioButton3);
jRadioButton4.setText("Blue");
jPanel2.add(jRadioButton4);
jRadioButton5.setText("jRadioButton5");
jPanel2.add(jRadioButton5);
jButton4.setText("Upload");
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton4ActionPerformed(evt);
}
});
jPanel2.add(jButton4);
jButton2.setText("Save");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jPanel2.add(jButton2);
jButton3.setText("Cancel");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
jPanel2.add(jButton3);
getContentPane().add(jPanel2, java.awt.BorderLayout.SOUTH);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileFilter(new FileFilter() {
public boolean accept(File pathname) {
return pathname.getName().endsWith(".txt");
}
@Override
public String getDescription() {
return "txt Files";
}
});
if (fileChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
PrintStream stream = null;
try {
File file = fileChooser.getSelectedFile();
stream = new PrintStream(new FileOutputStream(file));
for (Integer i : map.keySet()) {
Field f = map.get(i);
stream.print(i);
stream.print(" ");
stream.print(f.getX());
stream.print(" ");
stream.print(f.getY());
stream.print(" ");
stream.println(f.getColor());
}
} catch (FileNotFoundException ex) {
Logger.getLogger(EditorFrame.class.getName()).log(Level.SEVERE, null, ex);
} finally {
stream.close();
}
}
}
private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
private void jRadioButton3ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JRadioButton jRadioButton1;
private javax.swing.JRadioButton jRadioButton2;
private javax.swing.JRadioButton jRadioButton3;
private javax.swing.JRadioButton jRadioButton4;
private javax.swing.JRadioButton jRadioButton5;
// End of variables declaration
}