gpt4 book ai didi

java - 从 main 方法传递一个字符串以在同一类文件中进行绘制

转载 作者:行者123 更新时间:2023-12-01 04:31:44 25 4
gpt4 key购买 nike

我最近开始学习java编程,并且开始做一些图形。我正在为图形制作 GUI,但在传递使用 JOptionPane.showInputDialog 从数组中选择的形状时遇到了一些麻烦。是否建议为我想要绘制的每个 2D 形状创建方法,然后将 Paint 方法放在另一个类文件中并重载它?请给我一些帮助。我的代码如下所示。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Rectangle;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class test extends JFrame {

String x1; // starting x co-ordinate
String y1; // starting y co-ordinate
String r1; // radius for circle
String l1; // length for shape
String h1; // height for shape
String draw;

int x = 0;
int y = 0;
int l = 0;
int h = 0;
int r = 0;

/**
* Launch the application.
*/
public static void main(String[] args) {

EventQueue.invokeLater(new Runnable() {
public void run() {
try {
test frame = new test();
JPanel panel = new JPanel() {
@Override
public Dimension getPreferredSize() {
return new Dimension(2000, 2000);
}
};
// frame.getContentPane().add(new JScrollPane(panel),
// BorderLayout.CENTER);
frame.setVisible(true);
frame.setSize(1200, 600);

} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the frame.
*/
public test() {

String[] shapes = { "Circle", "Ellipse", "Line", "Rectangle" };

String drawShape = (String) JOptionPane.showInputDialog(null,
"Select a shape to be drawn: ", "Drawing 2D Shapes",
JOptionPane.QUESTION_MESSAGE, null, shapes, shapes[0]);

if (drawShape == "Circle") {
System.out.println("Shape selected is: " + drawShape);

x1 = JOptionPane.showInputDialog(null,
"Input center (x value): ");
y1 = JOptionPane.showInputDialog(null,
"Input center (y value): ");
r1 = JOptionPane.showInputDialog(null, "Input radius: ");

// parsing the variables from String to Integer
x = Integer.parseInt(x1);
y = Integer.parseInt(y1);
r = Integer.parseInt(r1);
}

if (drawShape == "Ellipse") {
System.out.println("Shape selected is: " + drawShape);

x1 = JOptionPane.showInputDialog(null,
"Input x starting point: ");
y1 = JOptionPane.showInputDialog(null,
"Input y starting point: ");
l1 = JOptionPane.showInputDialog(null, "Please enter Width: ");
h1 = JOptionPane
.showInputDialog(null, "Please enter Height: ");

// parsing the variables from String to Integer
x = Integer.parseInt(x1);
y = Integer.parseInt(y1);
l = Integer.parseInt(l1);
h = Integer.parseInt(h1);
}

if (drawShape == "Line") {
System.out.println("Shape selected is: " + drawShape);

x1 = JOptionPane.showInputDialog(null, "Input point 1: ");
y1 = JOptionPane.showInputDialog(null, "Input point 2: ");
l1 = JOptionPane.showInputDialog(null, "Input point 3: ");
h1 = JOptionPane.showInputDialog(null, "Input point 4: ");

// parsing the variables from String to Integer
x = Integer.parseInt(x1);
y = Integer.parseInt(y1);
l = Integer.parseInt(l1);
h = Integer.parseInt(h1);
}

if (drawShape == "Rectangle") {
x1 = JOptionPane.showInputDialog(null,
"Input x starting point: ");
y1 = JOptionPane.showInputDialog(null,
"Input y starting point: ");
l1 = JOptionPane.showInputDialog(null, "Please enter Width: ");
h1 = JOptionPane
.showInputDialog(null, "Please enter Height: ");

// parsing the variables from String to Integer
x = Integer.parseInt(x1);
y = Integer.parseInt(y1);
l = Integer.parseInt(l1);
h = Integer.parseInt(h1);
}

}

public void paint(Graphics g) {
super.paint(g);

// drawing the shape
Graphics2D g2 = (Graphics2D) g;

Rectangle r = new Rectangle(x, y, l, h);
g2.draw(r);


/*
* Ellipse2D.Double circle = new Ellipse2D.Double(x, y, r, r);
* g2.draw(circle); g2.drawString("This is a Circle", (x + (r / 2)), y);
*
* g2.setColor(new Color(0, 0, 255));
* System.out.println("Shape selected is: " + type);
*
* Ellipse2D.Double ellipse = new Ellipse2D.Double(x, y, l, h); //
* defining // the // object // graphically g2.draw(ellipse);
* g2.drawString("This is a ellipse", x, y);
*
* g2.setColor(new Color(0, 0, 255));
* System.out.println("Shape selected is: " + type);
*
* Line2D.Double line1 = new Line2D.Double(x, y, l, h); g2.draw(line1);
* g2.drawString("Point 1", x, y); g2.drawString("Point 2", l, h);
*/

}

}

最佳答案

有几种不同的选择。就像你有 x,y 等作为类变量一样,让你的drawShape成为一个类变量。然后在你的绘制方法中,你就会知道要画什么。

有时您可能想要绘制多个形状。

我会为您想要绘制的每个形状创建一个类。它们都应该实现一个接口(interface),该接口(interface)具有一个“draw”方法,该方法将 Graphics 作为参数。

假设您的界面名为 MyShapeI。您的类将具有 MyShapeI 对象的集合。

然后,您无需设置类变量,而是创建 MyCircle 的新实例,并设置其 x、y、r 字段,并将其添加到您的集合中。

最后在绘制中,您将循环遍历集合,并调用每个对象的绘制方法。

这给了你无限的可能性。您可以有一个 MyElephant 类,它知道如何在指定的 x,y 点绘制大象。

关于java - 从 main 方法传递一个字符串以在同一类文件中进行绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17911223/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com