WDD Lec 18
WDD Lec 18
Lecture 18
Java Graphics
Graphics
• Window is like a painter’s canvas
• App must paint its window contents
• Java components paint themselves
• Anything else: Programmer
• When to paint?
• How to paint?
JButton
MyApp
Open WinExp, Notepad
Close WinExplorer
Graphics2D g2 = (Graphics2D)g;
g2.drawRect(20,20,20,20);
g2.setColor(Color.blue);
g2.fillOval(50,50,20,20);
}
}
Example Code: Test1.java
import javax.swing.*;
import java.awt.*;
public class Test1{
JFrame f;
MyPan1 p;
public Test1(){
f = new JFrame();
Container c = f.getContentPane();
c.setLayout(new BorderLayout());
p = new MyPan1();
c.add(p);
f.setSize(400,400);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
} // end constructor
Example Code: Test1.java
public static void main(String args[ ]){
Test1 t = new Test1();
}
} // end class
Output
Graphics Primitives
Draw Fill
• Point (x,y)
• Line (pt1,pt2)
• PolyLine (pt list)
• Arc
• Oval (pt, w,h)
• Rectangle (pt, w,h)
• RoundRectangle
Hokie Orange