The Artima Developer Community
Sponsored Link

C# Answers Forum
Java Program Question

1 reply on 1 page. Most recent reply: Nov 15, 2004 12:58 AM by Matt Gerrans

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 1 reply on 1 page
Charles Thomas Boyd

Posts: 2
Nickname: froyd116
Registered: Nov, 2004

Java Program Question Posted: Nov 14, 2004 9:42 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);
}
}


Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Java Program Question Posted: Nov 15, 2004 12:58 AM
Reply to this message Reply
An interesting C# question to be sure.

Flat View: This topic has 1 reply on 1 page
Topic: How to write an XML-RPC client and server in Java? Previous Topic   Next Topic Topic: A Tale of Two Cities

Sponsored Links



Google
  Web Artima.com   

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