Lecture 9 - Graphics and Event Handling

Lecture 9 - Graphics and Event Handling - The Swing package...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Lecture 9 Graphics and Event-handling The Swing package • in the earlier versions of Java, graphical programming was achieved exclusively by making use of a package known as the Abstract Window Toolkit (AWT) .; • nowadays, graphics in Java is achieved via the Swing package; • the Swing classes build on the AWT classes to provide enhanced functionality and appearance; AWT and Swing The DrawHouse Class Analysis of the DrawHouse class The import clauses • the first of these imports the standard Java Abstract Window Toolkit; • although we are going to be using Swing, we still need many of the AWT classes for drawing and painting; • the next clause imports the Swing package - the Swing classes come in a library called Javax (Java eXtension). The class header: public class DrawHouse extends JFrame a JFrame is a Swing component that forms the visible window in which the graphic is displayed; by extending the JFrame class, we are making our DrawHouse class a kind of JFrame. The constructor public DrawHouse() { setTitle(“Draw a house”); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(300,350); setLocation(300,300); getContentPane().setBackgroundColor(Color.yellow); setVisible(true); }
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
// draws the House object public void paint(Graphics g) { super.paint(g); //call paint method of superclass JFrame g.drawRect(50,100,200,150); // walls g.drawRect(75,130,50,20); // left window g.drawRect(175,130,50,20); // right window g.drawRect(135,200,30,50); // door g.drawLine(50,100,150,25); // roof g.drawLine(150,25,250,100); // roof g.drawString("Home sweet home!",100,275); } The paint method for DrawHouse • this special method is a method of a basic graphics class called Component, of which JFrame is an extension; • we don't need to call this method anywhere because when the component becomes visible, the paint method is automatically called; • when this happens, an object of a core Java class called Graphics (which comes with the AWT package) is automatically sent in to this method. drawRect(x, y, l, h) The drawRect method of the Graphics class x y l h drawLine(x1, y1, x2, y2) The drawLine method of the Graphics class (x1,y1) (x2,y2) The SmileyFace class The class header: a JFrame is a Swing component that forms the visible window in which the graphic is displayed; by extending the JFrame class, we are making our SmileyFace class a kind of JFrame. The constructor The paint method for SmileyFace
Background image of page 2
• a Graphics object many useful methods. • the drawOval method takes four integer parameters; • referring to these as x , y , l , h, the oval that gets drawn fits into an imaginary rectangle that starts at position (x , y) , and is l pixels long and h pixels high: • because we want a circle, we have made the values of l and h equal. • the next line draws the mouth:
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/20/2008 for the course COMP 17011 taught by Professor Andynaftel during the Fall '07 term at University of Manchester.

Page1 / 7

Lecture 9 - Graphics and Event Handling - The Swing package...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online