【发布时间】:2014-12-06 06:06:49
【问题描述】:
我几乎创建了一个Shape 类,其中Rectangle、Circle、Triangle 扩展Shape,以及一个Square 类扩展Circle。我有使用这个主类的代码,但是我很难将它转换为 GUI,因为我不确定如何执行第 3 项以使其结合在一起以及如何制作 g.drawOval(with given x,y & radius) 和 draw triangle(given x,y, base and height) .
- 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。 Circle, Square, Triangle, Rectangle 上课?
@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();
}
}
【问题讨论】:
-
它的正方形扩展了 Rectangle* 而不是圆形
标签: java swing graphics jframe jpanel