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.

Slides:



Advertisements
Similar presentations
Java GUI building with the AWT. AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Advertisements

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.
Graphical User Interfaces
Graphic User Interfaces Layout Managers Event Handling.
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.
Java Swing Recitation – 11/(20,21)/2008 CS 180 Department of Computer Science, Purdue University.
Slides prepared by Rose Williams, Binghamton University Chapter 17 Swing I.
Inheritance Review CSC 171 FALL 2004 LECTURE 19. READING Read Horstmann, Chapter 11 Look at Chapter 12 – Will not be on MT or Final – One lab Begin Reading.
Swing CS-328 Dick Steflik John Margulies. Swing vs AWT AWT is Java’s original set of classes for building GUIs Uses peer components of the OS; heavyweight.
Advanced Java Class GUI – part 1. Intro to GUI GUI = Graphical User Interface -- “Gooey” Just because it’s “gooey” does not mean you may write messy code.
IEEM 110 Computing in Industrial Applications Basic User Interface in Java.
Carnegie Mellon University, MISM1 Java GUI programming and Java Threads GUI example taken from “Computing Concepts with Java 2” by Cay Horstmann Thread.
Object Oriented Programming Java 1 GUI example taken from “Computing Concepts with Java 2” by Cay Horstmann GUI Programming.
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.
Java Programming Chapter 10 Graphical User Interfaces.
OOP (Java): Layout/ OOP Objectives – –describe the basic layout managers for GUIs Semester 2, GUI Layout.
Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class.
Java Software Solutions Lewis and Loftus Chapter 10 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Graphical User Interfaces --
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
MSc Workshop - © S. Kamin, U.Reddy Lect 5 – GUI Prog - 1 Lecture 5 – GUI Programming r Inner classes  this and super r Layout r Reading: m.
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.
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.
Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6.
tiled Map Case Study: Rendering with JPanel © Allan C. Milne v
1 Why layout managers Can we perform layout without them? –Yes. A container’s layout property can be set to null. Absolute positioning: specify size and.
10/24/20151 Java GUI Programming. 10/24/20152 What is a GUI? Java has standard packages for creating custom Graphical User Interfaces Some of the fundamental.
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.
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,
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Layout Manager Summary
1 GUIs, Layout, Drawing Rick Mercer. 2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs)
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.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
University of Limerick1 Software Architecture Java Layout Managers.
The Abstract Window Toolkit (AWT) supports Graphical User Interface (GUI) programming. AWT features include: a rich set of user interface components; a.
1 GUIs, Layout, Drawing Rick Mercer. 2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs)
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 10: Event Handling 1 Event Handling.
Introduction to Java Chapter 9 - Graphical User Interfaces and Applets1 Chapter 9 Graphical User Interfaces and Applets.
Java Dynamic Graphics.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 10: Event Handling 1 Chapter 10 Event Handling.
Review_6 AWT, Swing, ActionListener, and Graphics.
Computer Science 209 GUIs Model/View/Controller Layouts.
J McQuillanSE204:2004/2005: Lecture 3Slide 1 Specialised Components Can create specialised components. Do this by subclassing the component that you are.
Java Swing One of the most important features of Java is its ability to draw graphics.
1 Layout Managers Layout managers –Provided for arranging GUI components –Provide basic layout capabilities –Processes layout details –Programmer can concentrate.
CSI 3125, Preliminaries, page 1 Layout Managers. CSI 3125, Preliminaries, page 2 Layout Managers The LayoutManagers are used to arrange components in.
Graphical User Interface (GUI) Two-Dimensional Graphical Shapes.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
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.
Chapter 7 A First Look at GUI Applications Layout Managers.
Object-Orientated Analysis, Design and Programming
GUIs Model/View/Controller Layouts
Modern Programming Language Java
Tim McKenna Layout Mangers in Java Tim McKenna
Chap 7. Building Java Graphical User Interfaces
Chapter 13: Advanced GUIs and Graphics
Graphical User Interfaces -- Introduction
Tim McKenna Layout Mangers in Java Tim McKenna
Creating Graphical User Interfaces
Introduction to Graphical Interfaces in Java: AWT and Swing
Advanced GUIs and Graphics
Graphical User Interface
Presentation transcript:

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 object, which can paint itself To do this: 1. Create a JPanel type class which can display your graphic 2. Add an object of this type to your frame, and show it

Create your JPanel type class by: 1. extending JPanel, so your class will BE a JPanel 2. set up similar to an applet… * constructor handles initialization and definition of any event handlers * override paintComponent instead of paint for drawing (NOT the paint method !!) Important: Your method paintComponent must call super.paintComponent to paint the background.

public class TriPanel extends JPanel{ //design my own JPanel Triangle mytri = new Triangle(10,10); //one triangle drawn and redrawn public TriPanel () { //set up panel and listener setPreferredSize( new Dimension(200,200)); class MListen extends MouseAdapter { //redraw when mouse clicks public void mouseClicked(MouseEvent e) { int x = e.getX(); int y = e.getY(); mytri.setTri(x,y); repaint(); } MListen mousel = new MListen(); addMouseListener(mousel); }

//continued public void paintComponent(Graphics g) { //override JPanel method super.paintComponent(g); //important!! Graphics2D gg = (Graphics2D) g; mytri.draw(gg); //triangle redraws itself }

//a JFrame can now use the class we have designed public class TriFrame{ public static void main (String [] args) { TriPanel mypanel = new TriPanel(); //create the panel JFrame myframe = new JFrame(); //put it in frame and show myframe.setContentPane(mypanel); //put it in frame myframe.pack(); myframe.show(); }

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 ( layoutmanager object); By default a panel’s layout is being managed by a FlowLayout() object. FLOW LAYOUT - components are added left to right, starting a new row if needed Different layouts: flow layout, border layout, grid layout

Border Layout Border layout places components into positions (center, north, west, south, eas Set up and use of Border layout panel.setLayout(new BorderLayout()); panel.add(myBtn, BorderLayout.SOUTH); *Border layout grows components to fit area (Flow layout retains size) *To avoid growth, place component into another panel (with flow layout), then add panel to content pane

Components Expand to Fill BorderLayout Area

Grid Layout Lays out components in rows and columns All components have the same size Add components left to right (top row first, then second row, etc.) panel.setLayout(new GridLayout(4, 3)); panel.add(button7); panel.add(button8); panel.add(button9); panel.add(button4);

Combining Layout Managers

The default layout of a frame is Border Layout. This layout provides for many panels to be placed on the frame. as Frames can begin to get complicated when layouts are added, it is a good idea to have methods which create the individual panels.. see Comp.java on website and a Frame itself is a component, whose code should not complicate an application… When a frame class extends JFrame, the main method of an application can just instantiate the frame…… (or create multiple instances if applicable) see FinalComp.java on website