The Artima Developer Community
Sponsored Link

Java Answers Forum
Urgetnt Problem on GUI

2 replies on 1 page. Most recent reply: Apr 12, 2003 7:09 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 2 replies on 1 page
Priya

Posts: 2
Nickname: sutha
Registered: Apr, 2003

Urgetnt 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();
}
}


Wayne Weeks

Posts: 5
Nickname: wayneweeks
Registered: Apr, 2003

You shouldn't sleep thru Trig. Posted: Apr 12, 2003 3:24 AM
Reply to this message Reply
Well my best guess for that would be to use a fixed starting point for your ball and then subtract or add x and y values to that origin value. To decide what to add you will have to write an equation.

To help you see this better get a piece of paper and a pencil. Draw a line from the center to the edge. Now draw a two lines from the endpoints of that line to make a right triange. The radius of the circle, aka the hypotenuse of your triange, has a lenght equal to ((x^2)+(y^2))^(1/2). Thank Unckle Pythagoras for that one.

If you solve out the algebra you should be able to generate a y value between the + and - absolute value of the radius and run that thru the equation to find the appropriate x value.

keep in mind that there is always,excepting y=raduis, a positive and a negative posiblity for x. I hope this helps.

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: Urgetnt Problem on GUI Posted: Apr 12, 2003 7:09 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();
    }
} 
{/java]

Flat View: This topic has 2 replies on 1 page
Topic: Cleaned code up, but still need help. Previous Topic   Next Topic Topic: java programming problem

Sponsored Links



Google
  Web Artima.com   

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