240 likes | 266 Views
Basic Graphics. 03/03/16 & 03/07/16. Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010. This Week's Topics. Creating a Window Getting Ready to Draw A Sample Drawing Panel Displaying a Panel The Class Graphics
E N D
Basic Graphics 03/03/16 & 03/07/16 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010
This Week's Topics • Creating a Window • Getting Ready to Draw • A Sample Drawing Panel • Displaying a Panel • The Class Graphics • The Coordinate System • Drawing Rectangles • Painting Shapes Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010
Objectives After studying this chapter, you should be able to: • Write a program that creates a window with graphics • Display in a window • Rectangles, Ovals and Arcs • Use color in Graphics Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010
Creating a Window • A rectangular area • Part of a user interface • Called a frame • Contains title bar • An empty frame Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010
Creating a Window • Construct object of class JFrame • From package javax.swing JFrame aWindow = new JFrame(); • Specify size aWindow.setSize(400, 600); • setTitle() puts a title in the title bar: aWindow.setTitle(“Bronco Window”); Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010
Creating a Window • Window must be made visible. aWindow.setVisible(true); • View sample program,EmptyWindow Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010
Creating a Window • If the user closes the window with the control button, the window closes. The program keeps running. • aWindow.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE); • Exits program when window closes.
Preparing to Draw • Cannot draw directly on JFrame object • This is a container • Contains other objects • Can create a JPanel object and add it to the JFrame, but it would be blank. • The JPanel class is in javax.swing
Preparing to Draw • Create a class of panelsupon which to draw. • DrawingPanel inherits JPanel. • Graphics g parameter allows it to draw. Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010
Simple Drawing Panel • Some basic shapes. • Now we create a window and add an instance of class DrawingPaneltothe window. • Example in WindowWithDrawing Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010
Class Graphics • Java runtime environment instantiates a Graphicsobject • The graphics context • Made available to method paintComponent • Use methods of that Graphics object to display shapes.
Coordinate System • Figure 5-2 The axes and some points on the coordinate system used for graphics
Lines, Shapes, Text • Methods: • g.drawline (x1, y1, x2, y2); • g.drawRect(x, y, width, height); • g.drawOval(x, y, width, height); • Note pixels on edges of 5 x 4 rectangleFigure 5-3
Lines, Shapes, Text • Filled rectangles and squares • Specify position, dimensionsg.fillRect(x, y, width, height); • Pixels in 5 x 4 rectangle painted by fillRect Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010
Lines, Shapes, Text • Ovals, Circles • g.drawOval(x, y, width, height);( for circle, height = width) • Imaginary rectangle encloses oval Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010
Draw Arc g.drawArc(35, 45, 75, 95, 0, 90); drawArc(int x, int y, int width, int length, int startAngle, int arcAngle) • Used to draw an arc inside an imaginary rectangle whose upper left corner is at (x,y). The arc is drawn from the startAngle to startAngle + arcAngle and is measured in degrees. • A startAngle of 0º points horizontally to the right (like the unit circle in math). Positive is a counterclockwise rotation starting at 0º.
Class Color • In java.awt.Color • Create a color by specifying RGB values • Each primary color given by integer 0 – 255 • Predefined color constants from Color Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010
Change Colors Assume g is a Graphic object. g.setColor(Color.RED); //Makes the “pen” // red.
Example Make a project that uses Graphics to make a smiley face. Make a Frame Put a Jpanel on the Frame Make a Drawing on the panel
Smiley Face Draw a yellow circle Draw two solid arcs for the eyes Draw a smile
Participation • Create a Jframe (window) • Create a Panel and add it to the JFrame • Use the paintComponent to make a Japanese flag. • Draw a solid white rectangle. • Draw a solid red circle centered inside the white rectangle.
import javax.swing.JFrame; publicclass WindowWithDrawing { publicstaticvoid main(String[] args) { //set up a JFrame window JFrame bWindow = new JFrame(); bWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); bWindow.setSize(400,300); bWindow.setTitle("Shapes"); //Put a DrawingPanel in the Window //Make the window visible bWindow.setVisible(true); } }
import javax.swing.JPanel; import java.awt.Graphics; publicclass DrawingPanel extends JPanel { //A panel to draw on. publicvoid paintComponent(Graphics pen){ //Statements that draw go here } }
Basic Graphics Chapter 2 Imagine! Java: Programming Concepts in Context by Frank M. Carrano