Java Graphics Chris North cs 3724 HCI Presentations












- Slides: 12

Java Graphics Chris North cs 3724: HCI

Presentations • peter hou • Vote: UI Hall of Fame/Shame?

Project 1 ideas • Direct Manipulation: • Timeline for scheduling, set on/off flags • Calendar shows overview of schedule (info vis!) • Shneiderman experiment: timelines beat clock and numeric See book section 6. 6

Review • Layout managers • Events • Listener: interface, abstract base class • Adapter: implements methods, stubs • Object-oriented techniques for GUI programs: • extends: inheritance, specialize a swing component • Implements: listeners • Anonymous classes, inner classes: listeners

Graphics in Java • Draw in container: JPanel • Build tree of nested panels to organize UI JFrame JPanel JButton • Subclass JPanel • Override paint. Component( ) method • Call repaint( ) to force redraw JPanel

Code public class My. Panel extends JPanel { public void paint. Component(Graphics g){ super. paint. Component(g); // erases background Graphics 2 D g 2 = (Graphics 2 D)g; //cast for java 2 // my graphics: g 2. set. Color(new Color(255, 0, 0)); g 2. fill. Rect(10, 200, 200); g 2. set. Color(new Color(0, 0, 0)); g 2. draw. String("Hello World", 100); } }

Basic Graphics • Java. awt. Graphics 2 D • http: //java. sun. com/j 2 se/1. 3/docs/api/index. html • Basic shapes: • g 2. draw. Line(), . draw. Rect(), … • g 2. fill. Rect(), … • Color and fonts: • g 2. set. Color(), . set. Font() • new Color(R, G, B) • Line and Fill style: • g 2. set. Stroke(), . set. Paint()

Advanced Shapes • Java. awt. geom. * • Creating shapes: • Line 2 D, Rect 2 D, …, General. Path, … • g 2. draw(shape), . fill(shape)

In JBuilder • Subclassing JPanel • Overriding paint. Component( ) • Using subclassed JPanel in a JFrame

Double Buffering • Ugly flashing when repaint • Paint background • Redraw shapes • Solution: Double buffering • Draw all graphics in off-screen image • Then paint image to JPanel image • Swing does this for you! • Draw graphics on JPanel • JPanel maintains off-screen image panel

Problem • Dynamic moving shapes over other graphics • Examples: Mac. Draw, powerpoint • Solutions: • Minimize repaint rect: mypanel. repaint(rect) • Modified double buffering: maintain buffer for background graphics • XOR: draws by inverting pixel colors, drawing with XOR twice returns to original look, g 2. set. XORMode( )

Next • Proj 2: design due today • Spring break!!!! Presentations: UI critique or HW 2 results • Next Tues: pat kennedy, james walker • Next Thurs: dan kirkpatrick, jason barnett