Thursday, August 14, 2014

JSlider Color Painter GUI Java [Swing]

This application has two JSlider s which paint a JPanel as their values change. The JSliders represent the coordinates of the JPanel to paint. A clear button clears the Panel.


Code:

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.event.*;

class Point {

    int x;
    int y;

    Point(int x, int y) {
        this.x = x;
        this.y = y;
    }
}

class Display extends JPanel {

    ArrayList<Point> points = new ArrayList<>();
    Point previous, newPoint;
    Color color = Color.RED;

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D gg = (Graphics2D) g;

        gg.setColor(color);

        previous = null;

        for (Point p : points) {
            if (previous == null) {
                previous = p;
                continue;
            }
            gg.drawLine(previous.x, previous.y, p.x, p.y);
            previous = p;
        }
    }
}

public class SketchBox2 extends JFrame {

    JButton clear = new JButton("Clear Points [0]");
    private Display screen = new Display();
    private JSlider xAxis = new JSlider(0, 0, 0);
    private JSlider yAxis = new JSlider(JSlider.VERTICAL, 0, 0, 0);

    SketchBox2() {
        yAxis.setInverted(true);
        xAxis.addChangeListener(cl);
        yAxis.addChangeListener(cl);
        xAxis.setValue(0);
        yAxis.setValue(0);

        setTitle("Painter");

        add(BorderLayout.CENTER, screen);
        add(BorderLayout.NORTH, clear);
        add(BorderLayout.WEST, yAxis);
        add(BorderLayout.SOUTH, xAxis);

        screen.addComponentListener(new ComponentAdapter() {
            public void componentResized(ComponentEvent e) {
                yAxis.setMaximum(screen.getHeight());
                xAxis.setMaximum(screen.getWidth());
            }
        });

        setSize(500, 500);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);

        clear.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                screen.points.clear();
                screen.repaint();
                clear.setText(
                        "[Erase] points.size() = "
                        + screen.points.size());
            }
        });

        clear.doClick();
    }

    ChangeListener cl = new ChangeListener() {

        @Override
        public void stateChanged(ChangeEvent e) {
            screen.newPoint = new Point(xAxis.getValue(), yAxis.getValue());
            screen.points.add(screen.newPoint);

            clear.setText(
                    "[Erase] points.size() = "
                    + screen.points.size());
            screen.repaint();
        }
    };

    public static void main(String[] args) {
        new SketchBox2();
    }
}

[Concept : Thinking In Java 4th Edition GUI Ex24]


No comments:

Post a Comment