1 / 4

Using Objects with Graphics

Learn how to use objects with Graphics to create and display shapes in Java. This tutorial covers drawing lines, polygons, and working with text areas.

chrisr
Download Presentation

Using Objects with 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. Using Objects with Graphics • The next example stores information about • the displayed shapes so that we can reproduce them • each time the system calls paintComponent. • We’ll make “smart” shape classes that can • draw themselves by using a Graphics object. • Method paintComponent in class DrawPanel • iterates through an array of MyLine objects. • Each iteration calls the draw method of the current MyLine object and • passes it the Graphics object for drawing on the panel.

  2. Drawing Polygons • A Polygon is a multisided closed shape • The Polygon class • can be used to define and draw a Polygon • Is part of the java.awt package • The overloaded drawPolygon and fillPolygon • take a single Polygon object as a parameter • instead of arrays of coordinates • A Polygon object encapsulates • the coordinates of the polygon

  3. Text Areas • Component • Used to show multiple lines of text • You can specify its number of rows and columns • To use for display purposes only • textArea.setEditable(false); • To update content: setText or append • To add scroll bars to a text area final int ROWS = 10; final int COLUMNS = 30; JTextArea textArea = new JTextArea(ROWS, COLUMNS); JTextArea textArea = new JTextArea(ROWS, COLUMNS); JScrollPane scrollPane = new JScrollPane(textArea);

  4. HashMap • A map • A collection of key/value pairs • Where every key has a unique value • A class implementing the map interface • HashMap part of java.util • Methods • put: to add an association • remove: to remove an association

More Related