Address Book

This is an object-oriented window forms program using C#. It is a simple address book that allows you to add manage and delete contacts.

[code lang=”csharp”]
///——————————————————
/// Cynthia Chadwick
/// C# Final
/// 12/08/2009
///
/// Program allows a user to add, delete, and save
/// contacts to a .mcl file that can be loaded.
///——————————————————
using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CynthiaChadwickFinalExam
{
public partial class CS233SFinal : Form
{
// List of contacts
public List<Contact> ContactList = new List<Contact>();
public const string FileName = "Contacts.mcl"; // name of file

public CS233SFinal()
{
InitializeComponent();
}

/*****************************************************************
* opens newContact form and creates a new contact
******************************************************************/
private void NewButton_Click(object sender, EventArgs e)
{
NewContact newContact = new NewContact();
newContact.NewContactUpdated +=
new NewContact.NewContactHandler(NewContact_ButtonClick);
newContact.Show();
}

/*****************************************************************
* takes the new contact and adds it to the list
******************************************************************/
private void NewContact_ButtonClick(object sender, Contact contact)
{
AddContact(contact);
}

/*****************************************************************
* deletes selected contact
******************************************************************/
private void DeleteButton_Click(object sender, EventArgs e)
{
int index = 0;

try
{
index = Convert.ToInt32(ContactsTreeView.SelectedNode.Name);
}
catch
{
ContactInfoTextBox.Text = "";
WarningException warning = new WarningException
("failed to parse index for tree view item. Index string:");
return;
}
ContactList.RemoveAt(index);
ContactsTreeView.SelectedNode.Remove();
}

/*****************************************************************
* save button saves to file
******************************************************************/
private void SaveButton_Click(object sender, EventArgs e)
{
Save();
}

/*****************************************************************
* saves contact list to overwriten file
******************************************************************/
private void Save()
{
using (TextWriter SaveContacts = new StreamWriter(FileName))
{
for (int i = 0; i < ContactList.Count; i++)
{
Contact SaveContact = ContactList[i];

string NewContact = string.Format("{0},{1},{2},{3},{4},{5},{6},{7};",
SaveContact.LastName, SaveContact.FirstName, SaveContact.Address,
SaveContact.City, SaveContact.State, SaveContact.ZipCode,
SaveContact.HomePhone, SaveContact.CellPhone);

SaveContacts.WriteLine(NewContact);
}
}
}

/*****************************************************************
* cleans the program and loads the file and fills the treeview
* and contact list
******************************************************************/
private void LoadButton_Click(object sender, EventArgs e)
{
Clear(); // clears the program
TreeViewFile(FileName);
// loads the file and fills the treeview and contact list
}

/*****************************************************************
* clears the contact list and the treeview
******************************************************************/
private void Clear()
{
ContactList.Clear(); // clears the contact list of values
for (int i = 0; i < ContactsTreeView.Nodes.Count; i++) {
ContactsTreeView.Nodes[i].Nodes.Clear(); // clears the tree view of values
}
}
public string[] Contacts; public string[] ViewContacts;
/*****************************************************************
* reads info from the file and splits it into contact strings
******************************************************************/
private void TreeViewFile(string file)
{
try
{
// open textreader
using (TextReader ContactFile = new StreamReader(file))
{
// read file and split it into contact
strings Contacts = ContactFile.ReadToEnd().Split(‘;’);
foreach (string str in Contacts)
{
// skip the lines that only contain white space
string Str = str.Trim();
if (Str != "")
{
// parse the line and add to contact AddContact(ParseContact(Str));
}
}
}
}
catch { return; }
}

/*****************************************************************
* adds contact to treeview and to list
******************************************************************/
private void AddContact(Contact contact)
{
try
{
ContactList.Add(contact);
char FirstLetter = Convert.ToChar(Convert.ToString (contact.LastName[0]).ToUpper());
if (FirstLetter >= ‘A’ && FirstLetter <= ‘Z’)
{
ContactsTreeView.Nodes[FirstLetter – ‘A’].Nodes.Add(
(ContactList.Count – 1).ToString(),
string.Format("{0}, {1}", contact.LastName,
contact.FirstName));
}
}
catch
{
return;
}
}

/*****************************************************************
* parses out the contact string
******************************************************************/
private Contact ParseContact(string info)
{
Contact contact = new Contact();

string[] parts = info.Split(‘,’);

contact.LastName = parts[0].Trim();
contact.FirstName = parts[1].Trim();
contact.Address = parts[2].Trim();
contact.City = parts[3].Trim();
contact.State = parts[4].Trim();
contact.ZipCode = parts[5].Trim();
contact.HomePhone = parts[6].Trim();
contact.CellPhone = parts[7].Trim();

return contact;
}

/*****************************************************************
* shows the contact info in the info window when
* you select a contact
******************************************************************/
private void ContactsTreeView_AfterSelect(object sender, TreeViewEventArgs e)
{
int index = 0;
try
{
index = Convert.ToInt32(e.Node.Name);
}
catch
{
ContactInfoTextBox.Text = "";
return;
}

ContactInfoTextBox.Text = string.Format(
"{0}, {1}" + Environment.NewLine +
"{2}" + Environment.NewLine +
"{3}, {4} {5}" + Environment.NewLine +
"Home Phone: {6,13}" + Environment.NewLine +
"Cell Phone: {7,13}", ContactList[index].LastName, ContactList[index].FirstName,
ContactList[index].Address, ContactList[index].City,
ContactList[index].State, ContactList[index].ZipCode,
ContactList[index].HomePhone, ContactList[index].CellPhone);
}
}
}
[/code]

Car Crash

This is a silly little Java app that I wrote for a Java class. It is two beetles (the car) that I drew myself using simple shapes driving around till they crash with a big explosion.

(Chadwick is my maiden name)

[code lang=”java”]
/**************************************************************************
* Name: Cynthia Chadwick FileName: Chadwick_Lab6/MainPanel.java
* CS 162 – Fall 2010 Lab #6 – Project 8.7 – pg 478
* Mon, Wed, 9:00am – 10:20am
* Date: 11/8/2010
*
* Program Name: Car Crash
* Program Description: Two cars move across the screen, when the first car
* reaches the end it turns around and crashes into
* the second car, resulting in a cartoon bang.
* Output – A pretty picture with animated cars and animated clouds
* Input – No outside input, a timer that runs the animation
* Process – MainPanel
* MainPanel : Constructor
* Creates Car objects : ArrayList<Car> Cars
* Creates Cloud objects : ArrayList<Cloud> Clouds
* Create and start timers
* paintComponent : method
* paints drawBackground : method
* paints Cars
* paints checkExplosion : method
* checkExplosion : method
* checks to see if the cars have collided
* if true drawExplosion
* drawExplosion : method
* fills a polygon
* draws a string
* draw a polygon
* drawBackground : method
* draws the background
* draws the clouds
* Listeners for cars and clouds
**************************************************************************/
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.ArrayList;
import javax.swing.*;
public class MainPanel extends JPanel
{
private Timer timerCarA, timerCarB, timeClouds;
private ArrayList<Car> Cars;
private Graphics2D g;
private ArrayList<Cloud> Clouds;
/**********************************************************************
* MainPanel Constructor
* Create Cars, Clouds, and set timers
**********************************************************************/
public MainPanel()
{
Cars = new ArrayList<Car>();
Cars.add(new Car(0, false, Color.blue));
Cars.add(new Car(-190, false, Color.red));

Clouds = new ArrayList<Cloud>();
Clouds.add(new Cloud(425, 50, 10, 10));
Clouds.add(new Cloud(750, 100, 20, 10));
Clouds.add(new Cloud(150, 200, 10, 5));

timeClouds = new Timer(45, new RepaintClouds());
timerCarA = new Timer(15, new RepaintCarA());
timerCarB = new Timer(45, new RepaintCarB());
timeClouds.start();
timerCarA.start();
timerCarB.start();
}
/**********************************************************************
* paintComponent paints everything to the screen
**********************************************************************/
public void paintComponent(Graphics page)
{
super.paintComponents(page);
g = (Graphics2D) page;
g.setBackground(Color.cyan);

drawBackground();

Cars.get(0).draw(g);
Cars.get(1).draw(g);

checkExplosion();
}
/**********************************************************************
* Check if there was explosion
**********************************************************************/
private void checkExplosion()
{
if(Cars.get(0).getRight() && !Cars.get(1).getRight())
{
if(Cars.get(0).getX() == Cars.get(1).getX()+180)
{
timerCarA.stop();
timerCarB.stop();
drawExplosion(Cars.get(0).getX(), Cars.get(0).getY());
}
}
else if(Cars.get(1).getRight() && !Cars.get(0).getRight())
{
if(Cars.get(1).getX() == Cars.get(0).getX()+180)
{
timerCarA.stop();
timerCarB.stop();
drawExplosion(Cars.get(1).getX(), Cars.get(1).getY());
}
}
else if(Cars.get(1).getRight() && Cars.get(0).getRight())
{
if(Cars.get(0).getX()+180 == Cars.get(1).getX())
{
timerCarA.stop();
timerCarB.stop();
drawExplosion(Cars.get(0).getX(), Cars.get(0).getY());
}
}
else
{
if(Cars.get(0).getX() == Cars.get(1).getX()+180)
{
timerCarA.stop();
timerCarB.stop();
drawExplosion(Cars.get(0).getX(), Cars.get(0).getY());
}
}
}
/**********************************************************************
* Draw an explosion
**********************************************************************/
private void drawExplosion(int x, int y)
{
Polygon p = new Polygon();
p.addPoint(x-50, y-350);
p.addPoint(x-50, y-250);
p.addPoint(x-200, y-350);
p.addPoint(x-150, y-200);
p.addPoint(x-250, y-250);
p.addPoint(x-150, y-100);
p.addPoint(x-300, y-50);
p.addPoint(x-150, y);
p.addPoint(x-250, y+100);
p.addPoint(x-150, y+50);
p.addPoint(x-250, y+250);
p.addPoint(x-150, y+150);
p.addPoint(x-150, y+300);
p.addPoint(x-50, y+200);
p.addPoint(x, y+300);
p.addPoint(x+50, y+150);
p.addPoint(x+150, y+250);
p.addPoint(x+100, y+50);
p.addPoint(x+250, y+150);
p.addPoint(x+200, y);
p.addPoint(x+300, y-50);
p.addPoint(x+250, y-100);
p.addPoint(x+300, y-200);
p.addPoint(x+150, y-100);
p.addPoint(x+200, y-300);
p.addPoint(x+100, y-150);
p.addPoint(x+100, y-350);
p.addPoint(x+50, y-200);
p.addPoint(x-50, y-350);

g.setColor(Color.white);
g.fill(p);

g.setColor(Color.black);
Font font = new Font("Serif", Font.BOLD, 96);
g.setFont(font);
g.drawString("Bang!", x-100, y+25);
g.draw(p);
}
/**********************************************************************
* Draw the background
**********************************************************************/
private void drawBackground()
{
Shape sky = new Rectangle2D.Float(0, 0, 800, 600);
g.setColor(Color.blue);
g.fill(sky);

Shape grass = new Arc2D.Float(-150, 300, 450, 600, 0, 180, Arc2D.CHORD);
Shape grassA = new Arc2D.Float(100, 300, 500, 600, 0, 180, Arc2D.CHORD);
Shape grassB = new Arc2D.Float(350, 300, 700, 600, 0, 180, Arc2D.CHORD);
g.setColor(Color.green);
g.fill(grass);
g.fill(grassA);
g.fill(grassB);

Shape road = new Rectangle2D.Float(0, 400, 800, 100);
g.setColor(Color.gray);
g.fill(road);

Shape roadLineWhiteA = new Rectangle2D.Float(0, 405, 800, 2);
Shape roadLineWhiteB = new Rectangle2D.Float(0, 493, 800, 2);
g.setColor(Color.white);
g.fill(roadLineWhiteA);
g.fill(roadLineWhiteB);
Shape roadLineYellowA = new Rectangle2D.Float(-1, 445, 802, 5);
g.setColor(Color.yellow);
g.draw(roadLineYellowA);

Clouds.get(0).draw(g);
Clouds.get(1).draw(g);
Clouds.get(2).draw(g);
}
/**********************************************************************
* Listener to move Car A
**********************************************************************/
class RepaintCarA implements ActionListener
{
public void actionPerformed(ActionEvent arg0)
{
if(Cars.get(0).getRight())
{
Cars.get(0).setX(Cars.get(0).getX()-1);
Cars.get(0).addCount(1);
} else {
Cars.get(0).setX(Cars.get(0).getX()+1);
Cars.get(0).addCount(-1);
}

repaint();
}
}
/**********************************************************************
* Listener to move Car B
**********************************************************************/
class RepaintCarB implements ActionListener
{
public void actionPerformed(ActionEvent arg0)
{
if(Cars.get(1).getRight())
{
Cars.get(1).setX(Cars.get(1).getX()-1);
Cars.get(1).addCount(1);
} else {
Cars.get(1).setX(Cars.get(1).getX()+1);
Cars.get(1).addCount(-1);
}
repaint();
}
}
/**********************************************************************
* Listener to move the Clouds
**********************************************************************/
class RepaintClouds implements ActionListener
{
public void actionPerformed(ActionEvent arg0)
{
for(int a=0; a < Clouds.size(); a++)
{
if(Clouds.get(a).getX() == -200)
{
Clouds.get(a).setX(900);
} else {
Clouds.get(a).setX(Clouds.get(a).getX()-1);
}
}
repaint();
}
}
}
[/code]