private JLabel makeLabel(final ImageIcon image, String str) { JLabel label = new JLabel(str, image, SwingConstants.LEFT); label.addMouseListener( new MouseAdapter() { private boolean flag; @Override public void mouseClicked(MouseEvent e) { JLabel l = (JLabel) e.getComponent(); l.setIcon(flag ? image : orgImage); flag ^= true; } }); return label; }
// init private static void init() { if (frame != null) frame.setVisible(false); frame = new JFrame(); offscreenImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); onscreenImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); offscreen = offscreenImage.createGraphics(); onscreen = onscreenImage.createGraphics(); setXscale(); setYscale(); offscreen.setColor(DEFAULT_CLEAR_COLOR); offscreen.fillRect(0, 0, width, height); setPenColor(); setPenRadius(); setFont(); clear(); // add anti-aliasing RenderingHints hints = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); hints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); offscreen.addRenderingHints(hints); // frame stuff ImageIcon icon = new ImageIcon(onscreenImage); JLabel draw = new JLabel(icon); draw.addMouseListener(std); draw.addMouseMotionListener(std); frame.setContentPane(draw); frame.addKeyListener(std); // JLabel cannot get keyboard focus frame.setResizable(false); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // closes all windows // frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); // closes only current window frame.setTitle("Standard Draw"); frame.setJMenuBar(createMenuBar()); frame.pack(); frame.requestFocusInWindow(); frame.setVisible(true); }
private JLabel makeLabelIcon() { ImageIcon i = new ImageIcon(getClass().getResource("duke.gif")); Dimension d = new Dimension(i.getIconWidth(), i.getIconHeight()); final BufferedImage image = new BufferedImage(d.width, d.height, BufferedImage.TYPE_INT_ARGB); Graphics g = image.createGraphics(); i.paintIcon(null, g, 0, 0); g.dispose(); final JLabel icon = new JLabel(i) { @Override public boolean contains(int x, int y) { return super.contains(x, y) && ((image.getRGB(x, y) >> 24) & 0xff) != 0; } }; icon.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR)); MouseAdapter l = new MouseAdapter() { private final transient Point start = new Point(); private Point loc; @Override public void mousePressed(MouseEvent me) { start.setLocation(me.getPoint()); } @Override public void mouseDragged(MouseEvent me) { loc = icon.getLocation(loc); int x = loc.x - start.x + me.getX(); int y = loc.y - start.y + me.getY(); icon.setLocation(x, y); } }; icon.addMouseListener(l); icon.addMouseMotionListener(l); icon.setBounds(new Rectangle(22, 22, d.width, d.height)); return icon; }
/** *********** Initialization of every component and variable being used ************* */ IPToolKit() { cp = new Container(); cp.setLayout(new BorderLayout()); Main_pnl = new JPanel(); jmb = new JMenuBar(); m_File = new JMenu("File"); m_Arith = new JMenu("Arithmetic"); m_Filter = new JMenu("Filters"); m_Open = new JMenuItem("Open"); m_Save = new JMenuItem("Save"); m_Exit = new JMenuItem("Exit"); m_Add = new JMenu("Add"); m_Sub = new JMenu("Subtract"); m_Mul = new JMenu("Multiply"); m_Div = new JMenu("Divide"); edge = new JMenu("Edge Detection"); mpre = new JMenu("Prewits"); msob = new JMenu("Sobel"); lbl_img = new JLabel(""); lbl_gray = new JLabel(""); lbl_res = new JLabel(""); jfc = new JFileChooser(); panel_image = new JPanel(); panel_gray = new JPanel(); panel_result = new JPanel(); scrollPane = new JScrollPane(); mAddConst = new JMenu("Constant"); mSubConst = new JMenu("Constant"); mMulConst = new JMenu("Constant"); mDivConst = new JMenu("Constant"); mAddSat = new JMenuItem("Saturation"); mAddWrap = new JMenuItem("Wrap Around"); mSubSat = new JMenuItem("Saturation"); mSubWrap = new JMenuItem("Wrap Around"); mMulSat = new JMenuItem("Saturation"); mMulWrap = new JMenuItem("Wrap Around"); mDivSat = new JMenuItem("Saturation"); mDivWrap = new JMenuItem("Wrap Around"); mAddImg = new JMenuItem("Image"); mSubImg = new JMenuItem("Image"); mMulImg = new JMenuItem("Image"); mDivImg = new JMenuItem("Image"); mlowPass = new JMenuItem("Low Pass"); mhighPass = new JMenuItem("High Pass"); mhighBoost = new JMenuItem("High Boost"); median = new JMenuItem("Median"); mBright = new JMenuItem("Brightness"); mContrast = new JMenuItem("Contrast Stretch"); mThreshold = new JMenuItem(" Threshold "); m_Enhance = new JMenu("Enhancement"); mInvert = new JMenuItem("Negative"); mpre_hor = new JMenuItem("Prewits Horizontal"); mpre_ver = new JMenuItem("Prewits Vertical"); mpre_both = new JMenuItem("Prewits Both"); msob_hor = new JMenuItem("Sobel Horizontal"); msob_ver = new JMenuItem("Sobel Vertical"); msob_both = new JMenuItem("Sobel Both"); ; mrob = new JMenuItem("Roberts"); mean = new JMenuItem("Mean"); mlap = new JMenuItem("Laplacian"); other = new JMenu("Other"); hist = new JMenuItem("Histogram Equalization"); conect = new JMenuItem("Connected Component"); mBlend = new JMenuItem("Blending"); vsb = new JScrollBar(JScrollBar.VERTICAL); hsb = new JScrollBar(JScrollBar.HORIZONTAL); // jsp = new JScrollPane(); setJMenuBar(jmb); add(cp); // setBackground(new Color(255,255,255)); panel_image.setBackground(new Color(255, 0, 0)); panel_gray.setBackground(new Color(0, 255, 0)); panel_result.setBackground(new Color(0, 0, 255)); jmb.add(m_File); jmb.add(m_Arith); jmb.add(m_Enhance); jmb.add(m_Filter); jmb.add(edge); jmb.add(other); cp.add(Main_pnl); Main_pnl.setLayout(new GridLayout(1, 3, 10, 10)); Main_pnl.add(panel_image); Main_pnl.add(panel_gray); Main_pnl.add(panel_result); m_File.add(m_Open); m_File.add(m_Save); m_File.add(m_Exit); m_Arith.add(m_Add); m_Add.add(mAddImg); m_Add.add(mAddConst); mAddConst.add(mAddSat); mAddConst.add(mAddWrap); m_Arith.add(m_Sub); m_Sub.add(mSubImg); m_Sub.add(mSubConst); mSubConst.add(mSubSat); mSubConst.add(mSubWrap); m_Arith.add(m_Mul); m_Mul.add(mMulImg); m_Mul.add(mMulConst); mMulConst.add(mMulSat); mMulConst.add(mMulWrap); m_Arith.add(m_Div); m_Div.add(mDivImg); m_Div.add(mDivConst); mDivConst.add(mDivSat); mDivConst.add(mDivWrap); m_Filter.add(mlowPass); m_Filter.add(mhighPass); m_Filter.add(mhighBoost); m_Filter.add(median); m_Filter.add(mean); edge.add(mpre); edge.add(msob); mpre.add(mpre_hor); mpre.add(mpre_ver); mpre.add(mpre_both); msob.add(msob_hor); msob.add(msob_ver); msob.add(msob_both); edge.add(mrob); edge.add(mlap); m_Enhance.add(mBright); m_Enhance.add(mContrast); m_Enhance.add(mThreshold); m_Enhance.add(mInvert); m_Enhance.add(mBlend); other.add(hist); other.add(conect); m_Open.addActionListener(this); m_Save.addActionListener(this); m_Exit.addActionListener(this); mAddImg.addActionListener(this); mSubImg.addActionListener(this); mMulImg.addActionListener(this); mDivImg.addActionListener(this); mAddSat.addActionListener(this); mAddWrap.addActionListener(this); mSubSat.addActionListener(this); mSubWrap.addActionListener(this); mMulSat.addActionListener(this); mMulWrap.addActionListener(this); mDivSat.addActionListener(this); mDivWrap.addActionListener(this); mlowPass.addActionListener(this); mhighPass.addActionListener(this); mhighBoost.addActionListener(this); median.addActionListener(this); mBright.addActionListener(this); mContrast.addActionListener(this); mThreshold.addActionListener(this); mInvert.addActionListener(this); mBlend.addActionListener(this); mpre_hor.addActionListener(this); mpre_ver.addActionListener(this); mpre_both.addActionListener(this); msob_hor.addActionListener(this); msob_ver.addActionListener(this); msob_both.addActionListener(this); mrob.addActionListener(this); mean.addActionListener(this); mlap.addActionListener(this); hist.addActionListener(this); conect.addActionListener(this); lbl_gray.addMouseListener(this); panel_image.add(lbl_img); panel_gray.add(lbl_gray); panel_result.add(lbl_res); cp.add(vsb, BorderLayout.EAST); cp.add(hsb, BorderLayout.SOUTH); // cp1 = getContentPane(); cp = getContentPane(); setSize(600, 400); setVisible(true); }
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) {} }); }