關於您的評論來改變添加公共顯示(圖形克)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); }
當我的意思是通過將圖形GI意味着將其添加到thearray [i]中。顯示器(圖形G),我還需要顯示器()閱讀,但林不知道如何把兩者結合在一起,因爲它只是說它不能找到圖形g符號 – 2014-12-07 00:19:06