java - 通过继承绘制Arc2D.Float

标签 java swing

所以我有一个继承Arc2D.Float的类:

package Simon;

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Arc2D;
import javax.swing.JComponent;

public class SimonLight extends Arc2D.Float implements ActionListener{

    public Color defCol, lightUpCol, color; //Colors
    public enum Orientation {leftU, leftD, rightU, rightD};
    Orientation orient;

    public SimonLight(Color defCol, Color lightUpCol, Orientation orient, int x, int y){
        super.x = x;
        super.y = y;
        super.width = 200;
        super.height = 200;
        super.start = 90;
        super.extent = 90;
        this.defCol = defCol;
        this.color = defCol;
        this.lightUpCol = lightUpCol;
        this.orient = orient;
    }
    public void actionPerformed(ActionEvent e){
        //Does nothing atm
        color = lightUpCol;
    }
    public Color getColor(){
        return color;
    }
}

但是在构造函数中,我尝试将弧类型设置为 Arc2D.PIE ,但父类(super class)中没有变量允许我更改它。有谁知道如何设置类型吗?

最佳答案

您应该考虑使用 super 构造函数......

public SimonLight(Color defCol, Color lightUpCol, Orientation orient, int x, int y){
    super(x, y, 200, 200, 90, 90, Arc2D.PIE);
    this.defCol = defCol;
    this.color = defCol;
    this.lightUpCol = lightUpCol;
    this.orient = orient;
}

这通常是更好的做法,以防父类(super class)需要在内部设置这些值;)

关于java - 通过继承绘制Arc2D.Float,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32108604/

相关文章:

java - GAE 数据存储 - 无法在 Windows 上以编程方式启动 LocalDevelopmentDatastore

java - 如何以编程方式在android Q中获取IMSI?

java - 引用动态创建的 JButton

java - 如何找出组件调整大小完成时的实例

java - 如何从 JPanel 加载/保存图像

java - jscrollpane 滚动面板

java - Google editboix 给出 InvalidElementStateException :Element is disabled and so may not be used for actions Command duration

java - ibatis和java中如何处理多个表的sql映射

java - 如何在 Netbeans 中使用框架绘制可选决策树

java - 如何在不删除以前绘制的东西的情况下重新绘制