1 GUIs, Layout, Drawing Rick Mercer. 2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs)

Slides:



Advertisements
Similar presentations
2D Graphics Drawing Things. Graphics In your GUI, you might want to draw graphics E.g. draw lines, circles, shapes, draw strings etc The Graphics class.
Advertisements

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.
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.
Corresponds with Chapter 12
GUI and Swing, part 2 The illustrated edition. Scroll bars As we have previously seen, a JTextArea has a fixed size, but the amount of text that can be.
Java Swing Recitation – 11/(20,21)/2008 CS 180 Department of Computer Science, Purdue University.
Event Driven Programming and GUIs Part 3 CS221 – 4/15/09.
Slides prepared by Rose Williams, Binghamton University Chapter 17 Swing I.
Graphics Programming. In this class, we will cover: The difference between AWT and Swing Creating a frame Frame positioning Displaying information in.
IEEM 110 Computing in Industrial Applications Basic User Interface in Java.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.
Unit 11 Object-oriented programming: Graphical user interface Jin Sa.
Web Design & Development Lecture 18. Java Graphics.
1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
CSE 219 Computer Science III Graphical User Interface.
Introduction to GUI in Java 1. Graphical User Interface Java is equipped with many powerful,easy to use GUI component such as input and output dialog.
CSE 219 Computer Science III Images. HW1 Has been posted on Blackboard Making a Game of Life with limited options.
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
Java GUI building with Swing. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
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.
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.
Lesson 34: Layering Images with Java GUI. The FlowLayout RECAP.
Field Trip #19 Animations with Java By Keith Lynn.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
Object Oriented programming Instructor: Dr. Essam H. Houssein.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
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.
GUI Basics. What is GUI? A graphical user interface (GUI) is a type of user interface item that allows people to interact with programs in more ways than.
Creating Windows. How can we use Java to create programs that use windows (GUI applications)? How can we use Java to create programs that use windows.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
Introduction to GUI in 1 Graphical User Interface 2 Nouf Almunyif.
Introduction to Java Chapter 9 - Graphical User Interfaces and Applets1 Chapter 9 Graphical User Interfaces and Applets.
Computer Science 209 GUIs Model/View/Controller Layouts.
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.
Java Swing One of the most important features of Java is its ability to draw graphics.
1 Event Driven Programs with a Graphical User Interface Rick Mercer.
Graphical User Interface (GUI) Two-Dimensional Graphical Shapes.
GUI Tutorial 2. What did we do last time?  Basic flow  instance variables, set up in ctor, close operation, size, visible  JFrame  Event-driven 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.
Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.
Graphical User Interface (GUI)
Event Handler Methods Text field Object Responder JAVA AWT Environment: Messages are sent between JAVA Objects Screen Event Notification Press Button.
Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
Getting Started with GUI Programming Chapter 10 CSCI 1302.
Basic Graphics 03/03/16 & 03/07/16 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
C Sc 335 Object-Oriented Programming and Design Rick Mercer
Java Visual Applications CSIS 3701: Advanced Object Oriented Programming.
GUI.1 Graphical User Interfaces GUIs. GUI.2 The Plan Components Flat Layouts Hierarchical Layouts Designing a GUI Coding a GUI.
GUI Programming in Java Hao Jiang Boston College April, 2009.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
GUIs and Events Rick Mercer.
Object-Orientated Analysis, Design and Programming
Graphical User Interfaces
Java Graphics.
Swing JComponents.
Modern Programming Language Java
Java Swing.
Basic Graphics Chapter 5 3/19/15 Thursday Section Only
Ellen Walker Hiram College
Graphical User Interface
Presentation transcript:

1 GUIs, Layout, Drawing Rick Mercer

2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs) to respond to user desires

3 A Few Graphical Components  A Graphical User Interface (GUI) presents a graphical view of an application to users.  To build a GUI application, you must: — Have a well-tested model that is independent of the view — Make graphical components visible to the user — Ensure the correct things happen for each event user clicks button, moves mouse, presses enter key,...  Let's first consider some of Java's GUI components: — windows, buttons, and text fields

4 Classes in the swing package  The javax.swing package has components that show in a graphical manner JFrame : window with title, border, menu, buttons JButton : A component that can "clicked" JLabel : A display area for a small amount of text JTextField : Allows editing of a single line of text

5 Get a window to show itself import javax.swing.JFrame; public class ShowSomeLayouts extends JFrame { public static void main(String[] args) { // Construct an object that has all the methods of JFrame JFrame aWindow = new ShowSomeLayouts(); aWindow.setVisible(true); } // Set up the GUI public FirstGUI() { // Make sure the program terminates when window closes this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // … more to come … }

6 Some JFrame messages  Set the size and locations of the window with setSize(400, 200); setLocation(200, 200); — The first int is the width of the window in pixels — the second int is the height of the window in pixels

7 Building components  So far we have an empty window  Let us add a button, a label, and an editable line  First construct three graphical components JButton clickMeButton = new JButton("Nobody is listening to me"); JLabel aLabel = new JLabel("Button above, text field below"); JTextField textEditor = new JTextField("You can edit this text ");  Next, add these objects to a JFrame

8 Add components to a window  Could use the default BorderLayout and add components to one of the five areas of a JFrame add(clickMeButton, BorderLayout.NORTH); add(textEditor, BorderLayout.CENTER); add(aLabel, BorderLayout.SOUTH);

9 The 5 areas of BorderLayout  By default, JFrame objects have only five places where you can add components — a 2nd add wipes out the 1 st  There are many layout managerslayout managers

10 FlowLayout  You can change the default layout strategy with a setLayout message setSize(600, 200); setLocation(200, 200); setLayout(new FlowLayout()); // Change layout Strategy add(clickMeButton); add(textEditor); add(aLabel);

11 GridLayout  Use this for evenly spaced layouts public GridLayout(int rows, int cols, int hgap, int vgap) setLayout(new GridLayout(2, 2, 4, 4)); add(clickMeButton); add(textEditor); add(aLabel); add (new JButton("Fourth component"));

12 JPanel Objects  Layout is made much easier using Jpanels — A JPanel can hold several things and be treated as one element to add to the Jframe JPanel buttonPanel = new JPanel(); // Default layout for JPanels is FlowLayout buttonPanel.add(new JButton("Add")); buttonPanel.add(new JButton("Remove")); buttonPanel.add(new JButton("Quit")); add(buttonPanel); // Add to the 4 th GridLayout spot

13 Null Layout LL

14 Drawing with a Graphics Object  The use of graphics is common among modern software systems  Java has strong support for graphics — coordinate system for Java graphics — drawing shapes such as lines, ovals, rectangles,... — basic animation techniques — the use of color — the use of fonts — drawing images — 3D rendering

15 The Coordinate System  A simple two-dimensional coordinate system exists for each graphics context or drawing surface  Each point on the coordinate system represents 1 pixel  top left corner of the area is coordinate // This string will be drawn 20 pixels right, // 40 pixels down as the lower left corner; // other shapes are upper right g2.drawString("is in Panel1", 20, 40);  A drawing surface has a width and height  Anything drawn outside of that area is not visible

16 The Coordinate System x y

17 Draw on a JPanel  Need to extend a class that extends JComponent — JPanel is good  To draw things: — extend JPanel — override paintComponent — panel surface is transparent: send drawing messages inside paintComponent to the graphic context Use an improved Graphics2D object ( g2 )

18 Put something in a JPanel  Create a JPanel class that draws a few strings import java.awt.*; public class DrawingPanel extends javax.swing.JPanel { // Override the paintComponent method in public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D)g; // Use improved Graphics2D g2.drawString("Draw in the graphics context g2", 20, 20); g2.drawString("that is in a instance of JPanel", 20, 40); g2.drawString("which will be added to a JFrame", 20, 60); }

19 The Graphics Object  paintComponent 's Graphics g argument represents a "graphical context" object. — You can tell it to draw things on the panel — If you want another method to draw, pass the Graphics object to it—it like a canvas that understands draws  The actual object passed to every JPanel is a Graphics2D, so you can cast to Graphics2D  Never send paintcomponent messages — send repaint() messages instead

20 Add the JPanel to a JFrame setLayout(new GridLayout(2, 2, 4, 4)); add(clickMeButton); add(textEditor); add(aLabel); add(new DrawingPanel()); // Adds a Panel to the 4 th spot

21 Drawing an Image  Java’s Image class in java.awt abstracts a bitmap image for use in drawing.  Images can be drawn on a panel  But first…

22 How do we load an image?  java.awt contains a method that returns an image from a file on your disk Image img = ImageIO.read(new File("fileName"));  Once we have an image and a graphics object to draw on, we can render that image // 'g2' is a Graphics context object and img // is an initialized Image. 12 is x, 24 is y (location) g.drawImage(img, 12, 24, null);

23 Drawing Our Image  This code would draw img at the coordinates (12, 24) on the panel  The final ‘this’ is for an ImageObserver object, which we won’t be using

24 Summary  To draw a png, jpg, or gif 1. Extend JPanel 2. Declare Image instance variables in that class 3. Let the constructor initialize the images 4. Overide paintComponent 5. get a Graphics2D object named g2 perhaps 6. send drawImage messages to g2

25 Example code that needs 6 jpg files in images public class CardsOnTheWater extends JPanel { private Image ocean, card1, card2, card3, card4, card5; public CardsOnTheWater() { try { ocean = ImageIO.read(new File("images/ocean.jpg")); card1 = ImageIO.read(new File("images/14h.jpg")); card2 = ImageIO.read(new File("images/13h.jpg")); card3 = ImageIO.read(new File("images/12h.jpg")); card4 = ImageIO.read(new File("images/11h.jpg")); card5 = ImageIO.read(new File("images/10h.jpg")); } catch (IOException e) { e.printStackTrace(); }

26 This method is called when the panel needs to be public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; g2.drawImage(ocean, 0, 0, this); g2.drawImage(card1, 10, 10, this); g2.drawImage(card2, 30, 15, this); g2.drawImage(card3, 50, 20, this); g2.drawImage(card4, 70, 25, this); g2.drawImage(card5, 90, 30, this); }

27 Still need to Add JPanel to a JFrame import javax.swing.JFrame; import javax.swing.JPanel; public class DrawCardsOnWaterMain extends JFrame { public static void main(String[] args) { new DrawCardsOnWaterMain().setVisible(true); } public DrawCardsOnWaterMain() { setSize(250, 250); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new CardsOnTheWater(); add(panel); }