1 Attachment(s)
need help finishing up this project but I don't know the code...please help
here is the code I have so far:
Code Java:
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import java.util.Scanner;
public class Program1 extends JFrame {
private int x1=100,y1=100,x2=150,y2=200,r1=50,r2=60;
boolean check = false;
public Program1()
{
String input = JOptionPane.showInputDialog("Enter the X-Coordinate of circle #1");
x1 = Integer.parseInt(input);
input = JOptionPane.showInputDialog("Enter the Y-Coordinate of circle #1");
y1 = Integer.parseInt(input);
input = JOptionPane.showInputDialog("Enter the X-Coordinate of circle #2");
x2 = Integer.parseInt(input);
input = JOptionPane.showInputDialog("Enter the Y-Coordinate of circle #2");
y2 = Integer.parseInt(input);
input = JOptionPane.showInputDialog("Enter the radius of circle #1");
r1 = Integer.parseInt(input);
input = JOptionPane.showInputDialog("Enter the radius of circle #2");
r2 = Integer.parseInt(input);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void paint(Graphics g)
{
super.paint(g);
g.drawOval(x1-r1, y1-r1, r1*2, r1*2);
g.drawOval(x2-r2, y2-r2, r2*2, r2*2);
g.drawLine(x1, y1, x2, y2);
double areaCircle1 = Math.PI*r1*r1;
double circumferenceCircle1 = 2*Math.PI*r1;
double areaCircle2 = Math.PI*r2*r2;
double circumferenceCircle2 = 2*Math.PI*r2;
g.drawString(String.format("Area = %1.3f", areaCircle1), x1, y1);
g.drawString(String.format("Circumference = %1.3f", circumferenceCircle1), x1, y1+10);
g.drawString(String.format("Area = %1.3f", areaCircle2), x2, y2);
g.drawString(String.format("Circumference = %1.3f", circumferenceCircle2), x2, y2+10);
double distance = Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
{
Program1 frame = new Program1();
frame.setSize(500, 500);
frame.setVisible(true);
}
}
Attachment 1033
The problem I am having is when I run the program, I need it to put the stats at the top of the window and not in the center of the circles like the image I included says. Can someone post the code for what I need to change please?
[If what I'm asking is confusing, I'll try to re-word it. If you run the program and see that it puts the stats in the center of the circles, I need to know what to change to get it to put the stats at the top of the window, but keep the circles the same like the picture I've included shows.]
Re: need help finishing up this project but I don't know the code...please help
Quote:
I need it to put the stats at the top of the window and not in the center of the circles
Are you talking about the values of the x,y variables when you call drawString()?
One way to see what the x,y values should be is to take a piece of grid paper, draw on it with a pencil where you want the Strings and circles to be and then see what the x,y values should be.
Re: need help finishing up this project but I don't know the code...please help
Quote:
Originally Posted by
Norm
Are you talking about the values of the x,y variables when you call drawString()?
One way to see what the x,y values should be is to take a piece of grid paper, draw on it with a pencil where you want the Strings and circles to be and then see what the x,y values should be.
If you click on the image I included, it shows the words "They do not overlap" and "Circle 1's stats:" and the rest of that all at the top of the window. That's what I need. But as of right now, it is being placed in the center of the circles.
Re: need help finishing up this project but I don't know the code...please help
Do you understand what I wrote in my last post? If not please ask about what you do not understand. I thought I explained exactly what was wrong and how you could fix it.
Re: need help finishing up this project but I don't know the code...please help
Quote:
Originally Posted by
Norm
Do you understand what I wrote in my last post? If not please ask about what you do not understand. I thought I explained exactly what was wrong and how you could fix it.
No sorry, I don't understand because this is my first assignment and I don't know much about Java programming yet. But yes, I'm talking about when you call drawString() and type in the messages, I need them at the top left hand corner of the window that pops up and they are going to the center of the circles right now.
Re: need help finishing up this project but I don't know the code...please help
The x,y values in the drawString method give the location to draw the String.
Where do you want to draw the Strings? Change the x,y in the drawString method to be where you want them to be drawn on the screen.
Re: need help finishing up this project but I don't know the code...please help
Quote:
Originally Posted by
Norm
The x,y values in the drawString method give the location to draw the String.
Where do you want to draw the Strings? Change the x,y in the drawString method to be where you want them to be drawn on the screen.
Alright, I kind of figured out what you meant. I changed the values of the x and y, but I still have one more problem. I need each stat on a separate line, so that its on 3 lines. I know about the \n escape character but I'm not sure if that's for sure what I need to do or also even how to use it in my code?
Re: need help finishing up this project but I don't know the code...please help
\n has no meaning for the drawString method. You have to position each line yourself.
Re: need help finishing up this project but I don't know the code...please help
Alright, and another thing I noticed. As I'm re-positioning the words, the circles aren't moving and so some of the words are on the circle?
Re: need help finishing up this project but I don't know the code...please help
The shapes and Strings will draw where you draw them. The x,y values control that. If you want some shape in a different location, you need to change its x,y.
The location of everything is up to you.
Re: need help finishing up this project but I don't know the code...please help
Ok, I moved the text to where I wanted it. Now where in my code do I change the numbers to move the circles because the words are overlapping the circles.
Re: need help finishing up this project but I don't know the code...please help
Quote:
where in my code do I change the numbers to move the circles
Look at the code that is drawing the circles. Change the variables it uses to control where the circles are drawn.
Look at the API doc for the Graphics class to see how to use the various drawing methods.
Java Platform SE 6
Find Graphics in the lower left, click on the link and the doc will be in the main window.