1 / 24

Basic Graphics

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

Download Presentation

Basic Graphics

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Basic Graphics 03/03/16 & 03/07/16 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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.

  8. 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

  9. 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

  10. 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

  11. 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.

  12. Coordinate System • Figure 5-2 The axes and some points on the coordinate system used for graphics

  13. 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

  14. 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

  15. 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

  16. 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º.

  17. 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

  18. Change Colors Assume g is a Graphic object. g.setColor(Color.RED); //Makes the “pen” // red.

  19. 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

  20. Smiley Face Draw a yellow circle Draw two solid arcs for the eyes Draw a smile

  21. 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.

  22. 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); } }

  23. 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 } }

  24. Basic Graphics Chapter 2 Imagine! Java: Programming Concepts in Context by Frank M. Carrano

More Related