如何绘制图形对象数组?

时间:2014-10-24 22:13:33

标签: java arrays graphics jpanel

所以我现在已经把头发拉了一段时间了,我真的可以帮忙了。我必须在超类中创建一个矩形图形对象,然后将该类扩展为一个子类,然后绘制7个所述矩形以绘制一个数字,就像你在计算器上看到的那样。我在子类中创建一个矩形数组并尝试将它们添加到面板中,但是我一直在尝试添加到面板的所有方法中都出现错误。任何帮助都将受到极大的赞赏。

这是我的超级

import javax.swing.*;
import java.awt.*;

public class Bar extends JPanel {
public Color myColor;
Graphics g;
int x;
int y;
int w;
int h;

public Bar(int newX, int newY, int newW, int newH) {
    x = newX;
    y = newY;
    w = newW;
    h = newH;
    super.paintComponent(g);
    g.setColor(getColor());
    g.fillRect(x, y, w, h);
}

public Color getColor() {
    return myColor;
}
}

然后是我的子类

import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class LED extends Bar {
public static Bar[] myLED;
public static int number;
public boolean isOn;


public LED(Bar[] myLED) {
    super(10, 10, 15, 45);
    switch(number) {
        case 0: myLED[0] = new Bar(10, 10, 15, 45);
            isOn = true;
    }
}

public void setOn(boolean isOn) {
    if(isOn = true) {
        myColor = Color.GREEN;
    }
    else 
        myColor = Color.BLACK;
}

public static Bar[] getLED() {
    return myLED;
}

public static void main(String[] a) {
    JFrame window = new JFrame("Calculator");
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setBounds(30, 30, 500, 500);
    JPanel panel = new JPanel();
    panel.add(myLED); //I'm getting an error on this line, as well as the one below this.
    window.getContentPane().add(myLED);//getContentPane().
    window.setVisible(true);
}   
}

感谢。

0 个答案:

没有答案