Welcome to Dream.In.Code
Become a Java Expert!

Join 149,456 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,137 people online right now. Registration is fast and FREE... Join Now!




square

 
Reply to this topicStart new topic

square

phatty
7 Oct, 2007 - 06:35 AM
Post #1

New D.I.C Head
*

Joined: 27 Sep, 2007
Posts: 5


My Contributions
CODE

import java.awt.*;
import javax.swing.*;
import java.util.Random;

public class Square
{
private int size;
private int x;
private int y;
private Color color;
private int a1, a2, a3;
private static Random generator = new Random ();
//----------------------------------------------
//A parameterless constructor that generates random values for the size, color,
//x, and y. Make the size between 50 and 100, x between 0 and 300, y between 0
//and 300. The squares can be any color—note that you can pass a single int
//parameter to the Color constructor, but it will only consider the first 24 bits
//(8 bits for each of R, G, B component), as follow.
// color =new Color( 33, 100,76)
//----------------------------------------------
public Square()
{
size = generator.nextInt(51) + (50);
x = generator.nextInt(301);
y = generator.nextInt(301);
a1 = generator.nextInt(256);
a2 = generator.nextInt(256);
a3 = generator.nextInt(256);
color = new Color(a1,a2,a3);



//your code here
}
//----------------------------------------------
//A draw method that draws the square at its x,y coordinate in its color. Note
// that you need a Graphics- object (pen) as you see in the method
//specification, than use pen.fillRect(…)metod to draw.
//----------------------------------------------
public void draw(Graphics pen)
{
//your code here
pen.setColor(color);
pen.fillRect(x, y, size, size);
}
}


hello can somebody tell me why this isnīt working.... what is the fault..? thanks
User is offlineProfile CardPM
+Quote Post

alpha02
RE: Square
7 Oct, 2007 - 06:42 AM
Post #2

D.I.C Addict
Group Icon

Joined: 20 May, 2006
Posts: 687


Dream Kudos: 850
My Contributions
Hi there, what errors are you getting, and what's the code supposed to do?
User is offlineProfile CardPM
+Quote Post

phatty
RE: Square
7 Oct, 2007 - 06:48 AM
Post #3

New D.I.C Head
*

Joined: 27 Sep, 2007
Posts: 5


My Contributions
QUOTE(alpha02 @ 7 Oct, 2007 - 07:42 AM) *



Hi thanks for the reply... i donīt get any errors..but when i try to open it in an apple it just wont start.... below is the message i get....



----jGRASP exec: appletviewer jgrasphta.htm

java.lang.ClassCastException: Square
at sun.applet.AppletPanel.createApplet(AppletPanel.java:721)
at sun.applet.AppletPanel.runLoader(AppletPanel.java:650)
at sun.applet.AppletPanel.run(AppletPanel.java:324)
at java.lang.Thread.run(Thread.java:595)

----jGRASP: operation complete.



this is what im trying to do...
1. Write a class Square that represents a square to be drawn. Store the following information in instance variables:
ı size (length of any side)
ı x coord of upper left-hand corner
ı y coord of upper left-hand corner
ı color
write an applet DrawSquares that uses your Square class to create and draw 5 squares. This code should be very
simple; the paint method will simply create 5 Squares and then draw it. Don't forget to pass the Graphics object to draw.
User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Square
7 Oct, 2007 - 07:09 AM
Post #4

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,010



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
The code posted in the first post compiles just fine. Maybe the errors you are receiving are in the applet code, so post the other codes pls.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 01:26PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month