【问题标题】:What value to pass the paint(Graphics) function? (AWT)传递paint(Graphics) 函数的值是多少? (AWT)
【发布时间】:2018-09-26 14:23:33
【问题描述】:

我正在尝试使用 AWT 进行一些简单的 GUI 操作。 我现在坚持的一点是如何调用绘制函数(我正在尝试为每个形状/对象创建一个方法)。我根本无法通过阅读 API 来判断“Graphics”指的是什么参数,我需要在调用时传递函数。看其他例子,我觉得我什至不需要专门称呼它。我很困惑..

另外,是否可以将 drawRect(int ... int) 函数传递给我的实际对象中的值,或者我是否必须诉诸硬编码值,就像它现在在范围内定义的方式一样?

import java.awt.*;
import java.awt.event.*;
import java.awt.Graphics.*;

class AWTFigur extends Panel{
Figur f;

AWTFigur (Figur f){
        this.f = f;
}

public void paint(Graphics g){
    f.paint(g);
}

public Dimension getPreferredSize(){
    return new Dimension (f.getWidth()+2, f.getHeight()+2);
}

public void paintRect(Graphics g){
    super.paint(g);
    g.drawRect(0, 0, 10, 10);

}


public static void main(String args[]){
    Frame F=new Frame();
    F.setLayout(new FlowLayout());
    F.addWindowListener(new WindowAdapter()
        {public void windowClosing(WindowEvent we){System.exit(0);}});
    AWTFigur P1=new AWTFigur(new Square(Integer.parseInt(args[0])));
    F.add(P1);
    AWTFigur P2=new AWTFigur(new Circle(Integer.parseInt(args[1])));
    F.add(P2);
    F.pack();
    F.setVisible(true);

}

}

circle类的代码位:

public class Circle extends Figur{
private int radius;
private int diameter;
private final double pi = 3.14159265358979323846264338327950288419;

public Circle(int radius){
    if (radius>0)
        this.radius=radius;
    else{
        this.radius=2;
    }
}

@Override
double getCircumference(){
    return (this.radius*this.pi*2);
}

@Override
double getArea(){
    return this.pi*this.radius*this.radius;
}

int getRadius(){
    return this.radius;
}

int getDiameter(){
    return this.diameter;
}

void setRadius(int radius){
    if (radius > 0)
        this.radius=radius;
    else{
        this.radius=2;
    }
}

void setDiameter(int diameter){
    if (diameter == (radius*2))
        this.diameter=diameter;
    else{
        this.diameter=radius*2;
    }
}

}

对于方形类:

public class Square extends Figur{
private int height;
private int width;

public Square(int height){
    if (height >0){
        this.height=height;
        this.width=height;
    }
    else{
        this.height=2;
        this.width=2;
    }
}

@Override
double getCircumference(){
    return this.height*4;
}

@Override
double getArea(){
    return this.width*2;
}


@Override
int getHeight(){
    return this.height;
}

@Override
int getWidth(){
    return this.width;
}

void setHeight(int height){
    if (height > 0)
        this.height=height;
    else{
        this.height=2;
    }
}

void setWidth(int width){
    if (width > 0)
        this.width=width;
    else{
        this.width=2;
    }
}

}

还有超类 Figur:

 import java.awt.*;

class Figur{
double getCircumference(){return 0;}
double getArea(){return 0;}
int getHeight(){return 0;}
int getWidth(){return 0;}
public void paint(Graphics g){}
}

【问题讨论】:

    标签: java user-interface graphics awt paint


    【解决方案1】:

    传递paint(Graphics) 函数的值是多少? (AWT)

    正如你后面的话所暗示的,你没有。只需调用 repaint(),AWT/Swing 的 GUI 工具包将确保使用有效的 Graphics 实例调用 paint(Graphics) 方法。

    另外,是否可以将 drawRect(int ... int) 函数传递给我的实际对象中的值,或者我是否必须诉诸硬编码值,就像它现在在范围内定义的方式一样?

    第一堂课是这样的:

    class AWTFigur extends Panel{
    Figur f;
    

    这里,f 是 Figure 类型的类的属性,该类指定方法 public void paint(Graphics g){},任何扩展它的类都可以覆盖该方法。 (但如果它是 interface 或 abstract 类会更好 - 原因是任何其他实现或扩展它的类都强制提供实现。)所以在在后一种情况下,Circle 和Square 都有一个绘制方法,当它想要/需要绘制该对象时,它可以被称为具有该类型对象的任何类(例如AWTFigur)。

    鉴于该课程具有以下内容,您似乎(至少)对此有一个微不足道的理解:

    public void paint(Graphics g){
        f.paint(g);
    } 
    

    这将导致Figure 被绘制。但是main(..) 方法表明您的理解似乎有些模糊。

    Frame F=new Frame();
    F.setLayout(new FlowLayout());
    F.addWindowListener(new WindowAdapter()
        {public void windowClosing(WindowEvent we){System.exit(0);}});
    AWTFigur P1=new AWTFigur(new Square(Integer.parseInt(args[0])));
    F.add(P1);
    AWTFigur P2=new AWTFigur(new Circle(Integer.parseInt(args[1])));
    F.add(P2);
    

    这会将AWTFigur 的两个实例添加到(默认情况下)具有BorderLayout 的框架中。 Component(如Panel)只能添加到BorderLayout 的单个约束。因为添加时没有指定约束,所以它们都将被添加到CENTER(只能显示其中一个)。

    这不是这里需要的。

    需要,是AWTFigur 的单个实例,它可以绘制任意数量的Figure 对象,因此打开代码可以更改为:

    class AWTFigur extends Panel{
    ArrayList<Figur> f;
    

    ArrayList 可以容纳 许多 Figur 对象。如果那时,除了(或者可能不是)接受Figur 对象的构造函数之外,该类还提供如下方法:

    public void addFigur(Figur figur) {
        f.addElement(figur); // add the new Figur to the existing collection
        repaint(); // schedule a call to paint(Graphics)
    }
    

    可以在main(..) 中调用该方法,以将第二个或第三个图形添加到数组列表所持有的集合中。

    然后在paint方法中,可以迭代整个集合并依次绘制每个集合。比如:

    public void paint(Graphics g){
        for (Figure figur : f) {
            g.paint(g);
        }
    }
    

    此类内容将在Performing Custom Painting 中介绍。仔细阅读课程,阅读示例并尝试每个示例,完成后应该会更加清晰。

    最后一点(或两个):

    1. 请学习常见的 Java 命名法(命名约定 - 例如EachWordUpperCaseClass、firstWordLowerCaseMethod()、firstWordLowerCaseAttribute,除非它是 UPPER_CASE_CONSTANT)并始终如一地使用它。
    2. 为什么要使用 AWT?请参阅 this answer 了解放弃 AWT 组件以支持 Swing 的许多充分理由。
    3. private final double pi = 3.14159265358979323846264338327950288419; 改用 Math.PI

    【讨论】:

      猜你喜欢
      • 2012-12-07
      • 2020-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多