Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Graphics;

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

public class Main {
    Icon icon = new DynamicIcon();
    final JSlider width = new JSlider(JSlider.HORIZONTAL, 1, 150, 75);
    final JSlider height = new JSlider(JSlider.VERTICAL, 1, 150, 75);
    final JLabel dynamicLabel = new JLabel(icon);

    Main() {
        Updater updater = new Updater();
        width.addChangeListener(updater);
        height.addChangeListener(updater);
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        f.add(width, BorderLayout.NORTH);
        f.add(height, BorderLayout.WEST);
        f.add(dynamicLabel, BorderLayout.CENTER);
        f.setSize(210, 210);
        f.setVisible(true);

    }

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

    class Updater implements ChangeListener {
        public void stateChanged(ChangeEvent ev) {
            dynamicLabel.repaint();
        }
    }

    class DynamicIcon implements Icon {
        public int getIconWidth() {
            return width.getValue();
        }

        public int getIconHeight() {
            return height.getValue();
        }

        public void paintIcon(Component c, Graphics g, int x, int y) {
            g.fill3DRect(x, y, getIconWidth(), getIconHeight(), true);
        }
    }
}