lec8 - COMP201 Java Programming Part II GUI Programming...

Info icon This preview shows pages 1–11. Sign up to view the full content.

View Full Document Right Arrow Icon
COMP201 Java Programming Part II: GUI Programming Topic 8: Basics of Graphics Programming Reading: Chapter 7
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
COMP201 Topic 8 / Slide 2 Outline Introduction: Ingredients of Swing GUI Creating a frame (window) Displaying information in a panel Displaying texts 2D shapes Colors and fonts Images
Image of page 2
COMP201 Topic 8 / Slide 3 Introduction: Ingredients of Swing GUI Top-level container: JFrame (Window) in this case Menu bar (optional) contentPane : contains all visible components Intermediate containers to organize various GUI components: JPanel s in this case JPanel3 JPanel1 JPanel2 Build GUI with javax.Swing, JDK1.2 Better than java.awt, JDK1.1
Image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
COMP201 Topic 8 / Slide 4 To create a swing GUI 1. Create a top-level container: JFrame 1. Get contentPane of the top-level container 1. Create JPanel s and add GUI components to the JPanel s 1. Layout the JPanel s onto the contentPane . JPanel s can contain Atomic components such as JButton s and Jlabel s (Topic 10) Custom graphics such as text, shapes, and images (This Topic) Introduction: Ingredients of Swing GUI
Image of page 4
COMP201 Topic 8 / Slide 5 Creating a Frame Frame: top-level window, not contained inside another window. Use JFrame class in javax.swing package. What can you do with JFrame : - Create a new one - get/setSize - get/setLocation - get/setTitle - show/hide - toFront/toBack - is/setResizable - dispose - setIconImage
Image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
COMP201 Topic 8 / Slide 6 Creating a Frame Most methods for working with JFrame inherited from superclasses: Object JFrame Component Container Window Frame JPanel JComponent Ancestor of all GUI objects
Image of page 6
COMP201 Topic 8 / Slide 7 Creating a Frame java.awt.Component: getLocation, setBounds, setLocation, getSize, setSize, setBackground, setForeground, repaint, …… java.awt.Window: toFront, toBack, show, hide, …… java.awt.Frame: dispose, setResizable, setTitle, setIcomImage, ……
Image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
COMP201 Topic 8 / Slide 8 Creating a Frame Example: class SimpleFrame extends JFrame { public SimpleFrame() { setSize (WIDTH, HEIGHT); } public static final int WIDTH = 300; public static final int HEIGHT = 200; } Default constructor of JFrame is called. Default size of a JFrame : 0x0
Image of page 8
COMP201 Topic 8 / Slide 9 Creating a Frame Things to know about coordinates Units are expressed pixels (depends on resolution) Coordinate system is vertically-flipped from Cartesian (0,0) is upper left corner of screen Frame defaults to location (0,0) and size (0,0)
Image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
COMP201 Topic 8 / Slide 10 Creating a Frame import javax.swing.*; public class SimpleFrameTest { public static void main( String [] args) { SimpleFrame frame = new SimpleFrame (); frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); frame.show(); } } // SimpleFrameTest.java Create a SimpleFrame.
Image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern