The Artima Developer Community
Sponsored Link

Java Answers Forum
Urgent Problem on GUI

1 reply on 1 page. Most recent reply: Apr 12, 2003 7:10 PM by Charles Bell

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
Priya

Posts: 2
Nickname: sutha
Registered: Apr, 2003

Urgent Problem on GUI Posted: Apr 12, 2003 3:11 AM
Reply to this message Reply
Advertisement
I need to write a program whereby, a ball of radius 40 is moved randomly within a red circle of framesize (300,300) when a button is clicked. However, i can only manage the ball to move all around the frame, how can i ge it to move within the red circle only? Any help would be greatly appreciated! Thanks! Im a beginner so detailed explanations would be better. Thanks again.

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

class Lab_06 extends JFrame
{
public Lab_06(String name) {
super(name);
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
getContentPane().add(new Circle(300, 300));

RandomBall display = new RandomBall(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("Random Ball Generator");
}
}

class Circle extends JLabel
{
public Circle(int width, int height) {
setPreferredSize(new Dimension(width, height));
}
}

class RandomBall extends JLabel
implements ActionListener
{
private int x0, y0, wBall, hBall, distance;

public RandomBall(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, wBall, hBall);
int x = getWidth(), y = getHeight();
g.setColor(Color.RED);
g.drawOval(x/300, y/300, x, y);
}

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


Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: Urgent Problem on GUI Posted: Apr 12, 2003 7:10 PM
Reply to this message Reply
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
 
class Lab_06 extends JFrame{
    public Lab_06(String name) {
        super(name);
        Container contentPane = getContentPane();
        contentPane.setLayout(new BorderLayout());
        getContentPane().add(new Circle(300, 300));
        
        RandomBall display = new RandomBall(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) {
        JFrame f = new Lab_06("Random Ball Generator");
    }
}
 
class Circle extends JLabel{
    public Circle(int width, int height) {
        setPreferredSize(new Dimension(width, height));
    }
} 
 
class RandomBall extends JLabel implements ActionListener{
    private int x0, y0, wBall, hBall, distance, wLabel, hLabel;
 
    public RandomBall(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, wBall, hBall);
        int x = getWidth(), y = getHeight();
        g.setColor(Color.RED);
        g.drawOval(x/300, y/300, x, y);
    }
 
    public void actionPerformed(ActionEvent e) {
        /* diameter of large circle is 300, radius 150 */
        /* diameter of ball is 80, radius 40 */
        /* So the center of the ball can move any where in a circle of radius 110. */
        /* and the angle theta can be anywhere between 0 and 2 pi radians. */
        double randomRadius = (110*Math.random()); 
        double theta = Math.PI*2d*Math.random(); 
        x0 = 150 + (int)(randomRadius  * Math.cos(theta));
        y0 = 150 + (int)(randomRadius  * Math.sin(theta));
        wBall = 40;
        hBall = 40;
        repaint();
    }
} 

Flat View: This topic has 1 reply on 1 page
Topic: Help with code modification Previous Topic   Next Topic Topic: testing range

Sponsored Links



Google
  Web Artima.com   

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