Java Graphics Swing Graphics Empty Swing containers have

  • Slides: 12
Download presentation
Java Graphics

Java Graphics

Swing Graphics • Empty Swing containers have no visual appearance except for a background

Swing Graphics • Empty Swing containers have no visual appearance except for a background color • Every JComponent must have a paint. Component method that is called when the component is first made visible or needs to be redrawn for some reason • The JPanel component is a lightweight container, making it suitable as a drawing area • A common way to do graphics is to extend the JPanel class and override the paint. Component method

The paint. Component Method • Called by the JVM when this component needs to

The paint. Component Method • Called by the JVM when this component needs to be redrawn • A single argument, the component's graphics context (class: Graphics), is passed when paint. Component is called • A Graphics object contains: • • • the component on which it draws the current color and font location origin clipping information and more

Graphics vs. Graphics 2 D • The Graphics class has limitations: • Cannot use

Graphics vs. Graphics 2 D • The Graphics class has limitations: • Cannot use real number coordinates • Cannot draw dotted, dashed, or variable-width lines • Cannot easily draw complex curves or fill complex shapes • Cannot use textures or gradient colors to fill shapes • The newer Graphics 2 D class extends Graphics and provides these capabilities • All GUI components use a Graphics 2 D object but paint. Component passes a Graphics object for backward compatibility

General Approach public class My. Panel extends JPanel { // instance variables public My.

General Approach public class My. Panel extends JPanel { // instance variables public My. Panel() { // public constructor } // public methods // private helper methods public void paint. Component(Graphics g) { super. paint. Component(g); Graphics 2 D g 2 d = (Graphics 2 D)g; // drawing messages sent to g 2 d. . . } }

The paint. Component Method • super. paint. Component(g) is called first to ensure that

The paint. Component Method • super. paint. Component(g) is called first to ensure that painting responsibilities defined in JPanel are carried out • You should not call paint. Component directly; it is called by the JVM when it needs to • You can indirectly call paint. Component on a component by using component. repaint()

Some Basic Graphics Methods • void set. Color(Color color) • void set. Font(Font font)

Some Basic Graphics Methods • void set. Color(Color color) • void set. Font(Font font) • void draw. String(String text, int x, int y) • (x, y)is the coordinate of the lower left corner of the drawn string's leftmost character

Graphics Example import javax. swing. *; import java. awt. *; public class Graphics. Panel

Graphics Example import javax. swing. *; import java. awt. *; public class Graphics. Panel extends JPanel { public Graphics. Panel() { set. Preferred. Size(new Dimension(200, 200)); set. Background(Color. magenta); // panel color } public void paint. Component(Graphics g) { super. paint. Component(g); Graphics 2 D g 2 D = (Graphics 2 D)g; g 2 D. set. Color(Color. blue); // drawing color g 2 D. set. Font(new Font("Helvetica", Font. BOLD, 24)); g 2 D. draw. String("Hello World", 25); } }

Graphics Example (cont'd) import javax. swing. *; import java. awt. event. *; public class

Graphics Example (cont'd) import javax. swing. *; import java. awt. event. *; public class Main. Frame extends JFrame { public Main. Frame() { set. Size(new Dimension(500, 300)); set. Location(100, 100); add. Window. Listener(new Window. Adapter () { public void window. Closing(Window. Event e) { dispose(); System. exit(0); } }); get. Content. Pane(). set. Layout( new Flow. Layout(Flow. Layout. CENTER)); Graphics. Panel gp = new Graphics. Panel(); get. Content. Pane(). add(gp); set. Visible(true); } public static void main(String[] args) { new Main. Frame(); } }

Display

Display

Notes On The Example • Graphics. Panel extends JPanel so that the paint. Component

Notes On The Example • Graphics. Panel extends JPanel so that the paint. Component method can be overridden • If you forget to call super's paint. Component method, you can get pixels from another desktop frame as background garbage • The background color is associated with the panel; the paint color with the Graphics 2 D object • The Main. Frame class extends JFrame and an instance of it is created in the main method

Drawing Shapes • You can draw any object that implements the java. awt. Shape

Drawing Shapes • You can draw any object that implements the java. awt. Shape interface. • Example: suppose g 2 D is a Graphics 2 D object: Shape s =. . . ; g 2 D. draw(s); The Java library supplies a number of classes that implement the Shape interface type.