java GUI problems

S

summer_haven

Hey guys,
I've created two seperate GUIs in Java, one of which simply outputs the
current time while the other shows squares moving across the screen.. I
am aiming to to put both GUIs onto one window, so that in the upper box
is the current time and in the lower box is the moving squares. Is this
possible? Any ideas how to go about it?
Here is the current code:

clock.java

import java.awt.*;
import java.applet.*;
import java.util.*;

public class clock extends Applet implements Runnable
{
Font f = new Font("ComicSans",Font.ITALIC,24);
Date d; Thread runner;

public void init()
{resize(300,100);}

public void paint(Graphics g)
{g.setFont(f);g.drawString(d.toString(),10,50);}

public void start()
{
while (runner == null)
{
runner = new Thread(this); runner.start();
}
}

public void run()
{
while (true)
{
d = new Date(); repaint();
try {Thread.sleep(1000);}
catch(InterruptedException e){};
}
}
}


The Square Box java code is very long so i will leave it out for now
unless it is needed by you?

Thanks for any help: )
 
O

Oliver Wong

summer_haven said:
Hey guys,
I've created two seperate GUIs in Java, one of which simply outputs the
current time while the other shows squares moving across the screen.. I
am aiming to to put both GUIs onto one window, so that in the upper box
is the current time and in the lower box is the moving squares. Is this
possible?

Yes, it's possible.
Any ideas how to go about it?
Here is the current code: [most of the code snipped]

public class clock extends Applet implements Runnable
{ [...]

public void paint(Graphics g)
{g.setFont(f);g.drawString(d.toString(),10,50);} [...]

The Square Box java code is very long so i will leave it out for now
unless it is needed by you?

Thanks for any help: )

Assuming your Square drawing program is also an applet and structured
similarly, you could just combine the two paint() methods together into one,
making the appropriate modifications so that the squares are below the
clock, instead of in the same location.

Alternatively, you could refactor the code so that instead of drawing
inside of an applet, you're drawing inside of a panel which is the sole
component inside the original applet. Then simply create a new applet which
adds these two panels to its main content frame.

- Oliver
 
C

Chris Smith

summer_haven said:
I've created two seperate GUIs in Java, one of which simply outputs the
current time while the other shows squares moving across the screen.. I
am aiming to to put both GUIs onto one window, so that in the upper box
is the current time and in the lower box is the moving squares. Is this
possible? Any ideas how to go about it?

Yes, it is possible. You are apparently working with AWT applets (it'd
be a good idea to say so in future questions). If you want to put both
of these two things into one applet, then you should modify the code so
that the interesting functionality is not in the Applet class, but in a
subclass of java.awt.Canvas. If you choose the formulaic way of doing
it, then your new Applet will look something like:

public class MyApplet extends Applet
{
Clock clock = new Clock();
SquareBoxes boxes = new SquareBoxes();

public MyApplet()
{
setLayout(new GridLayout());
add(clock);
add(boxes);
}

public void init()
{
clock.init();
boxes.init();
}

public void start()
{
clock.start();
boxes.start();
}

public void stop()
{
clock.stop();
boxes.stop();
}

public void destroy()
{
clock.destroy();
boxes.destroy();
}
}

where Clock is your original Clock applet, modified to subclass Canvas,
and SquareBoxes is the same for your applet with the boxes.

Then you should start cleaning up. It's messy to leave init(), start(),
stop(), and delete() methods in the Canvas subclasses, so look at what
they do and adjust as appropriate. If they don't do anything, you can
take them out. If init() does something, for example, then you may be
able to move it into a constructor, or into an addNotify override.

Hope that helps,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,270
Messages
2,571,102
Members
48,773
Latest member
Kaybee

Latest Threads

Top