关于您的评论来改变添加公共显示(图形克)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