2014-12-06 130 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