State Applet

<< Curve-O-Matic | OtherProjectsTrailIndex | Other Planets >>

See a working copy here.

If its hard to see how the sorting is done, take a look here

Using the utility class Collections we can sort our states. There are a variety of ways to sort them, and there are a variety of ways to compare objects in java.

  1. implementing the compareTo method
  2. making a static anonymous class that implements Comparator in the State Class
  3. making an anonymous class that implements Comparator in the applet
  4. Making a separate class that implements Comparator

each of the implementations of the Comparator interface have to implement the compare method.

See if you can add a getDensity method in the State class, and add that to the Panel ad then add the choice to sort by the population density!

State.java

import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.util.Comparator;


public class State implements Comparable<State>
{
	private String name;
	private String abbr;
	private int pop;
	private int area;
	public State(String name, String abbr, int pop, int area ){
		this.setName(name);
		this.setAbbr(abbr);
		this.setPop(pop);
		this.setArea(area);
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getName() {
		return name;
	}
	public void setAbbr(String abbr) {
		this.abbr = abbr;
	}
	public String getAbbr() {
		return abbr;
	}
	public void setPop(int pop) {
		this.pop = pop;
	}
	public int getPop() {
		return pop;
	}
	public void setArea(int area) {
		this.area = area;
	}
	public int getArea() {
		return area;
	}
	public Panel getPanel(){
		Panel result=new Panel(new GridLayout(1,0));
		result.add(new Label (getName()));
		result.add(new Label (getAbbr()));
		result.add(new Label (""+getPop()));
		result.add(new Label (""+getArea()));		
		return result;
	}
	@Override
	public int compareTo(State other) {

		return name.compareTo(other.getName());
	}
	public static Comparator<State> AbbrComparator = new Comparator<State>(){

		@Override
		public int compare(State state1, State state2) {
			return state1.getAbbr().compareTo(state2.getAbbr());
		}

	};

}

AreaComparator.java

import java.util.Comparator;

public class AreaComparator implements Comparator<State> {

	@Override
	public int compare(State s1, State s2) {

		return s1.getArea()-s2.getArea();
	}

}

StateApplet.java


import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import javax.swing.ButtonGroup;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;


@SuppressWarnings("serial")
public class StateApplet extends Applet implements ActionListener
{
	private ArrayList<State> states;
	private Panel statePanel;
	private JScrollPane sp;
	private Button sortButton;
	JRadioButton name, abbr, pop, area, ascending, descending;
	public void init(){
		states= new ArrayList<State>();
		states.add(new State("Alabama","AL",4802982,50744));
		states.add(new State("Alaska","AK",721523,571951));
		states.add(new State("Arizona","AZ",6412700,113635));
		states.add(new State("Arkansas","AR",2926229,52068));
		states.add(new State("California","CA",37341989,155959));
		states.add(new State("Colorado","CO",4939456,103718));
		states.add(new State("Connecticut","CT",3581628,4845));
		states.add(new State("Delaware","DE",900877,1954));
		states.add(new State("Florida","FL",18900773,53927));
		states.add(new State("Georgia","GA",9727566,57906));
		states.add(new State("Hawaii","HI",1366862,6423));
		states.add(new State("Idaho","ID",1573499,82747));
		states.add(new State("Illinois","IL",12864380,55584));
		states.add(new State("Indiana","IN",6501582,35867));
		states.add(new State("Iowa","IA",3053787,55869));
		states.add(new State("Kansas","KS",2863813,81815));
		states.add(new State("Kentucky","KY",4350606,39728));
		states.add(new State("Louisiana","LA",4553962,43562));
		states.add(new State("Maine","ME",1333074,30862));
		states.add(new State("Maryland","MD",5789929,9774));
		states.add(new State("Massachusetts","MA",6559644,7840));
		states.add(new State("Michigan","MI",9911626,56804));
		states.add(new State("Minnesota","MN",5314879,79610));
		states.add(new State("Mississippi","MS",2978240,46907));
		states.add(new State("Missouri","MO",6011478,68886));
		states.add(new State("Montana","MT",994416,145552));
		states.add(new State("Nebraska","NE",1831825,76872));
		states.add(new State("Nevada","NV",2709432,109826));
		states.add(new State("New Hampshire","NH",1321445,8968));
		states.add(new State("New Jersey","NJ",8807501,7417));
		states.add(new State("New Mexico","NM",2067273,121356));
		states.add(new State("New York","NY",19421055,47214));
		states.add(new State("North Carolina","NC",9565781,48711));
		states.add(new State("North Dakota","ND",675905,68976));
		states.add(new State("Ohio","OH",11568495,40948));
		states.add(new State("Oklahoma","OK",3764882,68667));
		states.add(new State("Oregon","OR",3848606,95997));
		states.add(new State("Pennsylvania","PA",12734905,44817));
		states.add(new State("Rhode Island","RI",1055247,1045));
		states.add(new State("South Carolina","SC",4645975,30109));
		states.add(new State("South Dakota","SD",819761,75885));
		states.add(new State("Tennessee","TN",6375431,41217));
		states.add(new State("Texas","TX",25268418,261797));
		states.add(new State("Utah","UT",2770765,82144));
		states.add(new State("Vermont","VT",630337,9250));
		states.add(new State("Virginia","VA",8037736,39594));
		states.add(new State("Washington","WA",6753369,66544));
		states.add(new State("West Virginia","WV",1859815,24078));
		states.add(new State("Wisconsin","WI",5698230,54310));
		states.add(new State("Wyoming","WY",568300,97100));
		this.setLayout(new BorderLayout());
		statePanel=new Panel(new GridLayout(0,1));
		for (State s:states)
			statePanel.add(s.getPanel());
		sp = new JScrollPane(statePanel);
		this.add(sp, BorderLayout.CENTER);

		setUpButtons();
	}
	public void setUpButtons()
	{
		 name = new JRadioButton("Sort by Name");
	     abbr = new JRadioButton("Sort by Abbr.");
	     pop = new JRadioButton("Sort by Pop");
	     area = new JRadioButton("Sort by Area");
	    name.setSelected(true);
	    ButtonGroup group = new ButtonGroup();
	    group.add(name);
	    group.add(abbr);
	    group.add(pop);
	    group.add(area);
	    Panel sortTypePanel=new Panel(new GridLayout(0,1));

	    sortTypePanel.add(name);
	    sortTypePanel.add(abbr);
	    sortTypePanel.add(pop);
	    sortTypePanel.add(area);
	    Panel eastPanel=new Panel(new GridLayout(0,1));
	    eastPanel.add(sortTypePanel);
	    this.add(eastPanel, BorderLayout.SOUTH);
	    sortButton = new Button("Sort");
		sortButton.addActionListener(this);
		Panel buttonPanel = new Panel(new FlowLayout());
		buttonPanel.add(sortButton);
		eastPanel.add(buttonPanel);

		 ascending = new JRadioButton("Acending");
	     descending = new JRadioButton("Descending");
	    ascending.setSelected(true);
	    ButtonGroup group2 = new ButtonGroup();
	    group2.add(ascending);
	    group2.add(descending);
	    Panel sortOrderPanel=new Panel(new FlowLayout());
	    sortOrderPanel.add(ascending);
	    sortOrderPanel.add(descending);
	    eastPanel.add(sortOrderPanel);
	    eastPanel.setBackground(new Color(255,237,185));
	    this.add(eastPanel, BorderLayout.EAST);
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource()==sortButton){
			if (name.isSelected()){
				if(ascending.isSelected()){
					Collections.sort(states);
				}else{
					Comparator<State> rev = Collections.reverseOrder();
					Collections.sort(states, rev);
				}
			}
			if(abbr.isSelected()){
				if(ascending.isSelected()){
					Collections.sort(states,State.AbbrComparator);
				}else{
					Comparator<State> rev = Collections.reverseOrder(State.AbbrComparator);
					Collections.sort(states, rev);
				}
			}
			if(pop.isSelected()){
				Comparator<State> popComp =new Comparator<State>(){
					public int compare(State s1, State s2){
						return s1.getPop()-s2.getPop();
					}
				};
				if(ascending.isSelected()){

					Collections.sort(states, popComp);
				}else{
					Comparator<State> rev = Collections.reverseOrder(popComp);
					Collections.sort(states, rev);
				}
			}
			if(area.isSelected()){
				if(ascending.isSelected()){
					Collections.sort(states,new AreaComparator());
				}else{
					Comparator<State> rev= Collections.reverseOrder(new AreaComparator());
					Collections.sort(states, rev);
				}
			}
			statePanel=new Panel(new GridLayout(0,1));
			for (State s:states)
				statePanel.add(s.getPanel());
			this.remove(sp);
			sp = new JScrollPane(statePanel);
			this.add(sp, BorderLayout.CENTER);
			this.validate();
		}

	}
}