40 likes | 59 Views
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.
E N D
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.
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
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);
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