All pastes #454737 Raw Edit

Mine

public text v1 · immutable
·published 2007-04-23 20:00 UTC
rendered paste body
************************************************************************************************
import javax.swing.*;
import java.util.*;
import java.awt.event.*;
import java.awt.*;
import javax.imageio.*;
import java.io.*;
import javax.swing.*;
import java.util.ArrayList;

public class windowOps {
//	class variables
	static Dimension screenSize;
	
	private static void getScreenSize() {
		Toolkit toolkit = Toolkit.getDefaultToolkit();
		screenSize = toolkit.getScreenSize();
	}
	
	private static int getFirstHeight() {
		getScreenSize();
    	double height = screenSize.getHeight() * 0.75;
    	Double realHeight = new Double(height);
    	return realHeight.intValue();
	}
	
	private static int getFirstWidth() {
		getScreenSize();
    	double width = screenSize.getWidth() * 0.75;
    	Double realWidth = new Double(width);
    	return realWidth.intValue();
	}
	
    public void drawFirstWindow() {
    	Data myData = new Data();
    	infoPanel info = new infoPanel(myData);
    	mapPanel map = new mapPanel(myData, info);
    	myData.openMap();
    	JScrollPane mapPane = new JScrollPane(map);
    	pilotMenu menu = new pilotMenu(myData);
    	FirstWindow first = new FirstWindow();
        first.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        first.setSize(getFirstWidth(), getFirstHeight());
        first.setLocationRelativeTo(null);
        first.setTitle("Pilot Plot");
        first.setLayout(new BorderLayout());
        first.add(mapPane, BorderLayout.CENTER);    
        first.add(menu, BorderLayout.NORTH);
        first.add(info, BorderLayout.SOUTH);
        first.setVisible(true);
    }
}

class mapPanel extends JPanel {
	
	Image map;
	String filename;
	public int tempX, tempY, tempX2, tempY2;
	MyPoint point0, point1;
	
	Data myData;
	infoPanel updater;
	
	mapPanel(final Data myData, final infoPanel updater) {
		this.updater = updater;
		this.myData = myData;
		addMouseListener(new MouseAdapter() {
			public void mouseReleased(MouseEvent e) {
				tempX = e.getX();
				tempY = e.getY();
				myData.list.add(new MyPoint(tempX, tempY, myData.color));
				myData.clickCount++;
				repaint();
				updater.updateDisplay();
			}
		});
	}

	
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
        setPreferredSize(new Dimension(myData.width, myData.height));
		g.drawImage(myData.map, 0, 0, myData.width, myData.height, null); // draw the map
		if (myData.clickCount > 1) {
			
			for (int i=1;i<myData.clickCount;i++) {
			point0 = (MyPoint) myData.list.get(i-1);
			point1 = (MyPoint) myData.list.get(i);
			switch (point1.color) {
			case 0: g.setColor(Color.red); break;
			case 1: g.setColor(Color.blue); break;
			case 2: g.setColor(Color.green); break;
			default: g.setColor(Color.red); break;
			}
			g.drawLine(point0.getX(), point0.getY(), point1.getX(), point1.getY());
			
			System.out.print(myData.color);
			}
		}
	}
	
	
	
}

class pilotMenu extends JMenuBar {
	Data myData;
	
	pilotMenu(final Data myData) {
		this.myData = myData;
		
		//the File Menu
		JMenu file = new JMenu("File");
		JMenuItem openMap = new JMenuItem("Open Map");
		file.add(openMap);
		openMap.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				myData.openMap();
			}
		});
		
		JMenuItem open = new JMenuItem("Open Route");
		file.add(open);
		open.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				openFile();
				
			}
		});
		JMenuItem save = new JMenuItem("Save");
		file.add(save);
		save.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				saveFile();
			}
		});
		JMenuItem exitItem = new JMenuItem("Exit");
		exitItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		file.add(exitItem);
		add(file);

		//the plotting Menu
		JMenu plotting = new JMenu("Plotting");
		ButtonGroup group = new ButtonGroup();
		JMenu color = new JMenu("Color Choices");
		JRadioButtonMenuItem red = new JRadioButtonMenuItem("Red");
		red.setSelected(true);
		red.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			myData.color=0;
			}
		});
		group.add(red);
		JRadioButtonMenuItem blue = new JRadioButtonMenuItem("Blue");
		blue.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				myData.color=1;
				}
			});
		group.add(blue);
		JRadioButtonMenuItem green = new JRadioButtonMenuItem("Green");
		green.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				myData.color=2;
				}
			});
		group.add(green);
		color.add(red); color.add(blue); color.add(green);
		plotting.add(color);
		add(plotting);

		//the Help Menu
		JMenu Help = new JMenu("Help");
		add(Help);
		JMenuItem brief = new JMenuItem("A Brief Help");
		brief.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				help();
			}
		});
		Help.add(brief);
	}
	
	public void saveFile() {
		JFileChooser fc = new JFileChooser();
		fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
		fc.showSaveDialog(null);
		File outFile = fc.getSelectedFile();
		try { 
			PrintWriter saver = new PrintWriter(outFile);
			for (int n=0;n<myData.list.size();n++) {
				MyPoint tempPoint;
				tempPoint = (MyPoint) myData.list.get(n);
				int tempX = tempPoint.getX();
				int tempY = tempPoint.getY();
				int tempC = tempPoint.getColor();
				saver.print(tempX + " " + tempY + " " + tempC + " " + myData.clickCount + " ");
			}
			saver.print(myData.filename);
			saver.close();
		} catch (FileNotFoundException e) {
	           JOptionPane.showMessageDialog(null, "ERROR:\n   File could not be saved.");
	           return; 
		}
		
	}
	
	public void openFile() {
		int i=0;
		JFileChooser fc = new JFileChooser();
		fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
		fc.showOpenDialog(null);
		File inFile = fc.getSelectedFile();
		myData.routename = inFile.getPath();
		try { 
			Scanner opener = new Scanner(inFile);
			myData.list.clear();
			myData.clickCount=0;
			MyPoint tempPoint;
			repaint();
			
			while (opener.hasNextInt()) {
				int tempX = opener.nextInt();
				int tempY = opener.nextInt();
				int tempC = opener.nextInt();
				myData.clickCount = opener.nextInt();
				tempPoint = new MyPoint(tempX, tempY, tempC);
				myData.list.add(tempPoint);
			}
			
			while (opener.hasNext()) {
				if (i == 0)
					myData.newfilename = (opener.next());
				else
					myData.newfilename = (myData.newfilename + " " + opener.next());
				i++;
			}
			
			opener.close();
			if (!myData.newfilename.equals(myData.filename)) {
				JOptionPane.showMessageDialog(null, "Wrong map save file.");
				myData.list.clear();
				myData.clickCount=0;
			}
			System.out.print("\n"+myData.filename+"\n");
			System.out.print(myData.newfilename);
			
		} catch (FileNotFoundException e) {
	           JOptionPane.showMessageDialog(null, "ERROR:\n   File could not be opened.");
	           return; 
		}
	}
	
	public void help() {
		JOptionPane.showMessageDialog(null, "<html>Thank you for using Pilot Plot v0.1!<br>" +
				"To use this program, Open a Map file, draw your route<br>" +
				"and either use your information or save it until later.<br></html>");
	}
}

class infoPanel extends JPanel{
	Data myData;
	JLabel colorLabel, routeSegs, currentMap, currentRoute;
	String color;
	
	infoPanel(final Data myData) {
		this.myData = myData;
		
		setLayout(new BorderLayout()); // set layout manager
		// working map panel
		JPanel workingMap = new JPanel();
		workingMap.setLayout(new GridLayout(3,0));
		currentMap = new JLabel("<html><body><center>The current working map is:  " + myData.filename, JLabel.CENTER);
		workingMap.add(currentMap);
		currentRoute = new JLabel("The current working route is:  " + myData.routename, JLabel.CENTER);
		workingMap.add(currentRoute);
		add(workingMap, BorderLayout.NORTH);
		
		// everything but the top panel
		JPanel everythingElse = new JPanel();
		//the clear button
		JButton clear = new JButton("<html>Clear Route. <br>Press and then click the map. </html>");
		clear.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				myData.list.clear();
				myData.clickCount=0;
			}
		});
		
		everythingElse.setLayout(new GridLayout(0,3,10,10));
		routeSegs = new JLabel("<html>The current number of route segments is:<br>" +
				"Not implemented yet :P<br>" + "For a grand total of: <br>" + "not yet :P" +
				"nautical miles.</html>", JLabel.CENTER);
		everythingElse.add(routeSegs);
		everythingElse.add(new JLabel("<html>If you were to click RIGHT THE FUCK NOW<br> you would have" +
				" a route segment of <br>" + "NOT YET!! <br>" + "nautical miles</html>", JLabel.CENTER));
		//subpanel for the right
		JPanel right = new JPanel();
		right.setLayout(new GridLayout(3,0));
		right.add(new JSlider());
		right.add(clear);
		switch (myData.color) {
		case 0: color = "red"; break;
		case 1: color = "blue"; break;
		case 2: color = "green"; break;
		default: color = "red"; break;
		}
		colorLabel = new JLabel("Current color is " + color);
		right.add(colorLabel);
		everythingElse.add(right);
		add(everythingElse, BorderLayout.SOUTH); 
	}
	
	public void updateDisplay() {
		switch (myData.color) {
		case 0: color = "red"; break;
		case 1: color = "blue"; break;
		case 2: color = "green"; break;
		default: color = "red"; break;
		}
		colorLabel.setText("Current color is " + color);
		routeSegs.setText("<html>The current number of route segments is:<br>" +
				(myData.list.size() - 1) + "<br>" + "For a grand total of: <br>" + "not yet :P" +
				"nautical miles.</html>");
		currentMap.setText("<html><body><center>The current working map is:  " + myData.filename);
		currentRoute.setText("The current working route is:  " + myData.routename);
	}
}


class MyPoint {
	int x;
	int y;
	int color;
	MyPoint (int inx, int iny, int incolor) {
		x=inx;
		y=iny;
		color=incolor;
	}
	public int getX() { return x; }
	public int getY() { return y; }
	public int getColor() { return color; }
}

*************************************************************************** Data.java
import java.util.ArrayList;
import javax.imageio.*;
import javax.swing.*;
import java.awt.*;
import java.io.*;


public class Data {
	int clickCount = 0;
	ArrayList list = new ArrayList(0);
	int color = 0; //0=red,1=blue,2=green
	String filename="", newfilename="",routename="New Route";
	Image map;
	int width, height, fullwidth, fullheight, wwidth, wheight; //pic variables
	
	public void openMap() {
		JOptionPane.showMessageDialog(null, "Please load a map file.");
		JFileChooser fc = new JFileChooser();
		fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
		fc.showOpenDialog(null);
		File inFile = fc.getSelectedFile();
		filename = inFile.getAbsolutePath();
        try {
        	map = ImageIO.read (inFile);
        } catch (IOException e) {
           JOptionPane.showMessageDialog(null, "ERROR:\n   Could not load map image");
           map = null;
           return;
        }
        fullwidth = width = map.getWidth(null);
        fullheight = height = map.getHeight(null);
        clickCount = 0;
        list.clear();
        routename="New Route";
		} 
}