guis part 4

21
GUIs Part 4 CS221 – 4/17/09

Upload: star

Post on 04-Feb-2016

46 views

Category:

Documents


0 download

DESCRIPTION

GUIs Part 4. CS221 – 4/17/09. Professional Assignments. Assignment # 2 – Download and install Visual Studio 2008 Professional Trial Software http://www.microsoft.com/downloads/details.aspx?FamilyID=83c3a1ec-ed72-4a79-8961-25635db0192b&displaylang=en - PowerPoint PPT Presentation

TRANSCRIPT

Testing and Debugging

GUIs Part 4CS221 4/17/09

Professional AssignmentsAssignment #2 Download and install Visual Studio 2008 Professional Trial Softwarehttp://www.microsoft.com/downloads/details.aspx?FamilyID=83c3a1ec-ed72-4a79-8961-25635db0192b&displaylang=en

Assignment #3 Implement any one of your sorting algorithms using C# instead of Java

To get credit for both assignments:Send me your working C# project in emailIf it compiles and works youll get full credit

What well cover todayQuick ReviewJava Class LibraryPackagesMore SWING Programming

Java Class LibraryJava includes over 3000 classes for your use!Almost entirely written in Java

These classes help with a variety of common tasksManaging lists of objectsString parsingGUI programming

You access through the use of classes grouped by packages

PackagesClasses are grouped together with packages

Why packages?Provides hierarchical organization to classesSolves the problem of class name collision

All java packages start with java or javax3rd party developers (like you) can create other package namesExample PackagesJava.lang fundamental classes to the language, e.g. string, process, thread, etcJava.io data streams, serialization and file systemJava.net support for building networking applicationsJava.awt basic GUI supportJavax.swing built on awt adds more GUI featuresJava.sql access to SQL databases

Many More

7Using PackagesImport statement tells the compiler you want to use a classE.g.: import java.awt.** symbol indicates you want to import all sub-packages within the java.awt package.

java.lang is special. Every java program automatically imports this package.Using PackagesYou can reference a class without the importE.g. new java.awt.Color(0,0,0);

Not recommended, using import is better for:ReadabilityMakes it easy to understand what classes your program depends upon

Under what condition would you want to reference a class without an import statement?Java.awt.listJava.util.list9Class MethodsConstructorUsed to manufacture new instances of a classInstanceAssociated with a specific instance of an objectObject must be instantiated (created) for an instance method to be calledStaticApplied to a class as a whole, rather than to an instanceCan be called without instantiating the class

ExamplesConstructorDeclaration:public Math()Usage:Math math = new Math();InstanceDeclaration: public int abs(int x)Usage: Math math = new Math();math.abs(-5);StaticDeclaration:public static int abs(int x)Usage:Math.abs(-5);

ConstantsConstants are variables whose values cannot changeMany classes define constantsThese are declared as:Static, value is associated with the classFinal, value cannot change

For example the Color class definesstatic final Color BLACK

ConstantsUse class defined constants to improve readabilitysetFillColor(Color.RED)area = Math.PI * radius * radius

How to declare your own constantsUse all caps PI, BLACK, RED, etc.Use underscores if necessaryNUMBER_OF_MONTHSAssign value at declarationpublic static final float PI = (float) 22/7;

Variable and Method Access

Back to SWINGLets turn this:

Into this:

Lets see how to do this

ProblemsHow to draw freely on the screen?Extend JComponentpaintComponent(Graphics g)

How to randomly place a circle in our componentMath.random()getWidth, getHeightLets make it better

ProblemsHow to capture mouse events?Implements MouseListeneraddMouseListenermousePressed(MouseEvent e)Think AboutHow would we:Keep drawing circles as long as the mouse is clicked?Allow the user to select a circle and drag it somewhere else?

What can we use to figure it out?