// Main JFrame

/*
 * @(#)ArrayMe.java 1.0 03/02/19
 *
 * You can modify the template of this file in the
 * directory ..\JCreator\Templates\Template_1\Project_Name.java
 *
 * You can also create your own project template by making a new
 * folder in the directory ..\JCreator\Template\. Use the other
 * templates as examples.
 *
 */

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

class ArrayMe extends JFrame {
	
	// Send a pointer from the main JFrame application
	// object to the 'inputs' panel so it can call back
	// to the JFrame to update the 'output' textArea.
	private MyPanel2 inputs = new MyPanel2(this);

	private RedPanel brianP = new RedPanel("Brian");
	private RedPanel other = new RedPanel("Other");
	private JTextArea output = new JTextArea();
	private JScrollPane scroller = new JScrollPane(output);
	
	private JTextField t1 = new JTextField("Hello from Brian");
	
	public ArrayMe() {
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				dispose();
				System.exit(0);
			}
		});
		
		brianP.add(t1);
		other.setLayout(new BorderLayout());
		other.add(brianP, BorderLayout.NORTH);
		other.add(scroller, BorderLayout.CENTER);
		
		Container c = getContentPane();
		c.add(inputs, BorderLayout.NORTH);
		c.add(other, BorderLayout.CENTER);
	}

	public static void main(String args[]) {
		System.out.println("Starting ArrayMe...");
		ArrayMe mainFrame = new ArrayMe();
		mainFrame.setSize(400, 300);
		mainFrame.setTitle("ArrayMe");
		mainFrame.setVisible(true);
	}
	
	public void updateOutput(String str) {
		output.setText(str);	
	}
}

// Fancy Panel

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

public class MyPanel2 extends RedPanel {
	
	JTextField index = new JTextField("0",3);
	JTextField value = new JTextField("0",3);
	
	JLabel iLabel = new JLabel(" index:");
	JLabel vLabel = new JLabel(" value:");
	
	JButton insert = new JButton("Insert");
	JButton reset = new JButton("Reset");
	
	IntArray data = new IntArray();
	ArrayMe parent; //a reference to the main application
			//  object, now you can call its' public
			//  methods.
	
	public MyPanel2(ArrayMe parent) {
		super("Inputs");
		this.parent = parent;
		JComponent[] items = {iLabel, index, vLabel, value, insert,
		                             reset };
		addItems(items);
			
		ActionListener insertIt = new InsertListener();
		insert.addActionListener(insertIt);
		index.addActionListener(insertIt);
		value.addActionListener(insertIt);
		reset.addActionListener(new ResetListener());
	}
	
	private class InsertListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			int loc = Integer.parseInt(index.getText());
			int val = Integer.parseInt(value.getText());
			data.set(loc, val);

			// This is calling back to the main
			// application object.
			parent.updateOutput(data.toString());

			index.selectAll();
			value.selectAll();
			index.requestFocus();
		}	
	}
	
	private class ResetListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			data = new IntArray();

			// This is calling back to the main
			// application object.
			parent.updateOutput(data.toString());
		}	
	}

}

// Support Classes

import javax.swing.*;
import java.awt.Color;
import javax.swing.border.*;

public class RedPanel extends JPanel {
	
	public RedPanel(String title) {
		super();
		setBackground(Color.RED);
		setBorder(makeBorder(title));	
	}
	
	public void addItems(JComponent[] items) {
		
		for (int i = 0; i < items.length; i++) {
			this.add(items[i]);	
		}	
	}
	
	private CompoundBorder makeBorder(String title) {
		Border a = BorderFactory.createEmptyBorder(5, 5, 5, 5);
		Border b = BorderFactory.createTitledBorder(title);
		CompoundBorder c = BorderFactory.createCompoundBorder(a,b);
		
		return c;
	}
}
public class IntArray {
	
	private int[] nums;
	private int size;
	
	public IntArray() {
		this(10);
	}	
	
	public IntArray(int size) {
		nums = new int[size];
		this.size = size;	
	}
	
	public void set(int index, int value) {
		
		if (index >= size) {
			nums = resize(index + 1);
		}
		
		nums[index] = value;
		
	}
	
	public int get(int index) {
		return nums[index];
	}
	
	private int[] resize(int newSize) {
		
		int[] temp = new int[newSize];
		
		int max = Math.min(size, newSize);
		
		for (int i = 0; i < max; i++) {
			temp[i] = nums[i];	
		}
		
		size = newSize;
		
		return temp;
	}
	
	public String toString() {
		java.util.Vector output = new java.util.Vector();
		
		for (int i = 0; i < size; i++) {
			output.add(new Integer(nums[i]));	
		}	
		
		return output.toString();
	}
	
	public static void main(String[] args) {
		IntArray x = new IntArray();
		
		x.set(0, 4);
		x.set(5, 1);
		x.set(15, 15);
		
		System.out.println(x);		
	}
}