06 intro eclipse, applets, graphics

Upload: kaykay19

Post on 07-Apr-2018

224 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/4/2019 06 Intro Eclipse, Applets, Graphics

    1/68

    Carnegie Mellon

    Topics in Software Engineering

    Lynn Robert Carter

    2008-05-12

    Copyright 2008, Lynn Robert Carter

    Classes: Fields, Methods, and Constructors

  • 8/4/2019 06 Intro Eclipse, Applets, Graphics

    2/68

    Carnegie Mellon Topics in Software EngineeringJava: Introduction to Eclipse, Applets, and Graphics2

    Agenda

    Introduction to Eclipse

    Applets

    Graphics

  • 8/4/2019 06 Intro Eclipse, Applets, Graphics

    3/68

    Carnegie Mellon Topics in Software EngineeringJava: Introduction to Eclipse, Applets, and Graphics

    Starting Eclipse

  • 8/4/2019 06 Intro Eclipse, Applets, Graphics

    4/68

    Carnegie Mellon Topics in Software EngineeringJava: Introduction to Eclipse, Applets, and Graphics

    Selecting a workspace

  • 8/4/2019 06 Intro Eclipse, Applets, Graphics

    5/68

    Carnegie Mellon Topics in Software EngineeringJava: Introduction to Eclipse, Applets, and Graphics

    The Welcome Screen-1

  • 8/4/2019 06 Intro Eclipse, Applets, Graphics

    6/68

    Carnegie Mellon Topics in Software EngineeringJava: Introduction to Eclipse, Applets, and Graphics

    The Welcome Screen-2

  • 8/4/2019 06 Intro Eclipse, Applets, Graphics

    7/68Carnegie Mellon Topics in Software EngineeringJava: Introduction to Eclipse, Applets, and Graphics

    The workbench

  • 8/4/2019 06 Intro Eclipse, Applets, Graphics

    8/68Carnegie Mellon Topics in Software EngineeringJava: Introduction to Eclipse, Applets, and Graphics

    Creating a new Java Project-1

  • 8/4/2019 06 Intro Eclipse, Applets, Graphics

    9/68Carnegie Mellon Topics in Software EngineeringJava: Introduction to Eclipse, Applets, and Graphics

    Creating a new Java Project-2

  • 8/4/2019 06 Intro Eclipse, Applets, Graphics

    10/68

    Carnegie Mellon Topics in Software EngineeringJava: Introduction to Eclipse, Applets, and Graphics

    The Create a Java project wizard-1

  • 8/4/2019 06 Intro Eclipse, Applets, Graphics

    11/68

    Carnegie Mellon Topics in Software EngineeringJava: Introduction to Eclipse, Applets, and Graphics

    The Create a Java project wizard-2

  • 8/4/2019 06 Intro Eclipse, Applets, Graphics

    12/68

    Carnegie Mellon Topics in Software EngineeringJava: Introduction to Eclipse, Applets, and Graphics

    The updated workbench-1

  • 8/4/2019 06 Intro Eclipse, Applets, Graphics

    13/68

    Carnegie Mellon Topics in Software EngineeringJava: Introduction to Eclipse, Applets, and Graphics

    The updated workbench-2

  • 8/4/2019 06 Intro Eclipse, Applets, Graphics

    14/68

    Carnegie Mellon Topics in Software EngineeringJava: Introduction to Eclipse, Applets, and Graphics

    Expanding the project folders

  • 8/4/2019 06 Intro Eclipse, Applets, Graphics

    15/68

    Carnegie Mellon Topics in Software EngineeringJava: Introduction to Eclipse, Applets, and Graphics

    Select the src folder

  • 8/4/2019 06 Intro Eclipse, Applets, Graphics

    16/68

    Carnegie Mellon Topics in Software EngineeringJava: Introduction to Eclipse, Applets, and Graphics

    Create a Package-1

  • 8/4/2019 06 Intro Eclipse, Applets, Graphics

    17/68

    Carnegie Mellon Topics in Software EngineeringJava: Introduction to Eclipse, Applets, and Graphics

    Create a Package-2

  • 8/4/2019 06 Intro Eclipse, Applets, Graphics

    18/68

    Carnegie Mellon Topics in Software EngineeringJava: Introduction to Eclipse, Applets, and Graphics

    The New Java Package wizard-1

  • 8/4/2019 06 Intro Eclipse, Applets, Graphics

    19/68

    Carnegie Mellon Topics in Software EngineeringJava: Introduction to Eclipse, Applets, and Graphics

    The New Java Package wizard-2

  • 8/4/2019 06 Intro Eclipse, Applets, Graphics

    20/68

    Carnegie Mellon Topics in Software EngineeringJava: Introduction to Eclipse, Applets, and Graphics

    The updated workbench

  • 8/4/2019 06 Intro Eclipse, Applets, Graphics

    21/68

    Carnegie Mellon Topics in Software EngineeringJava: Introduction to Eclipse, Applets, and Graphics

    Create a new class-1

  • 8/4/2019 06 Intro Eclipse, Applets, Graphics

    22/68

    Carnegie Mellon Topics in Software EngineeringJava: Introduction to Eclipse, Applets, and Graphics

    Create a new class-2

  • 8/4/2019 06 Intro Eclipse, Applets, Graphics

    23/68

    Carnegie Mellon Topics in Software EngineeringJava: Introduction to Eclipse, Applets, and Graphics

    The New Java Class wizard-1

  • 8/4/2019 06 Intro Eclipse, Applets, Graphics

    24/68

    Carnegie Mellon Topics in Software EngineeringJava: Introduction to Eclipse, Applets, and Graphics

    The New Java Class wizard-2

  • 8/4/2019 06 Intro Eclipse, Applets, Graphics

    25/68

    Carnegie Mellon Topics in Software EngineeringJava: Introduction to Eclipse, Applets, and Graphics

    The New Java Class wizard-3

  • 8/4/2019 06 Intro Eclipse, Applets, Graphics

    26/68

    Carnegie Mellon Topics in Software EngineeringJava: Introduction to Eclipse, Applets, and Graphics

    The updated workbench-1

  • 8/4/2019 06 Intro Eclipse, Applets, Graphics

    27/68

    Carnegie Mellon Topics in Software EngineeringJava: Introduction to Eclipse, Applets, and Graphics

    The updated workbench-2

  • 8/4/2019 06 Intro Eclipse, Applets, Graphics

    28/68

    Carnegie Mellon Topics in Software EngineeringJava: Introduction to Eclipse, Applets, and Graphics

    The updated workbench-3

  • 8/4/2019 06 Intro Eclipse, Applets, Graphics

    29/68

    Carnegie Mellon Topics in Software EngineeringJava: Introduction to Eclipse, Applets, and Graphics

    Removing the TODO task tag

  • 8/4/2019 06 Intro Eclipse, Applets, Graphics

    30/68

    Carnegie MellonTopics in Software Engineering

    Java: Introduction to Eclipse, Applets, and Graphics

    Entering the Java program-1

  • 8/4/2019 06 Intro Eclipse, Applets, Graphics

    31/68

    Carnegie MellonTopics in Software Engineering

    Java: Introduction to Eclipse, Applets, and Graphics

    Entering the Java program-2

  • 8/4/2019 06 Intro Eclipse, Applets, Graphics

    32/68

    Carnegie MellonTopics in Software Engineering

    Java: Introduction to Eclipse, Applets, and Graphics

    Save the edit changes

  • 8/4/2019 06 Intro Eclipse, Applets, Graphics

    33/68

    Carnegie MellonTopics in Software Engineering

    Java: Introduction to Eclipse, Applets, and Graphics

    Running the program

  • 8/4/2019 06 Intro Eclipse, Applets, Graphics

    34/68

    Carnegie MellonTopics in Software Engineering

    Java: Introduction to Eclipse, Applets, and Graphics

    The results

  • 8/4/2019 06 Intro Eclipse, Applets, Graphics

    35/68

    Carnegie MellonTopics in Software Engineering

    Java: Introduction to Eclipse, Applets, and Graphics35

    Agenda

    Introduction to Eclipse

    Applets

    Graphics

  • 8/4/2019 06 Intro Eclipse, Applets, Graphics

    36/68

    Carnegie MellonTopics in Software Engineering

    Java: Introduction to Eclipse, Applets, and Graphics

    Introduction to Applets-1

    An applet is a small application that resides withinsome other parent application

    Web browsers are common parent applications for applets

    Applets allow web pages to be dynamic and interactive

    Eclipse supports the development of both applicationsand applets with a process that starts out exactly thesame

    1. Create a new Java Project and give it a helpful name

    2. Within the Java Project folder, find the src folder

    3. Create a properly named Java Package within the src folder

  • 8/4/2019 06 Intro Eclipse, Applets, Graphics

    37/68

    Carnegie MellonTopics in Software Engineering

    Java: Introduction to Eclipse, Applets, and Graphics

    Introduction to Applets-2

    Unlike an application, an Applet does not have amain method, so do not ask Eclipse to create onefor you when you create the Class within the justcreated Java Package

    The class that roots the Applet must extend theJApplet class and must contain a method namedpaint

  • 8/4/2019 06 Intro Eclipse, Applets, Graphics

    38/68

    Carnegie MellonTopics in Software Engineering

    Java: Introduction to Eclipse, Applets, and Graphics

    The default class Applet

  • 8/4/2019 06 Intro Eclipse, Applets, Graphics

    39/68

    Carnegie MellonTopics in Software Engineering

    Java: Introduction to Eclipse, Applets, and Graphics

    Enhancing the default class

  • 8/4/2019 06 Intro Eclipse, Applets, Graphics

    40/68

    Carnegie MellonTopics in Software Engineering

    Java: Introduction to Eclipse, Applets, and Graphics

    Alternatives suggested by Eclipse

  • 8/4/2019 06 Intro Eclipse, Applets, Graphics

    41/68

    Carnegie MellonTopics in Software Engineering

    Java: Introduction to Eclipse, Applets, and Graphics

    Adding the import statement

  • 8/4/2019 06 Intro Eclipse, Applets, Graphics

    42/68

    Carnegie MellonTopics in Software Engineering

    Java: Introduction to Eclipse, Applets, and Graphics

    The cause of the warning

  • 8/4/2019 06 Intro Eclipse, Applets, Graphics

    43/68

    Carnegie MellonTopics in Software Engineering

    Java: Introduction to Eclipse, Applets, and Graphics

    Changing the warning settings

    43

    Right click on the

    package icon

    Select the Properties item

    from the pop-up menu

  • 8/4/2019 06 Intro Eclipse, Applets, Graphics

    44/68

    Carnegie MellonTopics in Software Engineering

    Java: Introduction to Eclipse, Applets, and Graphics

    Changing the default properties-1

  • 8/4/2019 06 Intro Eclipse, Applets, Graphics

    45/68

    Carnegie MellonTopics in Software Engineering

    Java: Introduction to Eclipse, Applets, and Graphics

    Changing the default properties-2

  • 8/4/2019 06 Intro Eclipse, Applets, Graphics

    46/68

    Carnegie MellonTopics in Software Engineering

    Java: Introduction to Eclipse, Applets, and Graphics

    Changing the default properties-3

  • 8/4/2019 06 Intro Eclipse, Applets, Graphics

    47/68

    Carnegie MellonTopics in Software Engineering

    Java: Introduction to Eclipse, Applets, and Graphics

    Changing the default properties-4

  • 8/4/2019 06 Intro Eclipse, Applets, Graphics

    48/68

    Carnegie MellonTopics in Software Engineering

    Java: Introduction to Eclipse, Applets, and Graphics

    Changing the default properties-5

  • 8/4/2019 06 Intro Eclipse, Applets, Graphics

    49/68

    Carnegie MellonTopics in Software Engineering

    Java: Introduction to Eclipse, Applets, and Graphics

    Changing the default properties-6

  • 8/4/2019 06 Intro Eclipse, Applets, Graphics

    50/68

    Carnegie MellonTopics in Software Engineering

    Java: Introduction to Eclipse, Applets, and Graphics

    Changing the default properties-7

  • 8/4/2019 06 Intro Eclipse, Applets, Graphics

    51/68

    Carnegie MellonTopics in Software Engineering

    Java: Introduction to Eclipse, Applets, and Graphics

    Creating the application

  • 8/4/2019 06 Intro Eclipse, Applets, Graphics

    52/68

    Carnegie MellonTopics in Software Engineering

    Java: Introduction to Eclipse, Applets, and Graphics

    Running the Applet

  • 8/4/2019 06 Intro Eclipse, Applets, Graphics

    53/68

    Carnegie MellonTopics in Software Engineering

    Java: Introduction to Eclipse, Applets, and Graphics

    The resulting Applet window-1

  • 8/4/2019 06 Intro Eclipse, Applets, Graphics

    54/68

    Carnegie MellonTopics in Software Engineering

    Java: Introduction to Eclipse, Applets, and Graphics

    The resulting Applet window-2

  • 8/4/2019 06 Intro Eclipse, Applets, Graphics

    55/68

    Carnegie MellonTopics in Software Engineering

    Java: Introduction to Eclipse, Applets, and Graphics55

    Agenda

    Introduction to Eclipse

    Applets

    Graphics

  • 8/4/2019 06 Intro Eclipse, Applets, Graphics

    56/68

    Carnegie MellonTopics in Software Engineering

    Java: Introduction to Eclipse, Applets, and Graphics

    Introduction to graphics-1

    Unlike most languages before it, support forgraphics has been a part of Java from nearly its

    beginning

    Graphics is supported by means of two Class

    libraries

    The Abstract Windowing Toolkit (AWT)

    Swing

  • 8/4/2019 06 Intro Eclipse, Applets, Graphics

    57/68

    Carnegie MellonTopics in Software Engineering

    Java: Introduction to Eclipse, Applets, and Graphics

    Introduction to graphics-2

    AWT was first and it provided support for Windows and allowing the user to change the size/shape

    Layout management (to support repositioning and changingthe size an shape of components within a window, when

    the size and shape of the window changes, so theinformation in the window is still useful)

    Widgets to support interactions between the program and ausers (widgets facilitated graphics input/output)

    Swing came later and added a moresophisticated layer to the AWT and enhancedJava graphics programs

  • 8/4/2019 06 Intro Eclipse, Applets, Graphics

    58/68

    Carnegie MellonTopics in Software Engineering

    Java: Introduction to Eclipse, Applets, and Graphics

    The Abstract Windowing Toolkit

    Set of user interface components

    Event-handling routines for currently popularwindow and point and click user interfaces

    Tools for drawing and displaying images

    Facilities for drawing shapes and using colors

    Routines for handling characters (fonts, styles, etc.)

    Layout managers for dealing with user changes to thesize and shape of the window

  • 8/4/2019 06 Intro Eclipse, Applets, Graphics

    59/68

    Carnegie MellonTopics in Software Engineering

    Java: Introduction to Eclipse, Applets, and Graphics

    Swing

    Focused on platform independence

    Designed to be extensible

    Component-based framework

    Designed to be customizable

    Lightweight user-interface that does not try to use thetools of the underlying hosts operating system

    Loosely-coupled architecture (patterned after themodel / view/ controller architecture model)

  • 8/4/2019 06 Intro Eclipse, Applets, Graphics

    60/68

    Carnegie MellonTopics in Software Engineering

    Java: Introduction to Eclipse, Applets, and Graphics

    Graphics within Applets-1

    Graphics can be done from within bothApplets and regular Java applications

    Setting up regular applications to do graphics

    requires quite a great deal of glue code and

    so we will defer that until a later lecture

  • 8/4/2019 06 Intro Eclipse, Applets, Graphics

    61/68

    Carnegie MellonTopics in Software Engineering

    Java: Introduction to Eclipse, Applets, and Graphics

    Graphics within Applets-2

    Applets depend upon an Applet Viewer program or aweb browser to support graphics This interface means that none of the glue code required by regular

    applications is needed

    Once the glue code has been provided, all of the code within an Applet

    can be used in regular applications, so learning how to do graphics inApplets is transferable to applications

    The paint() method is the heart of an graphicsprogram

    When a window must be drawn, the paint() methodis called to perform that operation

  • 8/4/2019 06 Intro Eclipse, Applets, Graphics

    62/68

    Carnegie MellonTopics in Software Engineering

    Java: Introduction to Eclipse, Applets, and Graphics

    The implementation process-1

    Creating graphics programs is similar to givingspecific instructions to someone who is doing

    work for you

    Start with a blank piece of paper, a canvas

    Design the solution

    Transform the design into code

  • 8/4/2019 06 Intro Eclipse, Applets, Graphics

    63/68

    Carnegie MellonTopics in Software Engineering

    Java: Introduction to Eclipse, Applets, and Graphics

    The implementation process-2

    Design the solution What components need to be placed onto the canvas?

    Where should the components be placed?

    What size should the components be?

    Select colors, line sizes, character fonts, character styles asrequired to create a useful design

    Transform the design to code

    Determine the code required to produce each component

    Determine the location of this component on the canvasusing the coordinate system of the canvas

    Determine the other parameters that the design requires

  • 8/4/2019 06 Intro Eclipse, Applets, Graphics

    64/68

    Carnegie MellonTopics in Software Engineering

    Java: Introduction to Eclipse, Applets, and Graphics

    The graphics canvas

  • 8/4/2019 06 Intro Eclipse, Applets, Graphics

    65/68

    Carnegie MellonTopics in Software Engineering

    Java: Introduction to Eclipse, Applets, and Graphics

    The graphics components

    Output components lines

    shapes (rectangles, ovals, arcs, polygons)

    strings

    images

    Input components buttons (standalone, radio buttons, check box) sliders

    text field

    text area

    menus

    combo boxes

    Structuring components windows, frames, view ports, layout managers

    lists, trees

  • 8/4/2019 06 Intro Eclipse, Applets, Graphics

    66/68

    Carnegie MellonTopics in Software Engineering

    Java: Introduction to Eclipse, Applets, and Graphics

    A small graphics Appletimportjavax.swing.JApplet;

    importjava.awt.Color;importjava.awt.Graphics;

    public class Mainline extends JApplet {

    public void paint(Graphics g){super.paint( g );g.drawRect(50, 50, 100, 100);g.drawRect(75, 75, 100, 100);g.setColor(Color.BLUE);g.fillRect(100, 50, 100, 100);g.setColor(Color.BLACK);g.fillRect(125, 75, 100, 100);

    }}

  • 8/4/2019 06 Intro Eclipse, Applets, Graphics

    67/68

    Carnegie MellonTopics in Software Engineering

    Java: Introduction to Eclipse, Applets, and Graphics

    The results of running the Applet

  • 8/4/2019 06 Intro Eclipse, Applets, Graphics

    68/68

    Agenda

    Introduction to Eclipse

    Applets

    Graphics