private static int[] makeGradientPallet() { BufferedImage image = new BufferedImage(100, 1, BufferedImage.TYPE_INT_RGB); Graphics2D g2 = image.createGraphics(); Point2D start = new Point2D.Float(0f, 0f); Point2D end = new Point2D.Float(99f, 0f); float[] dist = {0f, .5f, 1f}; Color[] colors = {Color.RED, Color.YELLOW, Color.GREEN}; g2.setPaint(new LinearGradientPaint(start, end, dist, colors)); g2.fillRect(0, 0, 100, 1); g2.dispose(); int width = image.getWidth(null); int[] pallet = new int[width]; PixelGrabber pg = new PixelGrabber(image, 0, 0, width, 1, pallet, 0, width); try { pg.grabPixels(); } catch (InterruptedException ex) { ex.printStackTrace(); } return pallet; }
private void _displayImgInFrame() { final JFrame frame = new JFrame("Google Static Map"); GUIUtils.setAppIcon(frame, "71.png"); // frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE); JLabel imgLbl = new JLabel(new ImageIcon(_img)); imgLbl.setToolTipText( MessageFormat.format( "<html>Image downloaded from URI<br>size: w={0}, h={1}</html>", _img.getWidth(), _img.getHeight())); GUIUtils.centerOnScreen(frame); frame.setVisible(true); frame.setContentPane(imgLbl); frame.pack(); frame.setResizable(false); imgLbl.addMouseListener( new MouseListener() { public void mouseClicked(MouseEvent e) {} public void mousePressed(MouseEvent e) { System.out.println("Mouse Listener: Mouse Clicked!"); mapIsUp = 1; sentX = 0.00; clickX = e.getX(); // Latitude clickY = e.getY(); // Longitude if ((clickX < (_img.getWidth() / 2)) && (clickY < (_img.getHeight() / 2))) { // 1st quadrant positive values sentX = Double.parseDouble(ttfLati.getText()) + (((-1 * (_img.getWidth() / 2)) + clickX) * pixelX); // Add to latitude sentY = Double.parseDouble(ttfLongi.getText()) + (((_img.getHeight() / 2) - clickY) * pixelY); // Add to Longitude System.out.println("Top left"); } else if ((clickX > (_img.getWidth() / 2)) && (clickY > (_img.getHeight() / 2))) { // 2nd quadrant negative values sentX = Double.parseDouble(ttfLati.getText()) + (((-1 * (_img.getHeight() / 2)) + clickX) * pixelX); sentY = Double.parseDouble(ttfLongi.getText()) + (((_img.getHeight() / 2) - clickY) * pixelY); System.out.println("Bottom Right"); } else if ((clickX < (_img.getWidth() / 2)) && (clickY > (_img.getHeight() / 2))) { // 3rd quadrant 1 positive 1 negative sentX = Double.parseDouble(ttfLati.getText()) + (((-1 * (_img.getWidth() / 2)) + clickX) * pixelX); sentY = Double.parseDouble(ttfLongi.getText()) + (((_img.getHeight() / 2) - clickY) * pixelY); System.out.println("Bottom Left"); } else { // 3rd quadrant 1 positive 1 negative sentX = Double.parseDouble(ttfLati.getText()) + (((-1 * (_img.getHeight() / 2)) + clickX) * pixelX); sentY = Double.parseDouble(ttfLongi.getText()) + (((_img.getHeight() / 2) - clickY) * pixelY); System.out.println("Top Right"); } BigDecimal toCoordsX = new BigDecimal(sentX); BigDecimal toCoordsY = new BigDecimal(sentY); sentX = (toCoordsX.setScale(6, BigDecimal.ROUND_HALF_UP)) .doubleValue(); // allows values of up to 6 decimal places sentY = (toCoordsY.setScale(6, BigDecimal.ROUND_HALF_UP)).doubleValue(); getCoords = sentX + " " + sentY; ttfLati.setText(Double.toString(sentX)); ttfLongi.setText(Double.toString(sentY)); System.out.println("... saving Coordinates"); saveLocation( getCoords); // pass getCoords through saveLocation. this string is appended to the // savedLocations file. System.out.println("... savedCoordinates"); // Update the Locations ComboBox with new additions ttfSave.removeAllItems(); // re-populate the ComboBox System.out.println("removed items"); getSavedLocations(); // run through file to get all locations for (int i = 0; i < loc.size(); i++) ttfSave.addItem(loc.get(i)); System.out.println("update combobox"); mapIsUp = 0; frame.dispose(); // closes window startTaskAction(); // pops up a new window } public void saveLocation(String xy) { BufferedWriter f = null; // created a bufferedWriter object try { f = new BufferedWriter( new FileWriter( "savedLocations.txt", true)); // evaluated true if file has not been created yet f.write(xy); // append passed coordinates and append to file if exists f.newLine(); f.flush(); } catch (IOException ioe) { ioe.printStackTrace(); } finally { // close the file if (f != null) { try { f.close(); } catch (IOException e) { // any error, catch exception System.err.println("Error: " + e.getMessage()); } } } } public void mouseReleased(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} }); }