All pastes #753274 Raw Edit

Assignment1.java

public java v1 · immutable
#753274 ·published 2007-10-29 02:42 UTC
rendered paste body
import java.awt.*; import java.applet.*;import java.awt.event.*;public class Assignment1 extends Applet implements ActionListener {		// Constants 	private static final Color defaultTopColor = Color.red;	private static final Color defaultMiddleColor = Color.yellow;	private static final Color defaultBottomColor = Color.blue;	private static final String RED = "Red";	private static final String YELLOW = "Yellow";	private static final String BLUE = "Blue";	private static final String RESET = "Reset";	private static final String TOP_LIST_LABEL = "Top Row Colors";	private static final String MIDDLE_LIST_LABEL = "Middle Row Colors";	private static final String BOTTOM_LIST_LABEL = "Bottom Row Colors";		// Instance Variables	private Panel mainPanel, boardPanel, listsPanel;	private Board board;	private Button resetButton;	private List topRowColorList, middleRowColorList, bottomRowColorList;		public void init() { 		// Create the main panel and all of its components and add them to panel applet.		mainPanel = makeMainPanel();		boardPanel = makeBoardPanel();		listsPanel = makeListsPanel();		this.add(mainPanel);		this.addListeners();	}		private List makeColorList(int itemNumber){		// Create a List with the 3 colour options; preselect the item with the given itemNumber		List list = new List(3, false);		list.add(RED);		list.add(YELLOW);		list.add(BLUE);		list.select(itemNumber);		return list;	}	private Panel makeListsPanel() {		// Make Labels and Lists for row colour choices, add them to a Panel		// The Panel uses GridLayout, 2 rows and 3 columns		topRowColorList = makeColorList(0);		middleRowColorList = makeColorList(1);		bottomRowColorList = makeColorList(2);		Panel panel = new Panel();		panel.setLayout(new GridLayout(2, 3, 0, 0));		panel.add(new Label(Assignment1.TOP_LIST_LABEL, 1));		panel.add(new Label(Assignment1.MIDDLE_LIST_LABEL, 1));		panel.add(new Label(Assignment1.BOTTOM_LIST_LABEL, 1));		panel.add(this.topRowColorList);		panel.add(this.middleRowColorList);		panel.add(this.bottomRowColorList);		return panel;	}		private Panel makeBoardPanel() {		// Create a Board using the default row colours, create a reset Button;		// add panel Board and Button to a Panel (which uses FlowLayout)		Panel panel = new Panel();		panel.setLayout(new FlowLayout());		board = new Board(Assignment1.defaultTopColor, Assignment1.defaultMiddleColor, Assignment1.defaultBottomColor);		panel.add(this.board.getBoard());		resetButton = new Button(Assignment1.RESET);		panel.add(this.resetButton);		return panel;	}		private Panel makeMainPanel() {		// create and return the Panel which contains the boardPanel and the ListsPanel 		// The Panel uses GridLayout, 2 rows and 1 column		boardPanel = makeBoardPanel();		listsPanel = makeListsPanel();		Panel panel = new Panel();		panel.setLayout(new GridLayout(2, 1, 0, 0));		panel.add(this.boardPanel);		panel.add(this.listsPanel);		return panel;	}			private void addListeners() {		// Adds the Applet as the ActionListener for the various components		board.getSquare("1").addActionListener(this);        board.getSquare("2").addActionListener(this);        board.getSquare("3").addActionListener(this);        board.getSquare("4").addActionListener(this);        board.getSquare("5").addActionListener(this);        board.getSquare("6").addActionListener(this);        board.getSquare("7").addActionListener(this);        board.getSquare("8").addActionListener(this);        board.getSquare("9").addActionListener(this);        resetButton.addActionListener(this);        topRowColorList.addActionListener(this);        middleRowColorList.addActionListener(this);        bottomRowColorList.addActionListener(this);	}		private Color makeColor(String colString) {		// Return a Color corresponding to the given String		if(colString.equals(RED))            return Color.red;        if(colString.equals(YELLOW))            return Color.yellow;        if(colString.equals(BLUE))            return Color.blue;        else            return null;	}		public void actionPerformed(ActionEvent event) {		// Responds appropriately to whichever button has been clicked		// If Reset has been clicked, change row colours to the selected colours, else move the square that has been clicked		Color topColor = makeColor(this.topRowColorList.getSelectedItem());        Color middleColor = makeColor(this.middleRowColorList.getSelectedItem());        Color bottomColor = makeColor(this.bottomRowColorList.getSelectedItem());        String whichButton = event.getActionCommand();        if (whichButton.equals(Assignment1.RESET))        {            this.board.changeRowColor(1, topColor);            this.board.changeRowColor(2, middleColor);            this.board.changeRowColor(3, bottomColor);            this.board.paintSquares();        }         else        {        	this.board.move(whichButton);        }	}	}// end of Assignment1 class