Computer Science 209 Images and GUIs.

Slides:



Advertisements
Similar presentations
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.
Advertisements

1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
Multithreading : animation. slide 5.2 Animation Animation shows different objects moving or changing as time progresses. Thread programming is useful.
Introduction to Java Classes, events, GUI’s. Understand: How to use TextPad How to define a class or object How to create a GUI interface How event-driven.
Computer Science 209 Applets. Applications and Applets A Java application runs on a stand-alone computer and may connect to other computers via sockets.
Computer Science 209 Graphics and GUIs. Working with Color The class java.awt.Color includes constants for typical color values and also supports the.
GUI Tutorial Day 3. Custom Dialog Create, display, hide, retrieve information.
© L.Lúcio, An example GUI in Java n Two graphic libraries in Java u AWT u Swing n Swing is more recent than AWT: u Built on top of AWT classes;
IEEM 110 Computing in Industrial Applications Basic User Interface in Java.
Object Oriented Programming Java 1 GUI example taken from “Computing Concepts with Java 2” by Cay Horstmann GUI Programming.
Graphical User Interfaces
GUI Tutorial Images. Useful Info – not on final exam.
1 Class 8. 2 Chapter Objectives Use Swing components to build the GUI for a Swing program Implement an ActionListener to handle events Add interface components.
Lesson 35: Review of the Java GUI. The JFrame, Container and JButton.
Introduction to GUI Java offers a great number of pre-defined classes to support the development of graphical user interfaces –These are broken down into.
Combo Box, Check Boxes, and Radio Buttons. Radio Buttons User can click radio buttons, just like other buttons BUT Radio buttons are mutually exclusive.
Object-Oriented Programming (Java), Unit 19 Kirk Scott 1.
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.
GUI programming Graphical user interface-based programming.
GUI Components and Design Here we add one more component to our programs, JButtons –JButtons can only be inserted into JPanels (or JApplets) –Clicking.
Object-Oriented Programming (Java), Unit 18 Kirk Scott 1.
GUIs for Video Games. The Plan ● Layout suggestions ● Observer/Observable ● Steps to writing Observer/Observables ● Sample Code ● Practice.
(c)2009 by E.S. Boese. All Rights Reserved. Classes Chapter 13 – Lecture Slides 1 Got class?
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.
CompSci 100E 35.1 Graphical User Interfaces: GUIs  Components  Flat Layouts  Hierarchical Layouts  Designing a GUI  Coding a GUI.
EVENTS Wiring together objects, code, and actions.
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.
Lesson 39: More wrapup on GUIs. 1.This presentation will focus on the decisions around software architecture and the decisions that will drive the code.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
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)
A simple swing example GETTING STARTED WITH WIND CHILL.
1 GUI programming Graphical user interface-based programming Chapter G1 (pages )
1 Lecture 25 Listening to buttons and mice Quotes by Tony Hoare There are two ways of constructing a software design: (1) make it so simple that there.
1 Applications & Applets Standalone applications & Java applets Peter Mozelius DSV/UCSC.
Review_6 AWT, Swing, ActionListener, and Graphics.
Computer Science 209 GUIs Model/View/Controller Layouts.
Java layout managers. import java.awt.*; import java.awt.event.*; import javax.swing.*; class ButtonPanel extends JPanel implements ActionListener { public.
Ch15 簡單計算機 物件導向系統實務. Ch22_Main.java import javax.swing.*; import java.awt.*; import java.awt.event.*; class Ch22_Main { public static void main(String.
1 Layout Managers Layout managers –Provided for arranging GUI components –Provide basic layout capabilities –Processes layout details –Programmer can concentrate.
Computer Science 209 Specializing by Subclassing.
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.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
제 14 장 고급 스윙 컴포넌트.
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.
Lecture 6 Object Oriented Programming Using Java By Rashid Ahmad Department of Computer Science University of Peshawar.
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
Graphical User Interfaces -- GUIs
GUIs Model/View/Controller Layouts
Computer Science 209 Graphics and GUIs.
Graphical User Interface (pronounced "gooey")
Computer Science 209 The Singleton Pattern.
Ellen Walker Hiram College
Graphical user interface-based programming
Components.
PC02 Consolidation %WITTY_QUOTE%. PC02 Consolidation %WITTY_QUOTE%
Chapter 7-2 (Book Chapter 14)
어서와 Java는 처음이지! 제13장 실전프로젝트.
Chapter 7-2 (Book Chapter 14)
CiS 260: App Dev I Chapter 6: GUI and OOD.
Specializing by Subclassing
Graphical User Interface
Presentation transcript:

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 colors You can also obtain any color in the RGB system with the syntax new Color(r, g, b)

Setting Colors of Components The method setForeground sets the color of the text of a label, button, or field The method setBackgound sets the color of these components and also of a panel

Example: Coloring Components public ColorView(){ setBackground(Color.black); setTitle("Color Example"); JLabel label = new JLabel("A label"); label.setForeground(Color.red); JTextField field = new JTextField("Some text"); field.setForeground(Color.blue); Container c = getContentPane(); c.add(label, BorderLayout.NORTH); c.add(field, BorderLayout.SOUTH); }

Displaying a Playing Card When a card is instantiated, load its image from a disk file Send the card to a panel for display (during instantiation or later) Retrieve the card’s image and display it

Classes MainView CardPanel Card

Example 1: Default Behavior import javax.swing.*; import java.awt.*; public class MainView1 extends JFrame{ public MainView1(){ CardPanel panel = new CardPanel(); Container c = getContentPane(); c.add(panel, BorderLayout.CENTER); } Reasonable display when model is not available

Example 2: Card at Instantiation import javax.swing.*; import java.awt.*; public class MainView2 extends JFrame{ public MainView2(){ Card queenSpades = new Card(Suit.spade, 12); queenSpades.turn(); CardPanel panel = new CardPanel(queenSpades); Container c = getContentPane(); c.add(panel, BorderLayout.CENTER); }

Example 3: Reset the Card import javax.swing.*; import java.awt.*; import java.awt.event.*; public class MainView3 extends JFrame{ public MainView3(){ final Deck deck = new Deck(); final CardPanel panel = new CardPanel(); JButton button = new JButton("Deal"); button.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ if (! deck.isEmpty()){ Card card = deck.deal(); card.turn(); panel.setCard(card); }}}); Container c = getContentPane(); c.add(panel, BorderLayout.CENTER); c.add(button, BorderLayout.SOUTH); }

Responsibilities of CardPanel Default display when there is no card (wire frame) Display the back side if not face up Display the face (the card’s image) if face up

The CardPanel Class import javax.swing.*; import java.awt.*; public class CardPanel extends JPanel{ private Card card; public CardPanel(){ this(null); } public CardPanel(Card c){ setBackground(Color.black); card = c; public void setCard(Card c){ repaint();

The paintComponent Method public void paintComponent(Graphics g){ super.paintComponent(g); Icon image; if (card == null){ image = Card.getBack(); g.setColor(Color.yellow); int x = (getWidth() - image.getIconWidth()) / 2; int y = (getHeight() - image.getIconHeight()) / 2; g.drawRect(x, y, image.getIconWidth(),image.getIconHeight()); } else{ image = card.getImage(); image.paintIcon(this, g, x, y);

Responsibilities of Card Load this card’s image at instantiation Load the back side on demand Provide access to this card’s image and to the back side

The Card Class import javax.swing.*; public class Card implements Comparable<Card>{ private boolean faceUp; private Icon image; private static Icon CARD_BACK; public Card(Suit suit, int rank){ faceUp = false; image = getImageFromFile(rank, suit); } public Icon getImage(){ if (faceUp) return image; else return getBack(); # Continued on the next slide . . .

The Card Class public static Icon getBack(){ if (CARD_BACK == null) CARD_BACK = getBackFromFile(); return CARD_BACK; } private Icon getImageFromFile(int rank, Suit suit){ String fileName = "DECK/"; fileName += rank; fileName += Character.toUpperCase(suit.toString().charAt(0)); fileName += ".GIF"; return new ImageIcon(fileName); private static Icon getBackFromFile(){ return new ImageIcon("DECK/CARDBACK.GIF");

Using getResource import javax.swing.*; public class Card implements Comparable<Card>{ private boolean faceUp; private Icon image; private static Icon CARD_BACK; public Card(Suit suit, int rank){ faceUp = false; image = getImageFromFile(rank, suit); if (CARD_BACK == null) CARD_BACK = getBackFromFile(); // All images must } // be loaded in // non-static context # Continued on the next slide . . . In Eclipse, you must use getResource with an object instance to locate an image file on disk

Using getResource public static Icon getBack(){ if (CARD_BACK == null) new Card(Suit.spade, 1); // Sets CARD_BACK return CARD_BACK; } private Icon getImageFromFile(int rank, Suit suit){ String fileName = "DECK/"; fileName += rank; fileName += Character.toUpperCase(suit.toString().charAt(0)); fileName += ".GIF"; return new ImageIcon(getClass().getResource(fileName)); private static Icon getBackFromFile(){ String fileName = "DECK/CARDBACK.GIF";

static Variables and Methods static variables, such as Math.PI, belong to no instance of a class but can be shared by all instances static methods, such as Math.sqrt, are run with the class, not an instance Use static to represent a resource that all instances have in common