이 문제에 대한 해결책을 찾기 위해 계속 노력해 왔지만이를 해결하는 방법을 이해할 수없는 것 같습니다. Java에서 사용자 입력을 기반으로 원을 그립니다.
- 가 입력 상자 (JOptionPane의)를 사용하여 원의 반지름의 사용자에게 질문 나는 프로그램에서 이러한 사양의 원을 그릴 수있는 간단한 프로그램을 작성하는 것을 시도하고있다.
- 사용자에게 입력 상자 (JOptionPane)에서 원의 x 및 y 좌표를 문의하십시오.
- 원의 둘레를 계산하십시오.
- 원의 면적을 계산하십시오.
- 원의 그림 아래 영역 및 원주를 표시합니다.
어떻게 든 원을 그리지 않고 원주와 면적을 계산합니다. 이 문제에 대한 해결책을 찾도록 도와 주시겠습니까?
다음은 내 코드 :
-------
package circleSquarePackage;
import circleSquarePackage.Circle;
import javax.swing.JOptionPane;
import javax.swing.JFrame;
public class CircleTester {
public static void main(String[] args)
{
JFrame frame = new JFrame();
// Display Circle in the frame
frame.setSize(600, 500);
frame.setTitle("CircleSquare");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create Circle Components
Circle round = new Circle();
frame.add(round);
frame.setVisible(true);
}
}
------- 주요
-------- 다른 클래스 ----- PEESKILLET 'S 답변에 따라 ----
package circleSquarePackage;
import javax.swing.JComponent;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Ellipse2D.Double;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Color;
import javax.swing.JOptionPane;
public class Circle extends JComponent {
// Member instance field
private Ellipse2D.Double sphere;
private int radius;
double circumference, area;
String x1; // starting x co-ordinate
String y1; // starting y co-ordinate
String r1; // radius for circle
String draw;
int x = 0;
int y = 0;
int r = 0;
// Default constructor
public Circle()
{
sphere = new Ellipse2D.Double();
}
// User defined constructor
public Circle(int xAxis, int yAxis, int rad)
{
rad = r;
xAxis = x;
yAxis = y;
sphere = new Ellipse2D.Double(xAxis, yAxis, rad, rad);
}
// Accessor methods
public double calcCircumference()
{
return circumference = 2 * Math.PI * radius;
}
public double calcArea()
{
return area = Math.PI * radius * radius;
}
// Methods
public void inputX()
{
x1 = JOptionPane.showInputDialog(null, "Input center (x value): ");
x = Integer.parseInt(x1);
}
public void inputY()
{
y1 = JOptionPane.showInputDialog(null, "Input center (y value): ");
y = Integer.parseInt(y1);
}
public void inputRadius()
{
r1 = JOptionPane.showInputDialog(null, "Input radius: ");
r = Integer.parseInt(r1);
}
public void paintComponent(Graphics g)
{
// Cast 1D to 2D graphics
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.BLUE); // set circle color to blue
g2.fill(sphere);
g2.draw(sphere);
g2.setColor(Color.BLUE);
g2.drawString("Circumference = " + calcCircumference(), 5, 450);
g2.drawString("Area = " + calcCircumference(), 200, 450);
}
}
UPDATE
012 3,516,package circleSquarePackage;
import javax.swing.JComponent;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Ellipse2D.Double;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JOptionPane;
public class Circle extends JComponent {
// Member instance field
private Ellipse2D.Double sphere;
private int radius;
double circumference, area;
// Default constructor
public Circle()
{
sphere = new Ellipse2D.Double();
}
public void setSphere(Ellipse2D.Double sphere) {
this.sphere = sphere;
repaint();
}
// User defined constructor
public Circle(int xAxis, int yAxis, int rad)
{
sphere = new Ellipse2D.Double(xAxis, yAxis, rad, rad);
}
// Accessor methods
public double calcCircumference()
{
return circumference = 2 * Math.PI * radius;
}
public double calcArea()
{
return area = Math.PI * radius * radius;
}
// Methods
public void inputX()
{
int x = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter x"));
double y = sphere.y; // why is there a double y here when it asks for x?
Ellipse2D.Double newSphere = new Ellipse2D.Double(x, y, size, size);
setSphere(newSphere);
}
public void inputY()
{
int y = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter y"));
double x = sphere.x;
Ellipse2D.Double newSphere = new Ellipse2D.Double(x, y, size, size);
setSphere(newSphere);
}
public void inputRadius()
{
// is this how I do for radius?
int r = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter radius"));
int size = r * 2;
Ellipse2D.Double newSphere = new Ellipse2D.Double(x, y, size, size);
setSphere(newSphere);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.BLUE); // set circle color to blue
g2.fill(sphere);
g2.draw(sphere);
g2.drawString("Circumference: " + calcCircumference(), 5, 490);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(600, 500);
}
}
-------- CircleTester 클래스, 당신은 sphere
당신의 인수 없음의 Circle
생성자에서
package circleSquarePackage;
import circleSquarePackage.Circle;
import javax.swing.JOptionPane;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Ellipse2D.Double;
public class CircleTester {
/*
private static Ellipse2D.Double getCircle() {
int x = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter integer for x-coordinates:"));
int y = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter integer for y-coordinates:"));
int radius = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter radius of circle:"));
int size = radius * 2;
return new Ellipse2D.Double(x, y, size, size);
}*/
public static void main(String[] args)
{
// Is there a way to call the inputX(), inputY(), and inputRadius() here?
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame();
frame.setTitle("CircleSquare");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Circle round = new Circle();
frame.add(round);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
/*
Ellipse2D.Double newCircle = getCircle();
round.setSphere(newCircle);
*/
}
});
}
}
도움 주셔서 감사합니다. 이 작품! JOptionPane을 public void inputX(), public void inputY() 및 public void inputRadius()의 Circle 클래스에 사용자 입력을 요청한 다음 CircleTester 클래스의 주 사용자에게 호출하는 방법을 궁금합니다. 위의 코드를보고 내가 말하는 것을 볼 수 있습니다. – user3768057
당신이 할 수있는 것은 각 메소드에서'JOPtionPane'을 호출하는 것입니다. 그냥 x를 얻고 싶다면 JOptionPane을 호출하고, 그 값에 따라 이전 값을 사용하고 새로운 x를 사용하여 오래된 Ellipse에서 새로운 Ellipse를 만듭니다. 그런 다음'setSphere'를 호출하십시오. 제발 잠깐 내게 –
보기 내 ** 업데이트 ** –