trouble getting buttons to show up

B

Bob

why don't the two buttons show up using the code below?

import javax.swing.JApplet;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class Ball extends JApplet implements ActionListener
{
public int x = 0, y = 50, dx = 1, dy = 2;
private int diameter = 50, width = 0, height = 0;
private JButton up, down;
private Container c;

public void init()
{
width = getSize().width;
height = getSize().height;
up = new JButton("up");
down = new JButton("down");
up.addActionListener(this);
down.addActionListener(this);
c = this.getContentPane();
c.setLayout(new FlowLayout());
c.add(up);
c.add(down);

}
public void paint(Graphics g)
{
while (true)
{

try{Thread.sleep(15);}
catch (Exception e){}

x += dx;
if (x < 0) dx = 1;
if (x > width-diameter) dx = -1;
y += dy;
if (y < 50) dy = 2;
if (y > height-diameter) dy = -2;
g.setColor(this.getBackground());
g.fillRect(0,45,width,height);
g.setColor(Color.red);
g.fillOval(x,y,diameter,diameter);
}
}
public void actionPerformed(ActionEvent e)
{
String cmd = e.getActionCommand();
if (cmd.equals("up"))
{
dx++;
dy++;
}

if (cmd.equals("down"))
{
dx--;
dy--;
}

}


Thank You

}
 
T

Thomas Weidenfeller

Bob said:
public void paint(Graphics g)
{
while (true)
{

You block the event dispatching thread. The list below (didn't I post
the list already once today?), contains a pointer to the TSC articles.
There is one explaining the Swing architecture and Swing's painting.
You might want to read them.

/Thomas

Sun's Swing Quick-Start Tutorial:

http://java.sun.com/docs/books/tutorial/uiswing/mini/index.html

Sun's Swing Tutorial:

http://java.sun.com/docs/books/tutorial/uiswing/index.html

Sun's Swing Connection (TSC) (Swing developer's site):

http://java.sun.com/products/jfc/tsc/index.html

The TSC article index (Swing architecture, Swing and threads, painting
architecture, etc.):

http://java.sun.com/products/jfc/tsc/articles/

Sun's well hidden set of Icons for Swing' Metal LnF:

http://developer.java.sun.com/developer/techDocs/hi/repository/

More icons:

http://sourceforge.net/projects/icon-collection/
(the link to javalobby.org is broken)

A collection of examples for doing nice things with Swing components
(some are a little bit outdated):

http://www2.gol.com/users/tame/swing/examples/SwingExamples.html

Christian Kaufhold's Java and Swing Know-How:

http://www.chka.de/

Marco Schmidt's Java resource page (Java imaging information)

http://www.geocities.com/marcoschmidt.geo/java.html

Jalice (Linda Radecke's site):

- no longer there

Karsten Lentsch's company web page:

http://www.jgoodies.com/

This group's archive at Google:

http://groups.google.com/groups?group=comp.lang.java.gui

Java Look and Feel Design Guidelines:

http://java.sun.com/products/jlf/ed2/book/index.html

Java Look and Feel Design Guidelines: Advanced Topics:

http://java.sun.com/products/jlf/at/book/index.html

Java 2D API:

The Programmer's Guide
http://java.sun.com/j2se/1.4.2/docs/guide/2d/spec/j2d-bookTOC.html

Java 3D API:

There is a separate newsgroup. see

http://groups.google.com/groups?group=comp.lang.java.3d

GUI Information in the SDK documentation (commonly
overlooked):

See your local SDK installation, or

Abstract Window Toolkit (AWT)
http://java.sun.com/j2se/1.4.2/docs/guide/awt/index.html

Swing
http://java.sun.com/j2se/1.4.2/docs/guide/swing/index.html

2D Graphics and Imaging
http://java.sun.com/j2se/1.4.2/docs/guide/2d/index.html

Image I/O
http://java.sun.com/j2se/1.4.2/docs/guide/imageio/index.html

Print Service
http://java.sun.com/j2se/1.4.2/docs/guide/jps/index.html

Input Method Framework
http://java.sun.com/j2se/1.4.2/docs/guide/imf/index.html

Accessibility
http://java.sun.com/j2se/1.4.2/docs/guide/access/index.html

Drag-and-Drop data
http://java.sun.com/j2se/1.4.2/docs/guide/dragndrop/index.html

Swing Examples in the SDK:

See the directory demo/jfc in your Java SDK installation.

General

The Java Tutorial:

http://java.sun.com/docs/books/tutorial/

All kinds of tutorials:
http://developer.java.sun.com/developer/onlineTraining/

Roedy's Java Glossary:

http://www.mindprod.com/jgloss/jgloss.html

The (more and more outdated) Java Programmer's FAQ:
http://www.afu.com/intro.html

Making the most out of a newsgroup (ignore the hacker slang):
http://www.catb.org/~esr/faqs/smart-questions.html

/Thomas
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top