0% found this document useful (0 votes)
6 views11 pages

Graphics

The document provides an overview of GUI components and graphics in Java, emphasizing the importance of event listeners and the painting process for components. It explains how to use the Graphics object for drawing and the Java coordinate system, along with methods for managing colors and fonts. Additionally, it introduces Graphics2D for more advanced drawing capabilities and includes resources for further reference.

Uploaded by

kassiechirag10
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views11 pages

Graphics

The document provides an overview of GUI components and graphics in Java, emphasizing the importance of event listeners and the painting process for components. It explains how to use the Graphics object for drawing and the Java coordinate system, along with methods for managing colors and fonts. Additionally, it introduces Graphics2D for more advanced drawing capabilities and includes resources for further reference.

Uploaded by

kassiechirag10
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

GUI: Graphics and Painting

April 19, 2023

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
April 19, 2023 1 / 10
Recap: Components & Events

[Link] contains many JComponents that can be added to your


GUI.
Event listeners respond to user actions accordingly.
Refer to the Java API (Java SE 17) and the textbook for details!!

[Link]
. . . . . . . . . . . . . . . . . . . .
javax/swing/[Link] . . . . . . . . . . . . . . . . . . . .
April 19, 2023 2 / 10
Graphics

Everything on a screen (even text!) must be drawn.


Every component is responsible for drawing itself!
If you want to draw on a component then you need to create a
subclass of it and override paintComponent().
import javax . swing . ∗ ;
import java . awt . ∗ ;
c l a s s HelloWorldDisplay extends JPanel {
public void paintComponent ( Graphics g ) {
super . paintComponent ( g ) ;
g . drawString ( " Hello World ! " , 2 0 , 3 0 ) ;
}

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
April 19, 2023 3 / 10
Graphics

Every GUI component has a Graphics object associated with it


(graphics context).
Keeps track of colours, fonts, ...
Has instance methods to draw shapes, text and images.
import javax . swing . ∗ ;
import java . awt . ∗ ;
c l a s s HelloWorldDisplay extends JPanel {
public void paintComponent ( Graphics g ) {
super . paintComponent ( g ) ; / / why a r e we d o i n g t h i s ?
g . f i l l R e c t ( x , y , width , height ) ;
}

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
April 19, 2023 4 / 10
Repainting
Define paintComponent() BUT call repaint()

paintComponent() is called when the component is first drawn, and


when the size changes.
It is called by the system: you should NOT call it directly!
Call repaint() defined in the Component class to inform the system
that the component needs to be redrawn
repaint() returns immediately and the call to paintComponent()
happens later!
Use instance variables to change what you want to display!

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
April 19, 2023 5 / 10
Java Coordinate System

Figure: Java Coordinate System

How can I get the width and height of a component?


i n t width = t h i s . getWidth ( ) ;
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
April 19, 2023 6 / 10
Java Coordinate System

Figure: Java Coordinate System

How can I get the width and height of a component?


i n t width = t h i s . getWidth ( ) ;
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
April 19, 2023 6 / 10
Code Example: BallController

Figure: Ball Controller with Graphics

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
April 19, 2023 7 / 10
Colors & Fonts

There are different ways of specifying colors but the most common is RGB
Color myColor = new Color ( r , g , b ) ;
/ / r , g , and b a r e i n t e g e r s i n t h e range 0 t o 2 5 5 .

[Link] == [Link]
Set the current color with methods defined in the Component class:
g . setColor(color);
[Link](color);
[Link](color);
In Java, you can specify the font name, style and size.
Font plainFont = new Font ( " S e r i f " , Font . PLAIN , 1 2 ) ;
// d e f a u l t s i z e i s 12

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
April 19, 2023 8 / 10
Graphics2D

Subclass of Graphics that allows for more complex drawing!


Allows for antialiasing and drawing thicker lines
Can use a more complex coordinates system & perform
transformations
public void paintComponent ( Graphics g ) {
super . paintComponent ( g ) ;
Graphics2D g2 ;
g2 = ( Graphics2D ) g ;
...
/ / Draw on t h e component u s i n g g2 .
}

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
April 19, 2023 9 / 10
Resources

[Link]
desktop/java/awt/[Link]
[Link]

. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
April 19, 2023 10 / 10

You might also like