The Artima Developer Community
Sponsored Link

Java Answers Forum
help on Drawing Program

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Charles Thomas Boyd

Posts: 2
Nickname: froyd116
Registered: Nov, 2004

help on Drawing Program Posted: Nov 14, 2004 11:36 PM
Reply to this message Reply
Advertisement
The problem is really detailed and i am not understanding much of it. But its exercise 8.18 in Dietel Java How to Prgram book. Essentially, i need to create an applet that randomly draws lines, rectangles, and ovals. Can anyone help? I know it should start like this....

import java.awt.Graphics;

public class MyLine extends Object {
// holds the coordniates of the line
private int x1, y1, x2, y2;
// default constructor
public MyLine() { x1=y1=x2=y2=0; }
// convenience constructor
public MyLine(int x1, int y1, int x2, int y2) {
this.x1=x1; this.y1=y1; this.x2=x2; this.y2=y2;
}
// Draw the line on a Graphics object
public void draw( Graphics g ) {
g.drawLine(x1, y1, x2, y2);
}
/* Java Beans style get and set methods
* need one for each private variable that
* that we wish to make available */
public void setX1( int x1 ) { this.x1 = x1; }
public int getX1() { return this.x1; }
}

MyName.java
import javax.swing.JApplet;
import java.awt.Graphics;

public class MyName extends JApplet {
/* Soon we will learn how to use an
* array and a superclass to do this
* more elegantly */
public MyLine m0, m1, m2, m3;

/* Create (instantiate) instances of
* our letters */
public void init() {
m0 = new MyLine(5,95,5,5);
m1 = new MyLine(95,5,95,95);
m2 = new MyLine(5,5,95,95);
}
/* Draw them, soon we will learn how
* to use an array and dynamic dispatch
* to do this more elegantly */
public void paint( Graphics g ) {
m0.draw(g);
m1.draw(g);
m2.draw(g);
}
}

Topic: first timer Previous Topic   Next Topic Topic: earn money with your IT skills

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use