Search in sources :

Example 6 with ActionEvent

use of java.awt.event.ActionEvent in project jna by java-native-access.

the class AlphaMaskDemo2 method run.

public void run() {
    // Must find a graphics configuration with a depth of 32 bits
    GraphicsConfiguration gconfig = WindowUtils.getAlphaCompatibleGraphicsConfiguration();
    frame = new JFrame("Alpha Mask Demo");
    alphaWindow = new JWindow(frame, gconfig);
    icon = new JLabel();
    icon.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    alphaWindow.getContentPane().add(icon);
    JButton quit = new JButton("Quit");
    JLabel label = new JLabel("Drag this window by its image");
    label.setHorizontalAlignment(SwingConstants.CENTER);
    alphaWindow.getContentPane().add(label, BorderLayout.NORTH);
    alphaWindow.getContentPane().add(quit, BorderLayout.SOUTH);
    quit.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }
    });
    MouseInputAdapter handler = new MouseInputAdapter() {

        private Point offset;

        public void mousePressed(MouseEvent e) {
            if (SwingUtilities.isLeftMouseButton(e))
                offset = e.getPoint();
        }

        public void mouseReleased(MouseEvent e) {
            offset = null;
        }

        public void mouseDragged(MouseEvent e) {
            if (offset != null) {
                Window w = (Window) e.getSource();
                Point where = e.getPoint();
                where.translate(-offset.x, -offset.y);
                Point loc = w.getLocationOnScreen();
                loc.translate(where.x, where.y);
                w.setLocation(loc.x, loc.y);
            }
        }
    };
    alphaWindow.addMouseListener(handler);
    alphaWindow.addMouseMotionListener(handler);
    JPanel p = new JPanel(new BorderLayout(8, 8));
    p.setBorder(new EmptyBorder(8, 8, 8, 8));
    p.setTransferHandler(new TransferHandler() {

        private static final long serialVersionUID = 1L;

        public boolean canImport(JComponent comp, DataFlavor[] transferFlavors) {
            List<DataFlavor> list = Arrays.asList(transferFlavors);
            if (list.contains(URL_FLAVOR) || list.contains(URI_LIST_FLAVOR) || list.contains(DataFlavor.imageFlavor) || list.contains(DataFlavor.javaFileListFlavor)) {
                return true;
            }
            if (DataFlavor.selectBestTextFlavor(transferFlavors) != null) {
                return true;
            }
            System.err.println("No acceptable flavor found in " + Arrays.asList(transferFlavors));
            return false;
        }

        public boolean importData(JComponent comp, Transferable t) {
            try {
                if (t.isDataFlavorSupported(URL_FLAVOR)) {
                    URL url = (URL) t.getTransferData(URL_FLAVOR);
                    setImage(Toolkit.getDefaultToolkit().getImage(url));
                    return true;
                }
                if (t.isDataFlavorSupported(URI_LIST_FLAVOR)) {
                    String s = (String) t.getTransferData(URI_LIST_FLAVOR);
                    String[] uris = s.split("[\r\n]");
                    if (uris.length > 0) {
                        URL url = new URL(uris[0]);
                        setImage(Toolkit.getDefaultToolkit().getImage(url));
                        return true;
                    }
                    return false;
                }
                if (t.isDataFlavorSupported(DataFlavor.imageFlavor)) {
                    Image image = (Image) t.getTransferData(DataFlavor.imageFlavor);
                    setImage(image);
                    return true;
                }
                if (t.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
                    List<File> files = (List<File>) t.getTransferData(DataFlavor.javaFileListFlavor);
                    File f = files.get(0);
                    URL url = new URL("file://" + f.toURI().toURL().getPath());
                    Image image = Toolkit.getDefaultToolkit().getImage(url);
                    setImage(image);
                    return true;
                }
                DataFlavor flavor = DataFlavor.selectBestTextFlavor(t.getTransferDataFlavors());
                if (flavor != null) {
                    Reader reader = flavor.getReaderForText(t);
                    char[] buf = new char[512];
                    StringBuilder b = new StringBuilder();
                    int count;
                    // encoding wrong
                    while ((count = reader.read(buf)) > 0) {
                        for (int i = 0; i < count; i++) {
                            if (buf[i] != 0)
                                b.append(buf, i, 1);
                        }
                    }
                    String html = b.toString();
                    Pattern p = Pattern.compile("<img.*src=\"([^\\\"\">]+)\"", Pattern.CANON_EQ | Pattern.UNICODE_CASE);
                    Matcher m = p.matcher(html);
                    if (m.find()) {
                        URL url = new URL(m.group(1));
                        System.out.println("Load image from " + url);
                        Image image = Toolkit.getDefaultToolkit().getImage(url);
                        setImage(image);
                        return true;
                    }
                    System.err.println("Can't parse text: " + html);
                    return false;
                }
                System.err.println("No flavor available: " + Arrays.asList(t.getTransferDataFlavors()));
            } catch (UnsupportedFlavorException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (Throwable e) {
                e.printStackTrace();
            }
            return false;
        }
    });
    p.add(new JLabel("<html><center>Drop an image with an alpha channel onto this window<br>" + "You may also adjust the overall transparency with the slider</center></html>"), BorderLayout.NORTH);
    p.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    final JSlider slider = new JSlider(0, 255, 255);
    slider.addChangeListener(new ChangeListener() {

        public void stateChanged(ChangeEvent e) {
            int value = slider.getValue();
            WindowUtils.setWindowAlpha(alphaWindow, value / 255f);
        }
    });
    p.add(slider, BorderLayout.SOUTH);
    frame.getContentPane().add(p);
    frame.pack();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    centerOnScreen(frame);
    frame.setVisible(true);
    WindowUtils.setWindowTransparent(alphaWindow, true);
    alphaWindow.setLocation(frame.getX() + frame.getWidth() + 4, frame.getY());
    try {
        URL url = getClass().getResource("tardis.png");
        if (url != null) {
            setImage(Toolkit.getDefaultToolkit().getImage(url));
        }
    } catch (Exception e) {
    }
}
Also used : JPanel(javax.swing.JPanel) Matcher(java.util.regex.Matcher) ActionEvent(java.awt.event.ActionEvent) JButton(javax.swing.JButton) Reader(java.io.Reader) Image(java.awt.Image) URL(java.net.URL) GraphicsConfiguration(java.awt.GraphicsConfiguration) DataFlavor(java.awt.datatransfer.DataFlavor) BorderLayout(java.awt.BorderLayout) JFrame(javax.swing.JFrame) JSlider(javax.swing.JSlider) List(java.util.List) ChangeListener(javax.swing.event.ChangeListener) EmptyBorder(javax.swing.border.EmptyBorder) Window(java.awt.Window) JWindow(javax.swing.JWindow) Pattern(java.util.regex.Pattern) MouseEvent(java.awt.event.MouseEvent) JWindow(javax.swing.JWindow) JComponent(javax.swing.JComponent) Transferable(java.awt.datatransfer.Transferable) JLabel(javax.swing.JLabel) Point(java.awt.Point) IOException(java.io.IOException) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) IOException(java.io.IOException) ActionListener(java.awt.event.ActionListener) ChangeEvent(javax.swing.event.ChangeEvent) TransferHandler(javax.swing.TransferHandler) File(java.io.File) MouseInputAdapter(javax.swing.event.MouseInputAdapter)

Example 7 with ActionEvent

use of java.awt.event.ActionEvent in project screenbird by adamhub.

the class RecorderPanel method startCountdown.

/**
     * Initiates countdown and prepares for screen capture. 
     */
private void startCountdown() {
    final Countdown[] countdown = new Countdown[1];
    countdownSec = 6;
    countdownTimer = new Timer(500, new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            countdownSec--;
            log("Counting: " + countdownSec);
            switch(countdownSec) {
                case 0:
                    countdownTimer.stop();
                    jLabel5.setText("Recording");
                    btnRecordNonRec.setText("Stop");
                    btnRecordRec.setText("Stop");
                    btnPlayPauseBackup.setText("");
                    btnPlayPauseBackup.setIcon(pauseIcon);
                    btnFinalizeBackup.setEnabled(true);
                    // Destroy the countdown window.
                    countdown[0].destroy();
                    try {
                        Thread.sleep(500);
                    } catch (Exception ee) {
                    }
                    startRecordState();
                    break;
                case 1:
                case 2:
                case 3:
                case 4:
                    btnPlayPauseBackup.setText(String.valueOf(countdownSec));
                    countdown[0].destroy();
                    countdown[0] = new Countdown(false, recorder);
                    countdown[0].setCount(countdownSec);
                    countdown[0].setVisible(true);
                    try {
                        SoundUtil.tone(880, 500, 0.15);
                    } catch (LineUnavailableException ee) {
                    }
                    break;
                case 5:
                    btnPlayPauseBackup.setText(String.valueOf(countdownSec));
                    // Hide drop box
                    if (captureBox != null) {
                        captureBox.setDragBoxVisible(false);
                    }
                    countdown[0] = new Countdown(false, recorder);
                    countdown[0].setCount(5);
                    countdown[0].setVisible(true);
                    try {
                        SoundUtil.tone(880, 500, 0.15);
                    } catch (LineUnavailableException ee) {
                    }
                    break;
            }
        }
    });
    countdownTimer.start();
}
Also used : Timer(javax.swing.Timer) ActionListener(java.awt.event.ActionListener) ActionEvent(java.awt.event.ActionEvent) LineUnavailableException(javax.sound.sampled.LineUnavailableException) Countdown(com.bixly.pastevid.screencap.components.capturebox.Countdown) MissingResourceException(java.util.MissingResourceException) FileNotFoundException(java.io.FileNotFoundException) LineUnavailableException(javax.sound.sampled.LineUnavailableException) IOException(java.io.IOException)

Example 8 with ActionEvent

use of java.awt.event.ActionEvent in project SKMCLauncher by SKCraft.

the class VersionListDialog method initComponents.

private void initComponents() {
    JPanel mainPanel = new JPanel();
    versionsList = new JList();
    JScrollPane versionsScroll = new JScrollPane(versionsList);
    LinedBoxPanel buttonsPanel = new LinedBoxPanel(true).fullyPadded();
    JButton cancelButton = new JButton(_("button.cancel"));
    JButton selectButton = new JButton(_("button.select"));
    mainPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
    mainPanel.setLayout(new BorderLayout());
    mainPanel.add(versionsScroll, BorderLayout.CENTER);
    buttonsPanel.addGlue();
    buttonsPanel.addElement(selectButton);
    buttonsPanel.addElement(cancelButton);
    add(mainPanel, BorderLayout.CENTER);
    add(buttonsPanel, BorderLayout.SOUTH);
    cancelButton.addActionListener(ActionListeners.dispose(this));
    selectButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            Object selected = versionsList.getSelectedValue();
            if (selected != null && selected instanceof Version) {
                version = (Version) selected;
            }
            dispose();
        }
    });
    getRootPane().setDefaultButton(selectButton);
    versionsList.addMouseListener(new DoubleClickToButtonAdapter(selectButton));
    ListenableFuture<?> future = executor.submit(new AbstractWorker<Object>() {

        @Override
        protected void run() throws Exception {
            try {
                setVersions(application.getAvailable());
            } catch (IOException e) {
                dispose();
                throw new LauncherException(e, _("versionList.failedFetchError"));
            } catch (InterruptedException e) {
                dispose();
            }
        }

        @Override
        public String getLocalizedTitle() {
            return _("selectVersions.fetchingVersionsTitle");
        }

        @Override
        public boolean shouldConfirmInterrupt() {
            return false;
        }
    });
    SwingHelper.addErrorDialogCallback(future, this);
}
Also used : LauncherException(com.sk89q.skmcl.LauncherException) ActionEvent(java.awt.event.ActionEvent) IOException(java.io.IOException) IOException(java.io.IOException) LauncherException(com.sk89q.skmcl.LauncherException) ActionListener(java.awt.event.ActionListener) Version(com.sk89q.skmcl.application.Version)

Example 9 with ActionEvent

use of java.awt.event.ActionEvent in project SKMCLauncher by SKCraft.

the class ConsoleFrame method initComponents.

/**
     * Add components to the frame.
     */
private void initComponents() {
    JButton pastebinButton = new JButton(_("console.uploadLog"));
    buttonsPanel = new LinedBoxPanel(true);
    buttonsPanel.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8));
    buttonsPanel.addElement(pastebinButton);
    add(buttonsPanel, BorderLayout.NORTH);
    add(messageLog, BorderLayout.CENTER);
    pastebinButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            pastebinLog();
        }
    });
}
Also used : ActionListener(java.awt.event.ActionListener) ActionEvent(java.awt.event.ActionEvent)

Example 10 with ActionEvent

use of java.awt.event.ActionEvent in project SKMCLauncher by SKCraft.

the class LoginDialog method bindListeners.

private void bindListeners() {
    idCombo.setComponentPopupMenu(TextFieldPopupMenu.INSTANCE);
    passwordText.setComponentPopupMenu(TextFieldPopupMenu.INSTANCE);
    idCombo.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            updateSelection();
        }
    });
    forgotLogin.addActionListener(ActionListeners.openURL(forgotLogin, "https://minecraft.net/resetpassword"));
    loginButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            prepareLogin();
        }
    });
    cancelButton.addActionListener(ActionListeners.dispose(this));
    rememberPassCheck.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            if (rememberPassCheck.isSelected()) {
                rememberIdCheck.setSelected(true);
            }
        }
    });
    rememberIdCheck.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            if (!rememberIdCheck.isSelected()) {
                rememberPassCheck.setSelected(false);
            }
        }
    });
}
Also used : ActionListener(java.awt.event.ActionListener) ActionEvent(java.awt.event.ActionEvent)

Aggregations

ActionEvent (java.awt.event.ActionEvent)3561 ActionListener (java.awt.event.ActionListener)2755 JButton (javax.swing.JButton)1116 JPanel (javax.swing.JPanel)961 JLabel (javax.swing.JLabel)606 JMenuItem (javax.swing.JMenuItem)550 Dimension (java.awt.Dimension)531 AbstractAction (javax.swing.AbstractAction)510 BorderLayout (java.awt.BorderLayout)381 GridBagConstraints (java.awt.GridBagConstraints)301 GridBagLayout (java.awt.GridBagLayout)292 Insets (java.awt.Insets)288 JScrollPane (javax.swing.JScrollPane)274 BoxLayout (javax.swing.BoxLayout)262 JTextField (javax.swing.JTextField)262 FlowLayout (java.awt.FlowLayout)257 JCheckBox (javax.swing.JCheckBox)242 JMenu (javax.swing.JMenu)217 Point (java.awt.Point)203 ImageIcon (javax.swing.ImageIcon)191