Tuesday, February 21, 2012

Swing Slider Example

I wrote a Slider 'Hello World' program to make sure I understand how to use them to control another value in the user interface.

Here is that self contained simple example.

package ca.sarah_happy.sandbox.circles;

import java.awt.BorderLayout;
import java.awt.Container;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JSlider;
import javax.swing.SwingUtilities;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class SliderSample implements Runnable {
    public static void main(String[] args) {
        /*
         * do all the work on the GUI thread.
         */
        SwingUtilities.invokeLater(new SliderSample());
    }

    /*
     * things I will need access to later.
     */

    private JSlider slider;
    private JLabel label;

    /*
     * do the work of creating the user interface.
     */

    @Override
    public void run() {
        slider = new JSlider();
        slider.setPaintTicks(true);
        slider.setPaintLabels(true);
        slider.setMaximum(127);
        slider.setMinimum(-128);
        slider.setMajorTickSpacing(32);
        slider.setMinorTickSpacing(8);
        slider.setValue(0);

        label = new JLabel("value = " + slider.getValue());

        JFrame frame = new JFrame("Slider Sample");
        frame.setLocationByPlatform(true);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        Container p = frame.getContentPane();
        p.add(label, BorderLayout.SOUTH);
        p.add(slider);

        slider.addChangeListener(sliderChangeListener);

        frame.pack();
        frame.setVisible(true);
    }

    /*
     * and things that will be triggered later
     */

    private ChangeListener sliderChangeListener = new ChangeListener() {
        @Override
        public void stateChanged(ChangeEvent e) {
            String adjust = "";
            if (slider.getValueIsAdjusting()) {
                adjust = " (adjusting)";
            }
            label.setText("value = " + slider.getValue() + adjust);
        }
    };
}

Saturday, February 11, 2012

tracking mouse events example

I expect to need a program to inspect mouse events soon. I intend to find out how mouse events are delivered on a touch screen, when I have access to such a device. In the mean time I put together a program to display the mouse events that are visible to a java application.

package org.yi.happy.mouse;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

/**
 * Track mouse events over a panel and log in a text area.
 */
public class MouseTrackMain implements Runnable {
    private final JTextArea textArea;
    {
        textArea = new JTextArea(10, 80);
        textArea.setEditable(false);
    }

    private void log(String event) {
        textArea.append(event + "\n");
    }

    private void log(String event, MouseEvent e) {
        log(event + ": " + e);
    }

    private MouseListener mouse = new MouseListener() {
        @Override
        public void mouseClicked(MouseEvent e) {
            log("click", e);
        }

        @Override
        public void mousePressed(MouseEvent e) {
            log("pressed", e);
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            log("released", e);
        }

        @Override
        public void mouseEntered(MouseEvent e) {
            log("entered", e);
        }

        @Override
        public void mouseExited(MouseEvent e) {
            log("exited", e);
        }
    };

    private MouseMotionListener mouseMotion = new MouseMotionListener() {
        @Override
        public void mouseMoved(MouseEvent e) {
            log("moved", e);
        }

        @Override
        public void mouseDragged(MouseEvent e) {
            log("dragged", e);
        }
    };

    @Override
    public void run() {
        JPanel blankPanel = new JPanel();
        blankPanel.setPreferredSize(new Dimension(400, 300));
        blankPanel.setBackground(new Color(0x7f, 0x7f, 0xff));

        JScrollPane scroll = new JScrollPane(textArea);

        JSplitPane split = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
        split.add(blankPanel);
        split.add(scroll);

        JFrame window = new JFrame("Mouse Track");
        Container panel = window.getContentPane();
        panel.setLayout(new BorderLayout());
        panel.add(split);

        blankPanel.addMouseListener(mouse);
        blankPanel.addMouseMotionListener(mouseMotion);

        window.pack();
        window.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        window.setLocationByPlatform(true);
        window.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new MouseTrackMain());
    }
}

This program is laid out so that there are no forward references.