The Artima Developer Community
Sponsored Link

Java Answers Forum
Pls explain on this GUI

1 reply on 1 page. Most recent reply: Apr 11, 2003 7:45 AM by Ling

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Ling

Posts: 13
Nickname: ling
Registered: Feb, 2003

Pls explain on this GUI Posted: Apr 11, 2003 3:56 AM
Reply to this message Reply
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();
    	}
    }
}


Ling

Posts: 13
Nickname: ling
Registered: Feb, 2003

Re: Pls explain on this GUI Posted: Apr 11, 2003 7:45 AM
Reply to this message Reply
ok...this is what i have done instead after realising my stupid mistakes in the earlier post. Can anyone offer some clues on how to make the random blue ball move only within the boundary of the red circle? thanx a lot!

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

class Lab_06 extends JFrame
{
private RandomOval display;

public Lab_06() {
super("Random Ball Generator");
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());

RandomOval display = new RandomOval(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);

setDe faultCloseOperation(EXIT_ON_CLOSE);
pack();
setResizable(false);
setVisible(true );
}

public static void main(String[] args)
{
JFrame f = new Lab_06();
}
}

class RandomOval extends JLabel implements ActionListener
{
private int x0, y0;
private int wOval = 40;
private int hOval = 40;

public RandomOval(int width, int height)
{
setPreferredSize(new Dimension(width, height));
setOpaque(true);
}

protected void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.RED);
g.drawOval(0, 0, 300, 300);

g.setColor(Color.BLUE);
g.fillOval(x0, y0, 40, 40);
}

public void actionPerformed(ActionEvent e)
{
int wLabel = getWidth();
int hLabel = getHeight();
x0 = (int)(wLabel * Math.random());
y0 = (int)(hLabel * Math.random());
repaint();
}
}

Flat View: This topic has 1 reply on 1 page
Topic: hashtabel help..urgent Previous Topic   Next Topic Topic: need help with some simple code.

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use