Beginners Guide: Java Tutorials

Hey, since I’m also still a student, and I know how it is if you’ve just started using Java, one of the most or even the most powerful object oriented language, I’d like to use this thread as a collective thread for simple tutorials, like printing text lines with different colors, creating a simple color picker using JSlide, etc.

RGB ColorSlider


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

public class ColorSlide extends JFrame implements ChangeListener {
    ColorPanel canvas = new ColorPanel();
    JSlider red = new JSlider(0, 255, 255);
    JSlider green = new JSlider(0, 255, 0);
    JSlider blue = new JSlider(0, 255, 0);

    public void init()
{
//rest of the init() code
}

    public ColorSlide() {
        super("Your Window Title");
        setSize(280, 300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(false);

        red.setMajorTickSpacing(50);
        red.setMinorTickSpacing(10);
        red.setPaintTicks(true);
        red.setPaintLabels(true);
        red.addChangeListener(this);

        green.setMajorTickSpacing(50);
        green.setMinorTickSpacing(10);
        green.setPaintTicks(true);
        green.setPaintLabels(true);
        green.addChangeListener(this);

        blue.setMajorTickSpacing(50);
        blue.setMinorTickSpacing(10);
        blue.setPaintTicks(true);
        blue.setPaintLabels(true);
        blue.addChangeListener(this);

        JLabel redLabel = new JLabel("Red: ");
        JLabel greenLabel = new JLabel("Green: ");
        JLabel blueLabel = new JLabel("Blue: ");
        GridLayout grid = new GridLayout(4, 1);
        FlowLayout right = new FlowLayout(FlowLayout.RIGHT);
        setLayout(grid);

        JPanel redPanel = new JPanel();
        redPanel.setLayout(right);
        redPanel.add(redLabel);
        redPanel.add(red);
        add(redPanel);

        JPanel greenPanel = new JPanel();
        greenPanel.setLayout(right);
        greenPanel.add(greenLabel);
        greenPanel.add(green);
        add(greenPanel);

        JPanel bluePanel = new JPanel();
        bluePanel.setLayout(right);
        bluePanel.add(blueLabel);
        bluePanel.add(blue);
        add(bluePanel);
        add(canvas);

        setVisible(true);
    }

    public void stateChanged(ChangeEvent evt) {
        JSlider source = (JSlider)evt.getSource();
        if (source.getValueIsAdjusting() != true) {
            Color current = new Color(red.getValue(), green.getValue(),
                blue.getValue());
            canvas.changeColor(current);
            canvas.repaint();
        }
    }

    public Insets getInsets() {
        Insets border = new Insets(45, 10, 10, 10);
        return border;
    }

    public static void main(String[] arguments) {
        ColorSlide cs = new ColorSlide();
    }
}

class ColorPanel extends JPanel {
    Color background;

    ColorPanel() {
        background = Color.pink;
    }

    public void paintComponent(Graphics comp) {
        Graphics2D comp2D = (Graphics2D)comp;
        comp2D.setColor(background);
        comp2D.fillRect(0, 0, getSize().width, getSize().height);
    }

    void changeColor(Color newBackground) {
        background = newBackground;
    }
}

I know that it’s newbie stuff, for all who already know about each argument, etc. In this example, I’d like to challenge myself, and try something new, - adding a textfield that displays the modified RGB code, since it’s “eye-sensitive” to look at each bar, and write the code down etc.

If anyone is using NetBeans, please tell me how to add images to the folder, since it won’t let me. :eek: