Ling
Posts: 13
Nickname: ling
Registered: Feb, 2003
Pls explain on this GUI
Posted: Apr 11, 2003 3:56 AM
Advertisement
Can anyone explain to me why this program cannot work? import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Lab_06 extends JFrame
{
private RandomOval display;
public Lab_06() {
super ("Random Circle Generator" );
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
Lab_06 display = new Lab_06(300, 300);
display.setBackground(Color.yellow);
contentPane.add(display, BorderLayout.CENTER);
JPanel control = new JPanel(new FlowLayout());
control.setBackground(Color.orange);
contentPane.add(control, BorderLayout.SOUTH);
JButton button = new JButton("Move" );
control.add(button);
button.addActionListener(display);
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setResizable(false );
setVisible(true );
}
public static void main(String[] args)
{
Lab_06 f = new Lab_06();
}
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.RED);
g.drawOval(0, 0, 300, 300);
}
class RandomOval extends JLabel implements ActionListener
{
private int x0, y0, wOval, hOval;
public RandomOval(int width, int height)
{
setPreferredSize(new Dimension(width, height));
setOpaque(true );
}
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.BLUE);
g.fillOval(x0, y0, wOval, hOval);
}
public void actionPerformed(ActionEvent e)
{
int wLabel = getWidth();
int hLabel = getHeight();
x0 = (int )(wLabel * Math.random());
y0 = (int )(hLabel * Math.random());
wOval = (int )((wLabel - x0) * Math.random());
hOval = (int )((hLabel - y0) * Math.random());
repaint();
}
}
}