Java Concepts Chapter 2 - PowerPoint PPT Presentation

Loading...

PPT – Java Concepts Chapter 2 PowerPoint presentation | free to download - id: 6ee59a-YmMxY



Loading


The Adobe Flash plugin is needed to view this content

Get the plugin now

View by Category
About This Presentation
Title:

Java Concepts Chapter 2

Description:

Java Concepts Chapter 2 Graphical Applications Ms. Pack AP Computer Science A Graphical Applications and Frame Windows Objective Overview of drawing graphical ... – PowerPoint PPT presentation

Number of Views:42
Avg rating:3.0/5.0
Slides: 12
Provided by: WendyB172
Learn more at: http://teacherpress.ocps.net
Category:

less

Write a Comment
User Comments (0)
Transcript and Presenter's Notes

Title: Java Concepts Chapter 2


1
Java ConceptsChapter 2 Graphical Applications
Ms. Pack AP Computer Science A
2
Graphical Applicationsand Frame Windows
  • Objective
  • Overview of drawing graphical applications
  • You will be able to draw basic graphics in a
    frame window (more attractive than a console
    window)
  • Construct a frame of a certain size
  • Construct a component (such as a rectangles,
    circles, or a combination)
  • Add the component to the frame
  • Make the frame visible

3
Graphical Applicationsand Frame Windows
  • Constructing a Frame
  • Construct an object of the JFrame class
  • JFrame frame new JFrame()
  • Set the size of the frame
  • frame.setSize(300, 400)
  • //Frame will be 300 pixels wide and 400 pixels
    tall
  • //Omitting this step will make the frame 0x0
  • Set the title of the frame (optional)
  • frame.setTitle("Graphical Application")
  • Set the "default close operation".
  • frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS
    E)
  • //When the user closes the frame, the program
    will end
  • Make the frame visible
  • frame.setVisible(true)

4
Graphical Applicationsand Frame Windows
  • Creating a Rectangle Component Object
  • In order to draw something in the frame, you must
    first create a component object and then add it
    to the frame
  • A component can consist of multiple graphics
    objects
  • Here are some of the classes that need to be
    imported in order to create a component object
    that draws a rectangle
  • import java.awt.Graphics //primitive class
    stores graphics state //including the
    current color, font, etc.
  • import java.awt.Graphics2D //Class used to
    draw shapes
  • import java.awt.Rectangle //Rectangles class
  • import javax.swing.JComponent //Used to draw in
    a frame

5
Graphical Applicationsand Frame Windows
  • Example of a Rectangle Component Object Class
  • (used for creating, drawing, and positioning
    Rectangle objects)
  • import java.awt.Graphics //Allows you to
    manipulate the graphics
  • //state (color, etc.)
  • import java.awt.Graphics2D //Contains methods
    to draw shapes
  • import java.awt.Rectangle
  • import javax.swing.JComponent
  • public class RectangleComponent extends
    JComponent
  • //Place all the drawing instructions inside
    paintComponent method
  • public void paintComponent(Graphics g)
  • Graphics2D g2 (Graphics2D) g //Cast to
    recover Graphics2D
  • Rectangle box new Rectangle(5, 10, 20, 30)
    //Construct rectangle
  • g2.draw(box) //Draw rectangle
  • box.translate(15, 25) //Move rectangle 15
    units right and 25 down
  • g2.draw(box) //Draw rectangle again

6
Graphical Applicationsand Frame Windows
  • Drawing a Rectangle Component
  • Construct a frame (see previous slide)
  • Construct an object of your component class
  • RectangleComponent component new
    RectangleComponent()
  • Add the component to the frame
  • frame.add(component)
  • Make the frame visible (see previous slide)

7
Graphical Applicationsand Frame Windows
  • Write a Client Program to View the Rectangles
  • (used for instantiating the RectangleComponent
    object and adding it to the frame)
  • import javax.swing.JFrame
  • public class RectangleViewer
  • public static void main(String args)
  • JFrame frame new JFrame()
  • frame.setSize(300, 400)
  • frame.setTitle("Graphics Application")
  • frame.setDefaultCloseOperation(JFrame.EXIT_ON_CL
    OSE)
  • RectangleComponent component new
    RectangleComponent()
  • frame.add(component)
  • frame.setVisible(true)

8
Graphical Applicationsand Frame Windows
  • Drawing a Face using Basic Shapes
  • You must first construct a "face" component and
    then add it to the frame
  • The face component will consist of different
    types of graphical objects for the head, eyes,
    and mouth
  • Here are some of the classes that need to be
    imported in order to use graphics
  • import java.awt.Color //color class
  • import java.awt.Graphics //primitive graphics
    class
  • import java.awt.Graphics2D//Contains methods to
    draw shapes
  • import java.awt.Rectangle //Rectangles class
  • Import java.awt.geom.Ellipse2D
  • Import java.awt.geom.Line2D
  • Import javax.swing.JPanel
  • import javax.swing.JComponent

9
Graphical Applicationsand Frame Windows
  • Write a Face Component Class
  • (used for creating, drawing, and positioning a
    face)
  • import java.awt.Color //color class
  • import java.awt.Graphics //primitive graphics
    class
  • import java.awt.Graphics2D //Extends Graphics
    class
  • import java.awt.Rectangle //Rectangles class
  • import java.awt.geom.Ellipse2D //Ellipse and
    circle class
  • import java.awt.geom.Line2D //Line class
  • import javax.swing.JPanel
  • import javax.swing.JComponent
  • public class FaceComponent extends JComponent
  • //Place drawing instructions inside this method
  • public void paintComponent(Graphics g)
  • Graphics2D g2 (Graphics2D) g //Make
    graphics 2D
  • Ellipse2D.Double head new Ellipse2D.Double(5,
    10, 100, 150) //Construct head (x, y, width,
    height)

10
Graphical Applicationsand Frame Windows
  • Write a Client Program to View the Face
  • (used for creating the FaceComponent object and
    adding it to the frame)
  • import javax.swing.JFrame
  • public class FaceViewer
  • public static void main(String args)
  • JFrame frame new JFrame() //Instantiate new
    frame
  • frame.setSize(300, 400) //Set frame size
  • frame.setTitle("Alien Face") //Set frame title
  • frame.setDefaultCloseOperation(JFrame.EXIT_ON_CL
    OSE)
  • FaceComponent component new FaceComponent()
    //Instantiate component
  • frame.add(component) //Add component to frame
  • frame.setVisible(true) //Set frame visible

11
Graphical Applicationsand Frame Windows
  • Classwork/Homework Assignment
  • Download the following classes from my website
  • FaceComponent Face drawing object
  • FaceViewer View the face
  • Use these classes as a starting point for your
    component class and client program to draw it in
    a frame
  • Refer to Java Concepts 2.11-2.13 for drawing
    other graphical objects (ellipse, lines,
    rectangles, etc.)
  • You can also refer to the link on my website
    named Drawing Geometric Shapes
  • Use your imagination and draw an object of your
    choice
  • Remember to include your name (JohnDoe) at the
    end of all classes you create, so I know who
    created them
  • Once these are finished, we will show them to the
    class
About PowerShow.com