(c)2009 by E.S. Boese. All Rights Reserved. Classes Chapter 13 – Lecture Slides 1 Got class?

Slides:



Advertisements
Similar presentations
Swing! Components and Images. Example Swing Components (also called “widgets”) Graphic from sun.com.
Advertisements

Computer Science 209 Images and GUIs. Working with Java Colors The class java.awt.Color includes constants, such as Color.red, for some commonly used.
Graphics You draw on a Graphics object The Graphics object cannot directly be created by your code, instead one is generated when the method paintComponent.
F27SB2 Programming Languages
Problem Solving 6 GUIs and Event Handling ICS-201 Introduction to Computing II Semester 071.
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
CMSC 341 Building Java GUIs. 09/26/2007 CMSC 341 GUI 2 Why Java GUI Development? Course is about Data Structures, not GUIs. We are giving you the opportunity.
Computer Science 209 Graphics and GUIs. Working with Color The class java.awt.Color includes constants for typical color values and also supports the.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 20, 2005.
1 Simple Phone Applet Lab Mobile Phone Display Area Send, Menu and End Numbers 0-9 * and #
18-Jun-15 Applets. 2 An applet is a program that is typically embedded in a Web page and can be run from a browser You need special HTML in the Web page.
Swing Components III Chapter 12 - Student JFrame, Component Methods.
28-Jun-15 Applets. 2 An applet is a program that is typically embedded in a Web page and can be run from a browser You need special HTML in the Web page.
COMP 14 Introduction to Programming Miguel A. Otaduy June 7, 2004.
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Unit 11 Object-oriented programming: Graphical user interface Jin Sa.
Web Design & Development Lecture 18. Java Graphics.
Chapter 9: Applets Jim Burns Fall Outline Learn about applets Learn about applets Write an HTML doc to host an applet Write an HTML doc to host.
Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class.
Java Programming, 3e Concepts and Techniques Chapter 3 Section 65 – Manipulating Data Using Methods – Java Applet.
(c) 2008 E.S.Boese All Rights Reserved. Threads and Media Chapter 8 - Lecture Slides 1.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
CIS 068 Welcome to CIS 083 ! Introduction to GUIs: JAVA Swing.
Layout Management Containers can arrange their components. Our container is a JPanel, and it can set the way it’s components will be laid out : mypanel.setLayout.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
CSE 219 Computer Science III Graphical User Interface.
CSE 219 Computer Science III Images. HW1 Has been posted on Blackboard Making a Game of Life with limited options.
GUI programming Graphical user interface-based programming.
Adding Graphics to a Frame Application Applets: Can generate drawings by overriding paint Frame: Do not draw directly on a frame. Draw graphics on a JPanel.
(c) by Elizabeth Sugar Boese.1 Chapter 12 Events on Components - Lecture Slides.
J McQuillan SE204: 2004/2005: Lecture 4slide 1 The Graphics Class Used when we need to draw to the screen Two graphics classes –Graphics –Graphics2D.
Canvas and Graphics CS 21a. 9/26/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L17: Canvas.
Field Trip #19 Animations with Java By Keith Lynn.
MSc Workshop - © S. Kamin, U. ReddyLect 3 - GUI -1 Lecture 3 - Graphical User Interfaces r GUI toolkits in Java API r JFrame r GUI components.
Swing GUI Components You can create graphics components to place on your applet using classes available in the Swing package ( javax.swing ) Class names.
MSc Workshop - © S. Kamin, U.Reddy Lect 4 - Events - 1 Lecture 4 – Event Handling r Painting r Event types r Catching different event types.
Swing Differences between Swing and AWT Naming Conventions All Swing components begin with a capital J -- JPanel, JButton, JScrollBar, JApplet, etc..
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Review of Graphics in Java,
Layout Managers Arranges and lays out the GUI components on a container.
1 GUIs, Layout, Drawing Rick Mercer. 2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs)
Applets and Frames. Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI Slide 2 Applets Usually.
University of Limerick1 Software Architecture Java Layout Managers.
Java Programming Working with Sound and Images. Topics Learn about the paint() and repaint() methods Learn about paintComponent() method Use the drawString()
CompSci Event Handling. CompSci Event Handling The Plan  Sequential (Single Thread) Model  Event Model  Making the GUI interactive  Examples.
Introduction to GUI in 1 Graphical User Interface 2 Nouf Almunyif.
1 GUIs, Layout, Drawing Rick Mercer. 2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs)
Field Trip #23 Hangman By Keith Lynn. JApplet A JApplet is a top-level container An applet is a small Java program that is executed by another program.
Applets Java code is compiled into byte code instead of machine language –Languages like C, C++, Pascal and others are compiled into machine language so.
(c) by Elizabeth Sugar Boese.1 Chapter 6 Events - Lecture Slides.
Variables and Methods Chapter 3 – Lecture Slides.
Variables and Methods Chapter 3 – Lecture Slides 1(c) 2008 by E.S.Boese. All Rights Reserved. Variables vary... After all, change is the status quo.
Basics of GUI Programming Chapter 11 and Chapter 22.
Creating a Window. A basic window in Java is represented by an object of the class Window in the package java.awt.
Ajmer Singh PGT(IP) JAVA IDE Programming - I. Ajmer Singh PGT(IP) GUI (Graphical User Interface) It is an interface that uses a graphic entities along.
Graphical User Interface (GUI) Two-Dimensional Graphical Shapes.
Java Layouts CSIS 3701: Advanced Object Oriented Programming.
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Applets. 9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L12: Applets Slide 2 Applets Usually.
GUI.1 Graphical User Interfaces GUIs. GUI.2 The Plan Components Flat Layouts Hierarchical Layouts Designing a GUI Coding a GUI.
Introduction Many Java application use a graphical user interface or GUI (pronounced “gooey”). A GUI is a graphical window or windows that provide interaction.
Graphical User Interfaces
INFSY 547: WEB-Based Technologies
Computer Science 209 Graphics and GUIs.
Graphical User Interface (pronounced "gooey")
Chapter 13: Advanced GUIs and Graphics
Steps to Creating a GUI Interface
Computer Science 209 Images and GUIs.
Advanced GUIs and Graphics
Graphical User Interface
Presentation transcript:

(c)2009 by E.S. Boese. All Rights Reserved. Classes Chapter 13 – Lecture Slides 1 Got class?

Classes  Separate files to divide up code  Sets up a template for each ‘object’  Create encapsulated ‘objects’  Instantiate multiple objects from one class Helper classes – classes that do not extend JApplet (c)2009 by E.S. Boese. All Rights Reserved. 2

JPANEL

(c) by Elizabeth Sugar Boese 4 JPanel Extend the JPanel class to create new components Can use for  Adding components  Drawing (e.g. drawString, Smiley ) Good for creating your own components, such as  Thermometer  Dial  Fancy button

(c) by Elizabeth Sugar Boese 5 JPanel Create a separate class that extends JPanel, then add this new class to your applet import java.awt.*; import javax.swing.*; public class Smiley extends JPanel { } Add a method paintComponent where you can use your drawing methods from chapter 2 import java.awt.*; import javax.swing.*; public class Smiley extends JPanel { public void paintComponent ( Graphics gr ) { super.paintComponent( gr ); }

(c) by Elizabeth Sugar Boese 6 JPanel Add the Smiley class in our applet  Create a Smiley object: Smiley smiles; smiles = new Smiley( ); Call the method setPreferredSize( new Dimension( width, height ) ) on our Smiley object. If we do not call this method, the default width and height is zero. So even if we add it to the applet, we wouldn't be able to see it because the size is zero! smiles.setPreferredSize( new Dimension(300, 300) ); Lastly, we can add it to the applet. add( smiles );

(c) by Elizabeth Sugar Boese 7 JPanel import java.awt.*; import javax.swing.*; public class SmileyApplet extends JApplet { Smiley smiles; JButton button; JTextField tf; public void init( ) { // create Smiley object smiles = new Smiley( ); // MUST set the bounds otherwise you won't see it! // default is width=0 height=0!!! smiles.setPreferredSize( new Dimension( 300,300 ) ); // create button and textfield button = new JButton( "go!" ); tf = new JTextField(10); // add to applet setLayout( new FlowLayout( ) ); add( button ); add( smiles ); add( tf ); } How would you customize your Smiley so that you can specify the color of the face?

(c) by Elizabeth Sugar Boese 8 Lecture Exercise: Modify the Smiley class to make it easy to create the following applet:

BACKGROUND IMAGE

(c) by Elizabeth Sugar Boese 10 Background Image Create a second class " ImgPanel " which extends JPanel and paints the Image in the paintComponent method Then use this ImgPanel as you would normally use JPanel in other classes. Add components to it, and the components will appear on top of the image panel. Written such that it will center the image within the panel

(c) by Elizabeth Sugar Boese 11 ImgPanel import java.awt.*; import javax.swing.*; public class ImgPanel extends JPanel { Image img; public ImgPanel( Image i ) { img = i; } public void paintComponent(Graphics g) { super.paintComponent(g); if ( img != null ) { int imgWidth = img.getWidth(this); // find the width of the image int panelWidth = this.getWidth( ); // find the width of the panel int x = (panelWidth - imgWidth ) / 2; // calculate x to center the img int imgHeight = img.getHeight( this ); // find height of image int panelHeight = this.getHeight( ); // find height of panel int y = (panelHeight - imgHeight ) / 2; // calculate y to center the img g.drawImage(img,x,y,img.getWidth(this),img.getHeight(this),this); }

(c) by Elizabeth Sugar Boese 12 Using ImgPanel in an Applet import java.awt.*; import javax.swing.*; public class ImgBackground extends JApplet { JLabel myplay, name; Image img; ImgPanel ipanel; public void init( ) { myplay = new JLabel( "My Fun Playground" ); name = new JLabel( "Java Rules" ); myplay.setForeground( Color.YELLOW ); name.setForeground( Color.RED ); img = getImage( getCodeBase( ), “Nepal.png" ); ipanel = new ImgPanel( img ); myplay.setFont( new Font( "Serif", Font.BOLD, 20 ) ); ipanel.setLayout( new GridLayout( 2,1 ) ); ipanel.add( myplay ); ipanel.add( name ); add( ipanel ); }

AddressFields Example import java.awt.*; import javax.swing.*; public class AddressFields extends JPanel { // instance variables JLabel name, street, city, state, zip; JTextField tf_name, tf_street, tf_city, tf_state, tf_zip; public AddressFields( ) // constructor { // initialize the instance variables name = new JLabel( "Name:", JLabel.RIGHT ); street = new JLabel( "Street:", JLabel.RIGHT ); city = new JLabel( "City:", JLabel.RIGHT ); state = new JLabel( "State:", JLabel.RIGHT ); zip = new JLabel( "Zip:", JLabel.RIGHT ); tf_name = new JTextField( 20 ); tf_street = new JTextField( 20 ); tf_city = new JTextField( 20 ); tf_state = new JTextField( 20 ); tf_zip = new JTextField( 20 ); // add to the panel setLayout( new GridLayout(5, 2) ); add( name ); add( tf_name ); add( street ); add( tf_street ); add( city ); add( tf_city ); add( state ); add( tf_state ); add( zip ); add( tf_zip ); } (c) by Elizabeth Sugar Boese 13

AddressFields Example import java.awt.*; import javax.swing.*; public class AddressApplet extends JApplet { JLabel homeAddress, billingAddress; AddressFields home, billing; public void init( ) { homeAddress = new JLabel( "Home Address" ); billingAddress = new JLabel( "Billing Address" ); home = new AddressFields( ); billing = new AddressFields( ); setLayout(new BoxLayout( getContentPane( ), BoxLayout.Y_AXIS )); add( homeAddress ); add( home ); add( billingAddress ); add( billing ); } (c) by Elizabeth Sugar Boese 14

Fancy Button Example import java.awt.*; import javax.swing.*; public class FancyButton extends JButton { public FancyButton( Image img, String text ) { setIcon( new ImageIcon(img) ); setText( text ); setHorizontalTextPosition( JButton.CENTER ); setForeground( Color.white ); setBorderPainted( false ); setContentAreaFilled( false ); setFocusable( false ); } (c) by Elizabeth Sugar Boese 15 import java.awt.*; import javax.swing.*; public class FancyButtonApplet extends JApplet { Image imge; FancyButton home, portfolio, contact; public void init( ) { setLayout( new FlowLayout( ) ); imge = getImage( getCodeBase( ), "buttonGreen.png" ); home = new FancyButton( imge, "Home" ); portfolio = new FancyButton( imge, "Portfolio" ); contact = new FancyButton( imge, "About Us" ); add( home ); add( portfolio ); add( contact ); } Add events on the buttons in the applet class, just like you would do for a normal JButton

Classes with Events public void itemStateChanged(ItemEvent ie) { Object src = ie.getSource(); if (src == rb_eyesBlue) player.setEyeColor(Color.BLUE); else if (src == rb_eyesGreen) player.setEyeColor(Color.GREEN); else if (src == dl_shirtColor) { String selection = (String)dl_shirtColor.getSelectedItem(); if (selection.equals("Red")) player.setShirtColor(Color.RED); else if (selection.equals("Yellow")) player.setShirtColor(Color.YELLOW); } (c)2009 by E.S. Boese. All Rights Reserved. 16 public class Player extends JPanel {... public Player(Color eye, Color sh) { eyes = eye; shirt = sh; } public void setShirtColor(Color s) { shirt = s; repaint(); } public void setEyeColor(Color e) { eyes = e; repaint(); }

Classes, Threads, Events Sometimes you need to pass an instance of the JApplet class to another class to call methods such as getImage and getAudioClip public class SlidesApplet extends JApplet { Slides myslideshow; public void init() { myslideshow = new Slides(); myslideshow.init(this);... } (c)2009 by E.S. Boese. All Rights Reserved. 17 public class Slides extends JPanel implements Runnable {... public void init(JApplet app) { photos = new Image[4]; // load the images into memory photos[0] = app.getImage(app.getCodeBase(), "Australia.jpg"); photos[1] = app.getImage(app.getCodeBase(), "Budapest.jpg"); photos[2] = app.getImage(app.getCodeBase(), "Galapagos.jpg"); photos[3] = app.getImage(app.getCodeBase(), "Nepal.jpg");

(c) by Elizabeth Sugar Boese 18 Summary JPanel Custom fonts Background Image Fancy buttons Incorporating events, threads