ch03g-2 - Building Java Programs 3G.2

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

View Full Document Right Arrow Icon
1 Building Java Programs 3G.2 These lecture notes are copyright (C) Marty Stepp and Stuart Reges, 2007.  They may not be  rehosted, sold, or modified without expressed permission from the authors.  All rights reserved.
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 Divide and Conquer Need to send commands to the Graphics g to draw the figure so pass Graphics g as a parameter. import java.awt.*; public class DrawCar { public static void main(String[] args) { DrawingPanel panel = new DrawingPanel(200,100); panel.setBackground(Color.LIGHT_GRAY); Graphics g = panel.getGraphics(); drawCar(g); }
Background image of page 2
3 Here are the details public static void drawCar(Graphics g) { g.setColor(Color.BLACK); g.fillRect(10, 30, 100, 50); g.setColor(Color.RED); g.fillOval(20, 70, 20, 20); g.fillOval(80, 70, 20, 20); g.setColor(Color.CYAN); g.fillRect(80, 40, 30, 20); } }
Background image of page 3

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

View Full DocumentRight Arrow Icon
4 Parameterized figures Task – draw the car multiple times Modify the previous car-drawing method to work at any location, so that it can produce an image such as the following: One car's top-left corner is at (10, 30). The other car's top-left corner is at (150, 10).
Background image of page 4
Parameterized figures To draw same figure many times write method to draw that figure method accepts the x/y position as parameters. Adjust x/y coordinates of your drawing commands to
Background image of page 5

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

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

Page1 / 23

ch03g-2 - Building Java Programs 3G.2

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

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