repaint, multi class help...
hi, i have a main applet which will controll my other class which will do the drawings..
in my main applet, i will tell my circle class to draw a circle, and tell my box class to draw a box..
and i have no idea how to make this work.. i just cant seem to make my main applet repaint..
my box/circle class are like this
public box(){
{
repaint();
}
public paint(Graphics a)
{
a.setColor(Color.red);
a.fillRect(12,12,12,12);
}
}
and my main applet
publicclass app
{
publicvoid init()
{
box a =new box();
}
}
so once my main applet runs, shouldnt it create a new instance of box? which will invoke the method repaint which should paint rite? but how come nothing paints?
please help me.... thanks
[1360 byte] By [
rxtype] at [2007-9-30 4:38:30]

Is box a subclass of Component? Is it added to the Applet, so that it can be made visible? For example:
public class Box extends Component //or some subclass of component...
{
//...
public Box(){
{
repaint(); //Probable shouldn't be here!!
//Because at construction the Box is probably NOT
//made visible, ie, added to a top levl component that is visible
}
public void paint(Graphics a)
{
a.setColor(Color.red);
a.fillRect(12,12,12,12);
}
//...
}
public class App extends Applet // If an Applet, you MUST extend Applet...
{
public void init()
{
Box a = new Box();
add(a); //So now, a (Box) is added to the Applet. The Applet will be made visible
//Hopefully, and when the applet is painted (automatically by the JVM when
//ever necessary) the Box will be also...
}
//...
}
You should look into the Tutorial:
http://java.sun.com/docs/books/tutorial/index.html
And look for GUIs, AWT, Swing, ...
ooo! i didnt add it to the componenetim gonna try that thhanks
ok.. it wont paint ...
[code]
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import javax.swing.*;
public class test extends Component implements Runnable
{
int x=2;
Thread a;
public void init()
{
a = new Thread(this);
System.out.println("start");
a.start();
System.out.println("stop");
}
public void run()
{
while (x != 0)
{
try
{
repaint();
Thread.sleep(100);
}
catch (InterruptedException e)
{
}
}
}
public void paint(Graphics g)
{
g.setColor(Color.red);
g.drawRect(x++,x++,x++,x++);
}
}
that is my test class where the stuff paints
import java.awt.*;
import java.applet.*;
public class abc extends Applet
{
public void init()
{
test a = new test();
add(a);
}
}
and this is my applet... before i didnt add componenet, the repaint method didnt even invoke cuz i added a showinputdialog in it to see if it was being used, and it wasnt,
now that i added component, its being used but its not painting....
oops, the test isnt suppose to be initit shoudl bepublic test()but that still doesnt work
It is a little more complicated than that, unfortunately. I have example code below, and hopefully enough comments to explain it...
// This is Box.java. It is a small red box that moves itself diagonally across the screen. We should NOT
// have the Box responsible for moving itself, that should be the job of the applet. But I kept it here because
// that is the way you had it...
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Color;
public class Box extends Component implements Runnable{
/* Use these four variables to control the size of the box
* we will draw, and where we should draw it...
*/
private int x = 0;
private int y = 0;
private int width = 20;
private int height = 20;
/* Constructor
*/
public Box() {
/* Make sure the Box drawing area starts at the top-left
* corner of its parent container (the applet) and is as
* big as we want a box to be...
*/
setBounds(x,y,width,height);
/* Create and start the Thread
*/
Thread t = new Thread (this);
t.start();
}
public void run()
{
/* We will continue to repaint until the box is drawn
* 500 pixels from the left of the applet
*/
while (x < 500)
{
try {
repaint();
/* After we repaint, we will move the box right 5px
* and down 5px. The x and y we use here is the location
* of THIS component in its parent container (the Applet)
*/
x+=5; y+=5;
setLocation(x,y);
Thread.sleep(100);
} catch (InterruptedException ie) {} //Its okay to be interrupted...
}
}
public void paint(Graphics g) {
g.setColor(Color.red);
/* We will draw the rectangle completely inside this component.
* Start at THIS component's 0,0, and draw according to the
* box's width and height (-1 for 0 indexing...).
*/
g.drawRect(0,0,width-1,height-1);
}
}
// This is the BoxRunner.java Applet. It also has a main method so you can run it from the
// command line. It just starts and holds a Box...
import java.awt.event.*;
import java.awt.*;
import java.applet.*;
public class BoxRunner extends Applet {
/* The init method will be used to initialize the
* GUI, which will include the Box. I use the
* init method to make sure that the Applet is
* correctly instantiated and made visible BEFORE
* we start trying to do repaints in the Box class...
*/
public void init()
{
/* I am setting the Layout to null so the Box's
* setBounds and setLocation will determine the
* size and placement of that component.
* The layout, and the lack of setBounds seemed to
* be the problem with the code you posted before
*/
setLayout(null);
Box b = new Box();
add(b);
}
/* Method main would be used if this where a stand alone
* application, and not an App. In that case we would
* create a Frame, and add the applet to it, then
* call the init() and start() methods
*/
public static void main(String[] args) {
/* Create the frame, give it a title
*/
final Frame f = new Frame("Box Drawer");
f.setSize(550,550);
/* The windowListener makes sure the frame is closed
* and the JVM exits when we want it to...
*/
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
f.dispose();
System.exit(0);
}
});
/* Create the applet, validate it by
* adding it to a frame, then making the
* frame visible. Then call the init and
* start methods...
*/
BoxRunner br = new BoxRunner();
f.add(br);
f.setVisible(true);
br.init();
br.start();
}
}
// This is the HTML we would need to display the applet in a webpage...
<!--
This is the HTML object tag we would use to add the Applet
into a webpage. We can run appletviewer using this
bare-minimums tag. Look up the proper use before
deploying...
-->
<object
width="550"
height="550"
>
<param name="code" value="BoxRunner.class">
<param name="codebase" value=".">
</object>
ok, it drew a white box, but it doesnt seem to be moving...
nevermindit works, i forgot to change the while partthank you very much!!!!!
Hi, i just ran into another problem...i have my box running accross the screen ok, but i want a circle to move down the screen, and when the box and cricle intersect... the graphics kinda screws up....how can i make it so that they dont screw up and will simply overlap?
Not sure what you mean. I made a circle class that was basically the same as the box class (except it used g.drawOval(0,0,width-1,width-1) in it's paint method) and had no problem.
I am gonna show my code below. I have since changed it a bit because I don't like a component (box and circle) controlling how it is placed inside of it's parent (the applet). So I made them just absolute basics, no threads, bounds, or nothing. Just drawing a box or circle with the dimensions provided.
I moved the thread, and positioning code into the Applet, where I believe they belong...
Here is the code:
// Box.java draws a rectanglular outline
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Color;
public class Box extends Component
{
private int width;
private int height;
public Box(int w, int h)
{
width = w;
height = h;
}
public void paint(Graphics g)
{
g.setColor(Color.red);
g.drawRect(0,0,width-1,height-1);
}
}
// Circle.java draws an oval outline
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Color;
public class Circle extends Component {
private int diameter;
public Circle(int d)
{
diameter = d;
}
public void paint(Graphics g)
{
g.setColor(Color.blue);
g.drawOval(0,0,diameter-1,diameter-1);
}
}
// BoxRunnerII.java will create a horixontal-moving square (box) and a verticle moving circle (circle) that
//will intersect in the middle of the applet...
import java.applet.Applet;
public class BoxRunnerII extends Applet implements Runnable {
private Box box;
private Circle circle;
// Controls the speed of the movement
private static final int DELTA_V = 3;
// controls the box position
private int box_x = 0;
private int box_y = 250;
// controls the circle position
private int circle_y = 0;
private int circle_x = 250;
//control the size of both the shapes
private int width = 20;
public void run()
{
while (box_x < 500)
{
try
{
repaint();
doMovement();
Thread.sleep(100);
} catch (InterruptedException ie) {}
}
}
// Change the locations of both the box and circle, based on
// DELTA_V for how many pixels to move.
private void doMovement()
{
box_x += DELTA_V;
circle_y += DELTA_V;
resetLocations();
}
private void resetLocations()
{
box.setLocation(box_x, box_y);
circle.setLocation(circle_x,circle_y);
}
public void init()
{
box = new Box(width,width);
box.setBounds(box_x,box_y,width,width);
circle = new Circle(width);
circle.setBounds(circle_x,circle_y,width,width);
setLayout(null);
add(box);
add(circle);
Thread t = new Thread(this);
t.start();
}
}
The HTML and main methods would be identicle to what I had before, but substitute "BoxRunnerII" for whenever you see "BoxRunner"
ok i will try that
also, when i want to paint something under the box's movement, that changes color, it would not work properly too, like i want a box to flash under the other box's movement red/yellow.. it would show half yellow and half red, but when it minimize the iwndow and open it up agian, it will show the whole thing 1 color... i have no idea why its occuring