Web Design & Development Lecture 19. Java Graphics 2.

Slides:



Advertisements
Similar presentations
Custom Painting Gestione della Grafica customizzata Vedi anche:
Advertisements

1 Graphical User Interface (GUI) Applications Abstract Windowing Toolkit (AWT) Events Handling Applets.
User Interfaces II GUI – Awt, Swing, Web
Rectangles moving and responding to the mouse. We want a window with a pile of rectangles in it When we click a rectangle it changes from filled to unfilled.
Introduction to Swing Components Chapter 14. Part of the Java Foundation Classes (JFC) Provides a rich set of GUI components Used to create a Java program.
Chapter 15 Graphics. To paint, you need to specify where to paint. Each component has its own coordinate system with the origin (0, 0) at the upper-left.
Copyright 2006 by Pearson Education 1 Building Java Programs Supplement 3G: Graphics.
OLD BUSINESS : Lets talk about the next exam Project proposals – Functional Definitions Go over homework – NEW BUSINESS: Chapter 4 today GUIs Assignment.
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.
Fun with Swing Chapter 9. Overview Swing vs. AWT Creating windows and panels. Displaying formatted text in panels. Drawing graphics (lines, circles, etc.)
Graphical User Interfaces
Made with love, by Zachary Langley Applets The Graphics Presentation.
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.
Frame Windows A frame object is used to create a graphical frame window. This frame is used to show information in a graphical application. The JFrame.
CSE 1341 Honors Professor Mark Fontenot Southern Methodist University Note Set 21.
Graphics and event-driven programs Learning objectives By the end of this lecture you should be able to: identify and use some of the common components.
Polymorphism Method overriding Method overloading Dynamic binding 1.
Chapter 2: Using Objects Part 2. Assume you wish to test the behaviour of some method. This is accomplished by providing a tester class: Supply a main.
Basic Java – Interface design. Understand: How to use TextPad for Java How to define classes and objects How to create a GUI interface How event-driven.
Graphical User Interfaces (Part IV)
Examples. // A simple Frame with Rectangle Inside import java.awt.*; import javax.swing.*; import java.awt.geom.*; // For Shapes class rectComponent extends.
AWT Components. 2 Using AWT Components 4 Component –Canvas –Scrollbar –Button –Checkbox –Label –List –Choice –TextComponent –TextArea –TextField 4 Component.
Mouse Listeners We continue our examination of GUIs by looking at how to interact with the mouse –Just as Java creates Events when the user interacts with.
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.
Graphics Chapter 16.  If you want to draw shapes such as a bar chart, a clock, or a stop sign, how do you do it?
CS18000: Problem Solving and Object-Oriented Programming.
LAB SESSION 7 Graphical user interface Applet fundamentals Methods in applets Execution of an applet Graphics class.
Graphics CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Unit 3 Graphical User Interface (GUI) Dr. Magdi AMER.
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.
Graphical User Interfaces, 2D Graphics & Game Programming.
Chapter 5 Programming Graphics. Chapter Goals To be able to write simple applications To display graphical shapes such as lines and ellipses To use colors.
Multithreading : animation. slide 5.2 Animation Animation shows different objects moving or changing as time progresses. Thread programming is useful.
Chapter 5 Programming Graphics. Chapter Goals To be able to write applications with simple graphical user interfaces To display graphical shapes such.
Graphical User Interface Bonus slides Interaction Between Components & Drawing.
Bar Graph Design. Left-side/Right-side mechanical processing creative, abstract reasoning.
More Java Drawing in 2D Animations with Timer. Drawing Review A simple two-dimensional coordinate system exists for each graphics context or drawing surface.
JAVA 程式語言入門 (II).  版面配置  事件驅動  Ch14_01.java 1. import javax.swing.*; 2. import java.awt.*; 3. class Ch14_01 4. { 5. public static void main(String.
Chapter 5 Ch 1 – Introduction to Computers and Java Defining Classes and Methods 1.
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.
Event-Driven Programming You might have realized in creating a GUI and clicking on the JButtons that nothing happens Clicking on a JButton causes the JVM.
Things to mention public static void main(String [] args) –The starting point for a free-standing Java application (i.e. one not run from the DrJava interactions.
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.
Event Handling Events and Listeners Timers and Animation.
Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.
Web Design & Development Lecture 18. Java Graphics.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 20.1 Test-Driving the Screen Saver Application.
CSE 219 Computer Science III Images. HW1 Has been posted on Blackboard Making a Game of Life with limited options.
Graphics in Java hussein suleman uct cs
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.
Lecture 8.3 The Use of JComponent. © 2006 Pearson Addison-Wesley. All rights reserved More About the Standard Drawing Classes java.awt.Container.
– Advanced Programming P ROGRAMMING IN Lecture 21 Introduction to Swing.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Java Classes, Objects, and Events: A Preview JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 21.1 Test-Driving the Painter Application.
(C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.
Java Dynamic Graphics.
1 Applets. 2 Design of Applets 3 Sun wrote Java to be executable within a hosting application browser The applications are applets. An applet is downloaded.
Review_6 AWT, Swing, ActionListener, and Graphics.
Java Swing One of the most important features of Java is its ability to draw graphics.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 27.1 Test-Driving the Drawing Shapes Application.
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
Event Driven (Asynchronous) Programming. Event handling in Unity Subclass a class that contains event handling methods, and then override those methods.
Java Applets.
Object-Orientated Analysis, Design and Programming
4.14 GUI and Graphics Case Study: Creating Simple Drawings (Cont.)
Applets.
A+ Computer Science METHODS.
A+ Computer Science METHODS.
Presentation transcript:

Web Design & Development Lecture 19

Java Graphics 2

When to Paint? Understanding Repaint Cycle

Your Painting Strategy Steps –Subclass JPanel class MyPanel extends JPanel –Override the paintComponent(Graphics g) method Inside method do what ever you want to do by using graphics object. –Install that JPanel inside a JFrame When frame becomes visible and its paintChildren() gets called your panel will become visible. To become visible your panel calls paintComponent() method on itself which will do your custom drawing

Last Example Code: MyPanel.java import javax.swing.*; import java.awt.*; public class MyPanel extends JPanel { public void paintComponent(Graphics g){ super.paintComponent(g); // erasing behaviour Graphics2D g2 = (Graphics2D)g; g2.drawRect(20,20,20,20); g2.setColor(Color.blue); g2.fillOval(50,50,20,20); g2.drawString("Hello World", 120, 50); }

Last Example Code: Test.java import javax.swing.*; import java.awt.*; public class Test{ JFrame f; MyPanel p; public Test1(){ f = new JFrame(); Container c = f.getContentPane(); c.setLayout(new BorderLayout()); p = new MyPanel(); c.add(p); f.setSize(400,400); f.setVisible(true); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } // end constructor

Last Example Code: Test.java public static void main(String args[ ]){ Test1 = new Test(); } } // end class

How to Animate Constantly need to call paintComponent() and draw the shape at new place (new co-ordinates) Painting is managed by system and calling paintComponent() directly is not recommended at all Use Repaint() mechanism

Problem & Solution What to do to move the shapes present in previous code when a mouse is dragged First time painting is what we already have done When a mouse is clicked find the co-ordinates of that place and paint Rectangle at that place by requesting, using repaint() call Here instead of Hardcoding the position of co-ordinates use some variables e.g. mx, my –g.drawRect(20,20,20,20) (small anim here) g.drawRect(mx,my,20,20) (small anim here)

Yesterday we have shown you a tennis game. Problem, How to code the paddle movement ? We will do it using mouse. Try it using Keys

Coordinate System Upside-down Cartesian Y window = height - Y cartesian (0,0) (width,0) (0,height)(width, height) X-axis Y-axis

Live Output

Example Code: MyPanel.java import javax.swing.*; import java.awt.*; public class MyPanel extends JPanel { int mX = 20; int mY = 20; public void paintComponent(Graphics g){ super.paintComponent(g); // erasing behaviour Graphics2D g2 = (Graphics2D)g; g2.fillRect(mX, mY,10, 20); }

Example Code: Test.java import javax.swing.*; import java.awt.*; Import java.awt.event.*; public class Test{ JFrame f; MyPanel p; public Test(){ f = new JFrame(); Container c = f.getContentPane(); c.setLayout(new BorderLayout()); p = new MyPanel(); c.add(p);

Example Code: Test.java f.setSize(400,400); f.setVisible(true); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Handler h = new Handler (); p.addMouseMotionListener(h); } // end constructor public class Handler extends MouseMotionAdapter{ public void mouseDragged(MouseEvent me){ p.mX = me.getX() ; p.mY = me.getY() ; p.repaint() ; }

Example Code: Test.java public static void main(String args[ ]){ Test t = new Test( ); } } // end class

Example Code: MyPanel.java import javax.swing.*; import java.awt.*; public class MyPanel extends JPanel { int mX = 200; int mY = 0; public void paintComponent(Graphics g){ super.paintComponent(g); // erasing behaviour Graphics2D g2 = (Graphics2D)g; g2.setColor(Color.blue); g2.fillOval(mX, mY, 20, 20); } } // end class

Example Code: AnimTest.java import javax.swing.*; import java.awt.*; Import java.awt.event.*; public class AnimTest implements ActionListener { JFrame f; MyPanel p; int x, y; public Test(){ f = new JFrame(); Container c = f.getContentPane(); c.setLayout(new BorderLayout()); x = 5; y = 3;

Example Code: AnimTest.java p = new MyPanel(); c.add(p); f.setSize(400,400); f.setVisible(true); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Timer t = new Timer (5, this); t.start(); } // end constructor …..

Example Code: AnimTest.java public void actionPerformed(ActionEvent ae){ if (f.getWidth()-40 == p.mX) x= -5; if (f.getHeight()-40 == p.mY) y= -3; if (p.mX == 0 ) x = 5; if (p.mY == 0 ) y=3; p.mX+=x; p.mY+=y; p.repaint() ; }

Example Code: AnimTest.java public static void main(String args[ ]){ AnimTest at = new AnimTest( ); } } // end class

Live Output