-6

I have this code:

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

public class ColorSliders 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 ColorSliders() {
        super("Color Slide");
        setSize(270, 300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);

        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 event) {
        JSlider source = (JSlider) event.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) {
        ColorSliders cs = new ColorSliders();
    }
}

class ColorPanel extends JPanel {
    Color background;

    ColorPanel() {
        background = Color.red;
    }

    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;
    }
}

Why does it defines 2 classes within the same class in something like Eclipse?

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
TimLinden
  • 117
  • 1
  • 8

2 Answers2

3

There is no any class inside another class, This source code has two class seperately. We can write many classes inside a single source code. easy of transport could be the reason. Note that when you write multiple classes in the same source code, you can only define one public class and its name should match with the file name of the source code.

user3717646
  • 436
  • 3
  • 10
0

The short answer is that having multiple top-level classes in a single source file is an anti-pattern and you should avoid doing it even though you can.

emory
  • 10,725
  • 2
  • 30
  • 58
  • It isn't an anti pattern. It heavily depends on the design. – Luiggi Mendoza Jul 04 '14 at 16:09
  • To which are you referring to: (1) having multiple top level classes in a single source file; or (2) having non-public top-level classes. I agree #2 is not an anti-pattern and edited my answer to specify #1. – emory Jul 04 '14 at 16:15