2014-12-06 85 views
0

关于您的评论来改变添加公共显示(图形克)JAVA,GUI的JPanel,JFrame中,的paintComponent,图形

[链接] http://www3.canyons.edu/Faculty/biblej/project6.html

1)Project6类将不得不延长JFrame类 2.)Project6构造函数将不得不设置GUI窗口。 3.)一种新的抽象方法:public void display(Graphics g);应该被添加到基类和派生类 4.)自定义JPanel必须使用paintComponent方法设置 5.)新显示(Graphics g)方法必须在GUI窗口上绘制图形并从paintComponent方法中的一个循环

public class Project6 extends JFrame { 

//project6 constructor without parameters to set up new JFrame 
public Project6() { 
add(new NewPanel()); 
} 
class NewPanel extends JPanel { 
@Override 
protected void paintComponent(Graphics g) { 
super.paintComponent(g); 

//所以我需要在这里添加Graphics g吗?或没有?

for(int i = 0; i < thearray.length && thearray[i] != null; i++) { 
thearray[i].display(**Graphics g**); 
}}} 

public static void main (String [] args) { 
JFrame frame = new JFrame(); 
frame.setSize(800, 700);       
frame.setTitle("Shapes"); 
frame.setLocationRelativeTo(null);     //Center Frame 
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
frame.setVisible(true); 

这里是一个类的例子,我是否将它添加到这样的结尾?我是否需要向Shape父类添加一个公共抽象void display(Graphics g)?以及它如何在project6类中调用?

public class Rectangle extends Shape { 
private int width; 
private int height; 

public Rectangle() { 
    setWidth(0); 
    setHeight(0); 
    setXPos(0); 
    setYPos(0);} 

public Rectangle(int xPos, int yPos, int height, int width) { 
    setWidth(xPos); 
    setHeight(yPos); 
    setXPos(height); 
    setYPos(width);} 

public int getWidth() { 
    return width;} 

public void setWidth(int width) { 
    this.width = width;} 

public int getHeight() { 
    return height;} 

public void setHeight(int height) { 
    this.height = height;} 

@Override 
public void display() { 
    System.out.println("Rectangle: (" + getXPos() + ", " + getYPos() + ") " + " Height: " + height + " Width: " + width);} 

@Override 
public void display(Graphics g) { 
    g.drawRect(getXPos(), getYPos(), width, height); } 
+0

当我的意思是通过将图形GI意味着将其添加到thearray [i]中。显示器(图形G),我还需要显示器()阅读,但林不知道如何把两者结合在一起,因为它只是说它不能找到图形g符号 – 2014-12-07 00:19:06

回答

3

新抽象方法:公共无效显示(图形克);应加入到基类和派生类

,因为我注意到,你在呼唤thearray[i].display();display是为了有一个参数,您没有正确完成此步骤。

如果正确地创建display方法,则都交给图形对象,可使用例如:

class Line extends Shape { 
    int x1, y1, x2, y2; 

    @Override 
    public void display(Graphics g) { 
     g.drawLine(x1, y1, x2, y2); 
    } 
} 
+0

好吧,即时通讯仍然是新的网站,真的找不到方式来回复新的代码,所以生病编辑我的帖子向你展示其中一个类,看看我是否做得对,因为我想我尝试了这一点,我得到一个错误,说它无法识别(图形g) – 2014-12-07 00:07:55

+0

*“ CA不识别...“*您可能需要'import java.awt.Graphics;'。 – Radiodef 2014-12-07 01:32:41

+0

其不能识别,其找不到符号 – 2014-12-07 01:34:39