// Digitize.java // get cooddinates from .gif .jpg .png images // rename and edit output file to suit your needs import java.awt.*; import java.awt.event.*; import java.text.*; import java.io.*; import javax.swing.*; public class Digitize extends JFrame { MyJPanel panel; private JDesktopPane theDesktop; ImageIcon imageActive = new ImageIcon(" "); // selected Dimension dimActive; int Pwidth = 800; int Pheight = 800; int point = 0; // x0,y0 x1,y0 x0,y1 int x00=0, y00=0, x10=0, y10=0, x01=0, y01=0; String action = new String("click start"); String file_name; public Digitize(String temp_name) { file_name = temp_name; setTitle("Digitize demonstration"); setSize(Pwidth+10,Pheight+10); setBackground(Color.white); setForeground(Color.black); // create menu bar and attach it to AwtMenuTest window JMenuBar bar = new JMenuBar(); setJMenuBar(bar); // set up Image menu JMenu imageMenu = new JMenu("Start"); bar.add(imageMenu); // add drop downs to menu bar // set up image types JMenuItem gifItem = new JMenuItem("sample p1.gif"); imageMenu.add(gifItem); JMenuItem jpgItem = new JMenuItem("sample curve.jpg"); imageMenu.add(jpgItem); JMenuItem jpg2Item = new JMenuItem("sample c6thrust.jpg"); imageMenu.add(jpg2Item); JMenuItem pngItem = new JMenuItem("sample chess2.png"); imageMenu.add(pngItem); JMenuItem fileItem = new JMenuItem("file"); imageMenu.add(fileItem); // set up display area theDesktop = new JDesktopPane(); getContentPane().add(theDesktop); // Create internal frame JInternalFrame frame = new JInternalFrame( "Digitize area", true, true, true, true); // attach panel to internal frame Container container = frame.getContentPane(); panel = new MyJPanel(); container.add(panel, BorderLayout.CENTER); // set size of internal frame to size of its contents frame.pack(); // uses class Dimension theDesktop.add(frame); panel.addMouseListener (new mousePressHandler()); frame.setVisible(true); gifItem.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) { System.out.println("Image p1.gif"); // load image imageActive = new ImageIcon( "p1.gif" ); dimActive = new Dimension( imageActive.getIconWidth(), imageActive.getIconHeight() ); System.out.println("imageGif dimension = "+dimActive); // debug print System.out.println("click on x=0, y=0 point"); action = new String("click on 0,0 point"); panel.repaint(); }}); jpgItem.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) { System.out.println("Image curve.jpg"); // load image imageActive = new ImageIcon( "curve.jpg" ); dimActive = new Dimension( imageActive.getIconWidth(), imageActive.getIconHeight() ); System.out.println("imageJpg dimension = "+dimActive); // debug print System.out.println("click on x=0, y=0 point"); action = new String("click on 0,0 point"); panel.repaint(); }}); jpg2Item.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) { System.out.println("Image c6thrust.jpg"); // load image imageActive = new ImageIcon( "c6thrust.jpg" ); dimActive = new Dimension( imageActive.getIconWidth(), imageActive.getIconHeight() ); System.out.println("imageJpg dimension = "+dimActive); // debug print System.out.println("click on x=0, y=0 point"); action = new String("click on 0,0 point"); panel.repaint(); }}); pngItem.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) { System.out.println("Image chess2.png"); // load image imageActive = new ImageIcon( "chess2.png" ); dimActive = new Dimension( imageActive.getIconWidth(), imageActive.getIconHeight() ); System.out.println("imagePng dimension = "+dimActive); // debug print System.out.println("click on x=0, y=0 point"); action = new String("click on 0,0 point"); panel.repaint(); }}); fileItem.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) { System.out.println("Image "+file_name); // load image from file imageActive = new ImageIcon( file_name ); dimActive = new Dimension( imageActive.getIconWidth(), imageActive.getIconHeight() ); System.out.println("imagefile dimension = "+dimActive); // debug print System.out.println("click on x=0, y=0 point"); action = new String("click on 0,0 point"); panel.repaint(); }}); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); setVisible(true); } // end constructor Digitize class mousePressHandler extends MouseAdapter { public void mousePressed (MouseEvent e) { int xp, yp, b; int width, height; double x, y; width = getSize().width; height = getSize().height; if(point==0) System.out.println("width="+width+", height="+height); xp = e.getX(); yp = e.getY(); b = e.getButton(); if(b==1) // left button { requestFocus(); if(point==0) { System.out.println("0,0 at x="+xp+", y="+yp); x00 = xp; y00 = yp; point++; System.out.println("click on x=1, y=0 point"); action = new String("click on 1,0 point"); } else if(point==1) { System.out.println("1,0 at x="+xp+", y="+yp); x10 = xp; y10 = yp; point++; System.out.println("click on x=0, y=1 point"); action = new String("click on 0,1 point"); } else if(point==2) { System.out.println("0,1 at x="+xp+", y="+yp); x01 = xp; y01 = yp; point++; System.out.println("digitize desired points"); action = new String("digitize desired points"); } else { System.out.println("point at x="+xp+", y="+yp); x = (double)(xp-x00)/(double)(x10-x00); y = (double)(yp-y00)/(double)(y01-y00); System.out.println(x+" "+y); } } panel.repaint(); } } // end mousePressHandler class MyJPanel extends JPanel { ImageIcon imageLocal; public void paintComponent(Graphics g) { // g.clearRect(0, 0, Pwidth, Pheight-100); // cover non-erase bug g.setColor(Color.red); super.paintComponent( g ); // display image imageLocal = imageActive; imageLocal.paintIcon( this, g, 0, 0 ); g.drawString(action, 10, Pheight-100); } // end paintComponent // return dimensions for sizing public Dimension getPreferredSize() { return new Dimension(Pwidth-15, Pheight-90); } } // end MyJPanel public static void main (String[] args) // optional file { String temp_name = new String("none specified"); if(args.length>0) temp_name = args[0]; new Digitize(temp_name); } // end main } // end class Digitize