我几乎创建了一个 Shape
类,其中 Rectangle
、Circle
、Triangle
扩展了 Shape
,以及扩展 Circle
的 Square
类。我有与这个主类一起工作的代码,但是我很难将其转换为 GUI,因为我不确定如何执行第 3 步来使其组合在一起以及如何制作 g.drawOval(给定 x、y 和半径)
并绘制三角形(给定 x、y、底边和高度)
。
- Project6 类必须扩展
JFrame
类 - Project6 构造函数必须设置 GUI 窗口。
- 应将新的抽象方法:
public void display(Graphics g);
添加到基类和派生类中。 - 必须使用
paintComponent
方法设置自定义JPanel
- 新的
display(Graphics g)
方法必须在 GUI 窗口上绘制形状,并从paintComponent
方法中的循环中调用。
import javax.swing.*;
import java.awt.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Project6 extends JFrame {
private Shape [] thearray = new Shape[100];
public static void main (String [] args) {
Project6 tpo = new Project6();
tpo.run();
}
public void run () {
int count = 0;
thearray[count++] = new Circle(20, 20, 40);
thearray[count++] = new Triangle(70, 70, 20, 30);
thearray[count++] = new Rectangle(150, 150, 40, 40);
thearray[count++] = new Square(100, 100, 50, 75);
for (int i = 0; i < count; i ++ ) {
thearray[i].display();
}
int offset = 0;
double totalarea = 0.0;
while (thearray[offset] != null) {
totalarea = totalarea + thearray[offset].area();
offset++;
}
System.out.println("The total area for " + offset + " Shape objects is " + totalarea);
}
public Project6() {
JFrame frame = new JFrame();
frame.setSize(800, 700);
frame.setTitle("Shapes: Circle, Triangle, Rectangle, Square");
frame.setLocationRelativeTo(null); //Center Frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static class MyPanel extends JPanel {
public static JPanel showJPanel(Graphics g) {
panel = new MyPanel();
return panel;
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
for(int i = 0; i < thearray.length && thearray[i] != null; i++) {
thearray[i].display();
我是否在每节课的末尾添加类似的内容? IE。 圆形
、正方形
、三角形
、矩形
类?
@Override
public void draw(Graphics g) {
g.drawRect(getXPos(), getYPos(), width, height);
}
<小时/>
我无法更改数组的设置方式,但这不应该是扩展 JFrame 的类吗?
public Project6() {
JFrame frame = new JFrame();
frame.setSize(800, 700);
frame.setTitle("Shapes: Circle, Triangle, Rectangle, Square");
frame.setLocationRelativeTo(null); //Center Frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
我是 GUI 新手,所以这有点难做到,但是这对于绘制形状有用吗?但我收到一条错误消息,指出无法从静态上下文中引用非静态方法 get()
class NewPanel extends JPanel {
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawLine(Triangle.getXPos(), 0, 0, Triangle.getYPos());
g.drawLine(Triangle.getXPos(), 0, Triangle.getXPos, Triangle.getYPos());
g.drawLine(Triangle.getXPos(), Triangle.getYPos, 0, Triangle.getYPos());
g.drawRect(Rectangle.getXPos(), Rectangle.getYPos(), Rectangle.getWidth(), Rectangle.getHeight());
g.drawRect(Square.getXPos(), Square.getYPos(), Square.getWidth(), Square.getHeight());
g.drawOval(Circle.getXPos(), Circle.getYPos(), Circle.getRadius(), 10);
for(int i = 0; i < thearray.length && thearray[i] != null; i++) {
thearray[i].display();
}
}
最佳答案
- 您的类扩展了一个从不显示的 JFrame
- 您应该在 JPanel 的 PaintComponent 方法中绘制形状,该方法会添加到您的 JFrame 中。
- 我会使用
ArrayList<Shape>
,而不是数组,因为这样我就可以向我的集合中添加任意数量的形状,而不必担心空项目。 - 然后,我将迭代 PaintComponent 方法重写中的集合,并使用 Graphics2D 对象绘制每个 Shape。
- 关于您的最后一个问题,
"Do I add something like this at the end of each of my classes? ie(Circle, square, triangle, rectangle class?..."
不,不需要“draw”方法,因为您将使用paintComponent 方法来进行绘图。
关于java - GUI 使用 JFrame 和 JPanel 绘制自定义形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27328866/