Graphical User Interfaces GUIs in Java - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

Graphical User Interfaces GUIs in Java

Description:

Create a Jar in the WaterGameGUI class. Jar jar = new Jar(); //an instance field ... You cannot find it directly, so infer it from the height of a jar. ... – PowerPoint PPT presentation

Number of Views:83
Avg rating:3.0/5.0
Slides: 26
Provided by: classesEn
Category:

less

Transcript and Presenter's Notes

Title: Graphical User Interfaces GUIs in Java


1
Graphical User Interfaces (GUIs) in Java
  • CS161 Recitation
  • May 11, 2006

2
The Water Game
3
Create a Driver
  • 1. Create a Java Project called WaterGame.
  • 2. Create the classes
  • a. Proj5 - contains main method
  • b. WaterGameGUI
  • c. Jar
  • 3. In Proj5 write
  • public class Proj5
  • public static void main(String args)
  • WaterGameGUI frame new WaterGameGUI()
  • frame.setVisible(true)

4
Creating the Frame
  • Create the Frame first. To do this, tell
    WaterGameGUI to extend JFrame
  • import java.awt. //for button
    listeners, etc.
  • import javax.swing. //for using J-stuff
  • public class WaterGameGUI extends JFrame

5
Creating the Frame
  • import java.awt. //for button
    listeners, etc.
  • import javax.swing. //for using
    J-stuff
  • public class WaterGameGUI extends JFrame
  • private final int FRAMEWIDTH 500,
    FRAMEHEIGHT 500
  • public WaterGameGUI()
  • this.setLayout(null)
  • this.setSize( FRAMEWIDTH, FRAMEHEIGHT )
  • this.setLocation(10,10)

6
Adding a JPanel
  • Add the instance field
  • private JPanel panel new JPanel()
  • Add this to the WaterGameGUI constructor
  • panel.setSize(400,400)
  • panel.setLocation(30,30)
  • panel.setVisible(true)
  • panel.setLayout(null)
  • panel.setBackground(Color.WHITE)
  • this.add(panel)
  • If youre done, change your code so that the
  • panel size and location are relative to the
  • frame size.

7
Creating a JComponent
  • Your Jar class will be a child class of
    JComponent
  • public class Jar extends JComponent
  • Are there any errors? Can you fix them?

8
Creating a JComponent
  • Your Jar class will be a child class of
    JComponent
  • import javax.swing.JComponent //fixed error
  • public class Jar extends JComponent
  • Are there any errors? Can you fix them?

You must import JComponent in order to use it.
9
Creating a JComponent
  • JComponents must have a paintComponent() method
    to know how to display themselves in your GUI.
    This method is called automatically. Do NOT call
    this method yourself. The method header must look
    exactly like this.
  • public void paintComponent(Graphics g)

10
Creating a JComponent
  • Lets first try painting a rectangle
  • 1. Choose a paint color
  • 2. Draw the rectangle
  • public void paintComponent(Graphics g)
  • int width getWidth()int height
    getHeight()
  • g.setColor(Color.RED) //step 1
  • g.fillRect(0,0,width,height) //step 2
  • Any errors?

11
Adding a JComponent
  • Create a Jar in the WaterGameGUI class
  • Jar jar new Jar() //an instance field
  • In the WaterGameGUI constructor
  • jar.setSize(50,50) //set width and height
  • jar.setLocation(20,20) //set upper left corner
    location
  • jar.setVisible(true)
  • panel.add(jar) //add jar to panel
  • Bored? Change the int parameters in setSize and
    setLocation to see what happens.

12
Onto Real Jars
  • What properties should a Jar have?

13
Properties of a Jar
  • What properties should a Jar have?

contents 8
capacity 3
color Color.BLUE
14
Properties of a Jar
  • Properties of an object are stored as instance
    fields
  • public class Jar extends JComponent
  • private int capacity
  • Can you write the other two?

contents 8
capacity 3
color Color.BLUE
15
Jar Constructor
  • What kind of constructor should you create for
    your Jar class?
  • Recall you will need to set the instance fields
    to create
  • An empty jar of 3 unit capacity
  • An empty jar of 5 unit capacity
  • A full jar of 8 unit capacity

16
What Should a Jar Do?
17
What Should a Jar Do?
  • 1. Report how much water it contains (getter
    method)

18
What Should a Jar Do?
  • 1. Report how much water it contains (getter
    method)
  • 2. Add to its water

19
What Should a Jar Do?
  • 1. Report how much water it contains (getter
    method)
  • 2. Add to its water
  • 3. Subtract from its water
  • 4. Reports its capacity, get/set color, etc.
  • Each item represents a method your Jar class
    should have.

20
Drawing Real Jars
  • Empty jars are white rectangles.
  • Modify paintComponent() to draw a white
    rectangle.
  • You may need to change
  • your panel color to see
  • your jar.

21
Drawing Real Jars
  • Add another jar to WaterGameGUI.
  • Remember the five steps to make it appear.
  • Set the locations of
  • your jars so that they
  • do not overlap!
  • Later, change the positions
  • of your jars to look
  • like the applet.

22
Drawing Real Jars
  • Resize your jars to represent their capacitities
  • In WaterGameGUI(), create a variable
  • int unitHeight 20 //pixels per unit of
    water
  • Set the size of each jar to match its capacity
  • jar3.setSize(jarWidth, 3unitHeight)
  • //enter an int for jarWidth

23
Drawing Lines on Jars
  • Draw lines on your jars to show their capacities
  • In paintComponent() find the unitHeight set in
    WaterGameGUI. You cannot find it directly, so
    infer it from the height of a jar.
  • unitHeight jarHeight / capacity
  • How do you find the jar height?
  • How to you find the capacity?

24
Drawing Lines on Jars
Draw lines on jars to show their capacity
  • 2. Set the paint color to black.
  • 3. Use the drawLine() method to draw a single
    line on each jar.
  • 4. Use a loop to draw evenly-space lines on each
    jar.

y-position unitHeight
y-position 2unitHeight
25
Drawing Water in Jars
  • Jar water is just another rectangle
  • Over the white rectangle
  • Under the black lines
  • The height of the blue water
  • rectangle is
  • waterHeight
  • unitHeightcontents

y 0
y 3unitHeight
y 5unitHeight
rectHeight 2unitHeight
Write a Comment
User Comments (0)
About PowerShow.com