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
}
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
}