It gets an error message on the line -
Oval shape = new Oval (x, y, d, d);
I narrowed it down to the "d's" in it.
Here is the code:
import java.awt.Color;
import javax.swing.JFrame;
import java.util.Scanner;
public class shapes
{
public static void main(String[] args) {
JFrame win = new JFrame("usershape");
win.setBounds(10, 10, 400, 300);
win.setLayout(null);
win.setVisible(true);
Scanner scanner = new Scanner(System.in);
System.out.println ("Enter the X and Y coordinates and area of a circle: ");
int x = scanner.nextInt();
int y = scanner.nextInt();
int a = scanner.nextInt(); // a is the area
double r = (Math.sqrt (a) / 3.14) ; // r is the radius
double d = (r * 2); // d is the diameter
Oval shape = new Oval(x, y, d, d);
shape.setBackground(Color.black);
win.add(shape, 0);
win.repaint();
}
}
Why doesn't this work?
Started by JaseR33, Aug 07 2010 09:34 PM
3 replies to this topic
#1
Posted 07 August 2010 - 09:34 PM
|
|
|
#2
Posted 08 August 2010 - 12:41 AM
- Classes must always begin with a Capital Letter.
- It doesn't know the Oval class because i don't think it exists...
If I had to draw an oval i'd do it this way:
import java.awt.*;
import javax.swing.*;
import java.util.Scanner;
public class Shapes [B][COLOR="red"]extends JPanel [/COLOR][/B]{
int x,y,d;
JFrame frame;
public Shapes(){
[B][COLOR="red"]super();[/COLOR][/B]
frame = new JFrame("userscreen");
frame.add(this);
frame.setSize(800,600);
askUser();
}
private void askUser(){
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the X and Y coordinates and area of a circle: ");
x = scanner.nextInt();
y = scanner.nextInt();
int a = scanner.nextInt(); // a is the area
System.out.println("scanning done");
d = (int) Math.round(Math.sqrt(a*4/Math.PI)); // d is the diameter
frame.setVisible(true);
}
public static void main(String[] args) {
Shapes shape = new Shapes();
}
[B] [COLOR="red"] @Override
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.RED);
g.fillOval(x,y,d,d);
}[/COLOR][/B]
}
Note that setting the frame visible will automatically call the paint method.If you need to manually call the paint method you can use:
paint(this.getGraphics());
2nd note: the area you give in is not in cm² but pixels². So if you give an area of let's say 50. You'll barely see an oval. Area of 5000 gives a nice circle ;)
#3
Posted 08 August 2010 - 04:54 PM
Yeah , the Oval class definately exists, thats why I dont understand why it wont draw it.
Oh and how do I put my code in a little blue box like you do on here? Sorry for being a stupid newbie :)
here is the Oval class-
import javax.swing.*;
import java.awt.*;
/** Oval Supplier Class
* Author: David D. Riley
* Date: April, 2004
*/
public class Oval extends JComponent {
/** post: getX() == x and getY() == y
* and getWidth() == w and getHeight() == h
* and getBackground() == Color.black
*/
public Oval(int x, int y, int w, int h) {
super();
setBounds(x, y, w, h);
setBackground(Color.black);
}
/** post: this method draws a filled Oval
* and the upper left corner of the bounding rectangle is (getX(), getY())
* and the oval's dimensions are getWidth() and getHeight()
* and the oval's color is getBackground()
*/
public void paint(Graphics g) {
g.setColor( getBackground() );
g.fillOval(0, 0, getWidth()-1, getHeight()-1);
paintChildren(g);
}
}
Oh and how do I put my code in a little blue box like you do on here? Sorry for being a stupid newbie :)
here is the Oval class-
import javax.swing.*;
import java.awt.*;
/** Oval Supplier Class
* Author: David D. Riley
* Date: April, 2004
*/
public class Oval extends JComponent {
/** post: getX() == x and getY() == y
* and getWidth() == w and getHeight() == h
* and getBackground() == Color.black
*/
public Oval(int x, int y, int w, int h) {
super();
setBounds(x, y, w, h);
setBackground(Color.black);
}
/** post: this method draws a filled Oval
* and the upper left corner of the bounding rectangle is (getX(), getY())
* and the oval's dimensions are getWidth() and getHeight()
* and the oval's color is getBackground()
*/
public void paint(Graphics g) {
g.setColor( getBackground() );
g.fillOval(0, 0, getWidth()-1, getHeight()-1);
paintChildren(g);
}
}
#4
Posted 08 August 2010 - 10:16 PM
Ahhhhhh it's your own class :D That explains why i coudn't find it on the internet .
Posting code in the box = putting the code in code tags. like
Posting code in the box = putting the code in code tags. like
here goes my code [/ code] without a space between / and code. You can also press the # button. Okay why doesn't it work? The Oval constructor expects int, int, int, int and you give it int, int, double, double. try [code]Oval shape = new Oval(x, y, (int) d, (int) d);Note: casting double to int will not round the value but just drop the decimal part 1.999 would become 1 and not 2. You can do
int value = (int) Math.round(1.999);if you like to round.


Sign In
Create Account


Back to top









