Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.awt.Dimension;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.util.HashSet;
import java.util.Set;

import javax.swing.JFrame;

public class Main extends JFrame {
    State state;
    Set<StateListener> listeners = new HashSet<StateListener>();

    public Main() {
        state = new State();
        addComponentListener(new ComponentAdapter() {
            @Override
            public void componentResized(ComponentEvent e) {
                state.setSize(getSize(), true);
            }

            @Override
            public void componentMoved(ComponentEvent e) {
            }

            @Override
            public void componentShown(ComponentEvent e) {
            }

            @Override
            public void componentHidden(ComponentEvent e) {
            }
        });
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }

    private void addListener(StateListener sl) {
        listeners.add(sl);
    }

    public void associateWith(Main other) {
        other.addListener(new MirrorStateListener());
    }

    public static void main(String[] args) {
        Main f1 = new Main();
        Main f2 = new Main();
        f1.associateWith(f2);
        f2.associateWith(f1);
        f1.setSize(300, 300);
        f2.setLocation(600, 10);
    }

    class State {
        Dimension size;

        public void setSize(Dimension newSize, boolean fireEvent) {
            if (newSize.equals(size)) {
                return;
            }
            size = newSize;
            Main.this.setSize(size);
            if (fireEvent) {
                for (StateListener sl : listeners) {
                    sl.sizeChanged(size);
                }
            }
        }
    }

    class MirrorStateListener implements StateListener {
        @Override
        public void sizeChanged(Dimension newSize) {
            state.setSize(newSize, false);
        }
    }
}

interface StateListener {
    void sizeChanged(Dimension newSize);
}