2014-12-06 107 views
0

差不多我创建了一个Shape类,与RectangleCircleTriangle延长ShapeSquare类扩展Circle。我有代码与这个主类一起工作,但我很难将它转换成GUI,因为我不知道如何做第3号以使它们聚集在一起以及如何制作g.drawOval(with given x,y & radius)draw triangle(given x,y, base and height)GUI使用的JFrame,与JPanel的绘制自定义形状

  1. Project6类将不得不延长JFrame
  2. Project6构造函数将要成立的GUI窗口。
  3. 一种新的抽象方法:public void display(Graphics g);应该被添加到基类和派生类中。
  4. 定制JPanel必须设置了一个paintComponent方法
  5. 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(); 

难道我在我的每个类的末尾添加这样的事? I.E. Circle,Square,Triangle,Rectangle class?

@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); 
} 

我是新来的图形用户界面,所以这是一个有点难以做到的事情,但是这会用于绘制形状吗?但是,我得到一个错误说非静态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(); 
} 
} 
+0

其广场延伸矩形*不圆 – 2014-12-06 21:04:26

回答

1
  1. 你的类扩展一个JFrame,它永远不会显示
  2. 你应该吸取你的形状在JPanel的paintComponent方法里面引用,添加到您的JFrame中的一个。
  3. 我会使用一个ArrayList<Shape>,而不是一个数组,因为这样我就可以添加尽可能多或更少的形状到我的集合,而不必担心空项目。
  4. 然后,我会遍历paintComponent方法覆盖中的集合,并使用Graphics2D对象绘制每个Shape。
  5. 关于你最后的问题,"Do I add something like this at the end of each of my classes? ie(Circle, square, triangle, rectangle class?..."不,不需要“绘制”方法,因为你将使用paintComponent方法来绘制图形。
+0

好吧,我不能改变数组,我只需要添加的JFrame和JPanel并显示形状,但不 – 2014-12-06 20:55:55

+0

@FalafelMan:你上面的说法混淆了我很大。请澄清。 – 2014-12-06 20:57:25

+0

好吧所以这里的说明,我已经完成除Phase4之外的所有东西,我不知道该怎么办[链接] http://www3.canyons.edu/Faculty/biblej/project6.html – 2014-12-06 21:05:19