Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
An introduction to graphics programming in java, covering the creation and centering of frames, adding components to frames, layout managers such as flowlayout, gridlayout, and borderlayout, and drawing on panels using the paintcomponent method. It also discusses using colors, fonts, and font metrics, as well as drawing geometric figures like lines, rectangles, ovals, arcs, and polygons.
Typology: Slides
1 / 44
Graphics Class Hierarchy (Swing)
AWTEvent Font FontMetrics
Component
Graphics
Object Color
Container
Panel Applet
Frame Dialog
Window
JComponent
JApplet
JFrame JDialog
Swing Components in the javax.swing package
Lightweight
Heavyweight
Classes in the javax.swing package 1
LayoutManager
JComponent . JButton JMenuItem JCheckBoxMenuItem AbstractButton JComponent JMenu .JRadioButtonMenuItem .JToggleButton JCheckBox JRadioButton .JComboBox .JInternalFrame .JLayeredPane .JList .JMenuBar .JOptionPane .JPopupMenu .JProgressBar .JPane .JScrollBar .JScrollPane .JFileChooser .JSeparator .JSplitPane .JSlider .JTabbedPane .JTable .JTableHeader .JTextComponent .JTextField .JEditorPane .JTextArea .JToolBar .JToolTip .JTree .JRootPane .JPanel .JPasswordField .JColorChooser .JLabel
AWT (Optional) AWTEvent Font FontMetrics
Component
Graphics
Object Color
Canvas
Button
TextComponent
Label
List
CheckBoxGroup
CheckBox
Choice
Container Panel Applet Frame Dialog FileDialog
Window
TextField
TextArea
MenuComponent (^) MenuItem MenuBar
Menu
Scrollbar
LayoutManager
Frames
UI Components
Frame Pull-down Menus
User Interface Components (UI)
Panel
Panel Panel UI
Panel UI
Panel UI
Applet
Panel User Interface Components
Panel User Interface Components
Panel User Interface Components
Panel User Interface Components
panel
Pull-down Menus
Creating Frames
Run
import javax.swing.; public class MyFrame { public static void main(String[] args) { JFrame frame = new JFrame("Test Frame"); frame.setSize(400, 300); frame.setVisible(true); // frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE); } }*
NOTE: To enable it to run in JDK 1.2, the EXIT_ON_CLOSE option is commented.
Centering Frames
Centering Frames, cont.
CenterFrame Run
screenH
screenWidth
frameHeight
screenWidth
(x, y)
Frame
Screen
Adding Components into a
Frame
// Add a button into the frame frame.getContentPane().add( new JButton("OK"));
MyFrameWithComponents Run
Layout Managers
Kinds of Layout Managers
Example 8.
Testing the FlowLayout Manager
ShowFlowLayout Run
FlowLayout Constructors
Constructs a new FlowLayout with a specified alignment, horizontal gap, and vertical gap. The gaps are the distances in pixel between components.
Constructs a new FlowLayout with a specified alignment and a default gap of five pixels for both horizontal and vertical.
Constructs a new FlowLayout with a default center alignment and a default gap of five pixels for both horizontal and vertical.
Example 8.
Testing the GridLayout Manager
ShowGridLayout Run
GridLayout Constructors
hGap, int vGap)
ShowBorderLayout (^) Run
Using Panels as Containers
Example 8.4 Testing Panel
TestPanels Run
Drawing on Panels
text) and enable user interaction.
The Color Class
Color c = new Color(r, g, b);
r, g, and b specify a color by its red,
green, and blue components.
Example:
Setting Colors
The Font Class
Example:
Font myFont = new Font("SansSerif ", Font.BOLD, 16); Font myFont = new Font("Serif", Font.BOLD+Font.ITALIC, 12);
Setting Fonts
public void paint(Graphics g)
{
Font myFont = new Font("Times", Font.BOLD, 16); g.setFont(myFont); g.drawString("Welcome to Java", 20, 40);
//set a new font g.setFont(new Font("Courier", Font.BOLD+Font.ITALIC, 12)); g.drawString("Welcome to Java", 20, 70);
}
The FontMetrics Class
Leading
Ascent
Baseline Descent
Height