-
Need help
i need some help on how to draw 2D box and circle using loops under this condition...any1 kind enough to help me?
max amount of balls a box can hold is 50 balls ( 5 length x 10 height)
box and ball is a variable
-
-
this is wad i have currently and i also have another problem which is that the ballValue and boxValue beside the scrollbar does not refresh...
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class Division extends Applet implements AdjustmentListener {
int ballValue=0;
int boxValue=0;
int extra,noex;
Scrollbar ball,box;
Panel p1;
public void adjustmentValueChanged(AdjustmentEvent event){
ballValue = ball.getValue();
boxValue = box.getValue();
repaint();
}
public void init() {
p1 = new Panel();
p1.setLayout(new GridLayout(2,3));
p1.add(new Label("Enter number of balls : "));
p1.add(ball = new Scrollbar(Scrollbar.HORIZONTAL,0,1,0,501));
p1.add(new Label(""+ballValue));
p1.add(new Label("Enter number of boxes : "));
p1.add(box = new Scrollbar(Scrollbar.HORIZONTAL,0,1,0,11));
p1.add(new Label(""+boxValue));
add(p1);
ball.addAdjustmentListener(this);
box.addAdjustmentListener(this);
}
public void paint(Graphics g) {
extra= ballValue%boxValue;
noex= ballValue/boxValue;
if((ballValue%boxValue==0)||(boxValue==1))
{ g.drawString("Each box contains "+noex+" ball(s).",100,100);}
else if(boxValue>ballValue)
{ g.drawString("Not enough balls for "+boxValue+" boxes!!",100,100);}
else{
g.drawString("Can't divide "+ballValue+" balls equally into "+boxValue+" boxes!!",100,100);
g.drawString("There are "+extra+" balls left after division!!",100,120);
}
}
}
-
balls can be drawn with
Code:
g.fillOval(int x, int y, int width, int height);
and boxes (only outline) with
Code:
g.drawRect(int x, int y, int width, int height);
See javadoc for class Graphics for more details.
But i have to say that I'm not shure about what you intend to do.
Where are the coordinates of your balls and boxes and how do you want them to be drawn?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks