Search in sources :

Example 1 with UniverseListener

use of ij3d.UniverseListener in project TrakEM2 by trakem2.

the class Display3DGUI method newPanelColors.

private static final JPanel newPanelColors(final Image3DUniverse univ) {
    final JPanel p = new JPanel();
    p.setBackground(Color.white);
    final GridBagLayout gb = new GridBagLayout();
    final GridBagConstraints c = new GridBagConstraints();
    c.anchor = GridBagConstraints.NORTHWEST;
    c.fill = GridBagConstraints.HORIZONTAL;
    p.setLayout(gb);
    final String[] labels = new String[] { "Red", "Green", "Blue" };
    final JScrollBar[] sliders = new JScrollBar[3];
    final JTextField[] typers = new JTextField[3];
    for (int i = 0; i < 3; ++i) {
        final JScrollBar slider = new JScrollBar(JScrollBar.HORIZONTAL, 255, 1, 0, 256);
        sliders[i] = slider;
        final JTextField typer = new IntegerField(255, 3);
        typers[i] = typer;
        final int k = i;
        slider.addAdjustmentListener(new AdjustmentListener() {

            @Override
            public void adjustmentValueChanged(final AdjustmentEvent e) {
                final Content content = univ.getSelected();
                if (null == content) {
                    Utils.log("Nothing selected!");
                    return;
                }
                Color3f color = content.getColor();
                // default to yellow
                if (null == color)
                    color = new Color3f(1, 1, 0);
                final float[] co = new float[3];
                color.get(co);
                co[k] = e.getValue() / 255.0f;
                content.setColor(new Color3f(co));
                typer.setText(Integer.toString(e.getValue()));
            }
        });
        typer.addKeyListener(new SliderTyperLink(univ, slider, typer));
        final JLabel l = new JLabel(labels[i]);
        // Layout
        c.gridx = 0;
        c.gridy = i;
        gb.setConstraints(l, c);
        p.add(l);
        c.gridx = 1;
        c.weightx = 1;
        c.fill = GridBagConstraints.HORIZONTAL;
        gb.setConstraints(slider, c);
        p.add(slider);
        c.gridx = 2;
        c.weightx = 0;
        c.fill = GridBagConstraints.NONE;
        gb.setConstraints(typer, c);
        p.add(typer);
    }
    // Alpha slider
    c.gridx = 0;
    c.gridy += 1;
    final JLabel aL = new JLabel("Alpha:");
    gb.setConstraints(aL, c);
    p.add(aL);
    c.gridx = 1;
    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = 1;
    final JScrollBar alphaSlider = new JScrollBar(JScrollBar.HORIZONTAL, 255, 1, 0, 256);
    gb.setConstraints(alphaSlider, c);
    p.add(alphaSlider);
    c.gridx = 2;
    c.fill = GridBagConstraints.NONE;
    c.weightx = 0;
    final JTextField alphaTyper = new IntegerField(255, 3);
    gb.setConstraints(alphaTyper, c);
    p.add(alphaTyper);
    alphaSlider.addAdjustmentListener(new AdjustmentListener() {

        @Override
        public void adjustmentValueChanged(final AdjustmentEvent e) {
            final Content content = univ.getSelected();
            if (null == content) {
                Utils.log("Nothing selected!");
                return;
            }
            final float alpha = e.getValue() / 255.0f;
            content.setTransparency(1 - alpha);
            alphaTyper.setText(Integer.toString(e.getValue()));
        }
    });
    alphaTyper.addKeyListener(new SliderTyperLink(univ, alphaSlider, alphaTyper));
    // Button to colorize randomly
    c.gridx = 0;
    c.gridy += 1;
    c.gridwidth = 3;
    c.weightx = 1;
    c.insets = new Insets(15, 4, 4, 4);
    final JButton r = new JButton("Assign random colors to all");
    r.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(final ActionEvent e) {
            randomizeColors(univ);
        }
    });
    gb.setConstraints(r, c);
    p.add(r);
    addTitledLineBorder(p, "Colors");
    univ.addUniverseListener(new UniverseListener() {

        @Override
        public void universeClosed() {
        }

        @Override
        public void transformationUpdated(final View arg0) {
        }

        @Override
        public void transformationStarted(final View arg0) {
        }

        @Override
        public void transformationFinished(final View arg0) {
        }

        @Override
        public void contentSelected(final Content arg0) {
            if (null == arg0) {
                return;
            }
            Color3f color = arg0.getColor();
            // default to yellow
            if (null == color)
                color = new Color3f(1, 1, 0);
            final float[] co = new float[3];
            color.get(co);
            for (int i = 0; i < 3; ++i) {
                // Disallow the slider from firing an event when its value is adjusted
                sliders[i].setValueIsAdjusting(true);
                final int val = (int) (co[i] * 255);
                typers[i].setText(Integer.toString(val));
                sliders[i].setValue(val);
            }
            // After all are set, re-enable, which triggers events (the color will be set three times...)
            for (int i = 0; i < 3; ++i) {
                sliders[i].setValueIsAdjusting(false);
            }
            // Alpha slider:
            alphaSlider.setValueIsAdjusting(true);
            final int alpha = (int) ((1 - arg0.getTransparency()) * 255);
            alphaTyper.setText(Integer.toString(alpha));
            alphaSlider.setValue(alpha);
            alphaSlider.setValueIsAdjusting(false);
        }

        @Override
        public void contentRemoved(final Content arg0) {
        }

        @Override
        public void contentChanged(final Content arg0) {
            if (arg0 == univ.getSelected()) {
                contentSelected(arg0);
            }
        }

        @Override
        public void contentAdded(final Content arg0) {
        }

        @Override
        public void canvasResized() {
        }
    });
    return p;
}
Also used : JPanel(javax.swing.JPanel) GridBagConstraints(java.awt.GridBagConstraints) Insets(java.awt.Insets) GridBagLayout(java.awt.GridBagLayout) ActionEvent(java.awt.event.ActionEvent) AdjustmentEvent(java.awt.event.AdjustmentEvent) Color3f(org.scijava.vecmath.Color3f) JButton(javax.swing.JButton) JLabel(javax.swing.JLabel) IntegerField(ini.trakem2.utils.IntegerField) JTextField(javax.swing.JTextField) UniverseListener(ij3d.UniverseListener) View(org.scijava.java3d.View) JScrollBar(javax.swing.JScrollBar) ActionListener(java.awt.event.ActionListener) Content(ij3d.Content) AdjustmentListener(java.awt.event.AdjustmentListener)

Aggregations

Content (ij3d.Content)1 UniverseListener (ij3d.UniverseListener)1 IntegerField (ini.trakem2.utils.IntegerField)1 GridBagConstraints (java.awt.GridBagConstraints)1 GridBagLayout (java.awt.GridBagLayout)1 Insets (java.awt.Insets)1 ActionEvent (java.awt.event.ActionEvent)1 ActionListener (java.awt.event.ActionListener)1 AdjustmentEvent (java.awt.event.AdjustmentEvent)1 AdjustmentListener (java.awt.event.AdjustmentListener)1 JButton (javax.swing.JButton)1 JLabel (javax.swing.JLabel)1 JPanel (javax.swing.JPanel)1 JScrollBar (javax.swing.JScrollBar)1 JTextField (javax.swing.JTextField)1 View (org.scijava.java3d.View)1 Color3f (org.scijava.vecmath.Color3f)1