0

我正在尝试在 JFrame 内的 MainPanel 内的 JPanel 内绘制一个矩形。在主类中,我初始化了一个 RoomView:

roomView = new RoomView(newropc,"Default");

然后我执行:

private void init_panels() {
    mainPanel.setLayout(new BorderLayout());
    mainPanel.add(toolsPanel,BorderLayout.SOUTH);
    mainPanel.add(roomView,BorderLayout.EAST);
    mainPanel.add(libraryPanel,BorderLayout.WEST);
}

然后课堂 roomview 执行:

public class RoomView extends JPanel{
    private RoomPController rpc;
    private double w, h;
    int qFactor;
    Vector v;
    String roomName;
    DrawingPanelVector dpv;


public RoomView(RoomPController newRpc, String newRoomName) {
setPreferredSize(new Dimension(600,350));
    rpc = newRpc;
w = 0;
h = 0;
qFactor = 1;
v = new Vector();
roomName = newRoomName;
    v.add(new GraphicRectangle(0, 0, (int)(w*qFactor), (int)(h*qFactor), Color.black));
    dpv = new DrawingPanelVector(v);
    add(dpv);
    setVisible(true);
    getPreferredSize();
}

public void RepaintRoom() {
    Scanner c = new Scanner(System.in);
    v = new Vector();
    w = rpc.getRoomW(roomName);
    h = rpc.getRoomL(roomName);
    System.out.println("Room w = " + w + " Room h = " + h);
    System.out.println("Height = " + getHeight() + " Width = " + getWidth());
    if ((double)getHeight()/h < (double)getWidth()/w) qFactor = (int)((double)getHeight()/h);
    else qFactor = (int)((double)getWidth()/w);
    System.out.println("qFactor = " + qFactor);
    v.add(new GraphicRectangle(0, 0, (int)(w*qFactor), (int)(h*qFactor), Color.black));
    dpv = new DrawingPanelVector(v);
    add(dpv);
    setVisible(true);
}
}

最后,DrawingPanelVector 是:

public class DrawingPanelVector extends JPanel {
    // variable miembro
    private Vector v;
    // constructor
    public DrawingPanelVector(Vector va) {
        super(new FlowLayout());
        setPreferredSize(new Dimension(600,350));
        v = va;
        getPreferredSize();
    }
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Drawable dib;
        Iterator it;
        it = v.iterator();
        while (it.hasNext()) {
        dib = (Drawable) it.next();
        dib.draw(g);
    }
}
}

它什么也不做,MainPanel 上也没有出现任何内容。
这让我发疯,我必须在 7 小时内将此代码提供给我的老师。

4

1 回答 1

1

在我看来,最初的问题是......

你打电话...

roomView = new RoomView(newropc,"Default");

哪个叫...

public RoomView(RoomPController newRpc, String newRoomName) {
    setPreferredSize(new Dimension(600,350));
    rpc = newRpc;
    w = 0;
    h = 0;
    qFactor = 1;
    v = new Vector();
    roomName = newRoomName;
    getPreferredSize();
}

这不会为组件添加任何内容。没有DrawingPaneVector添加到您的RoomView,这意味着不会绘制任何内容...

额外的

你违反了油漆链。绘制过程是一个复杂的系统,涉及许多子方法和委托。通过覆盖该paint方法并且未能调用super.paint您已经绕过了整个绘画过程。

相反,您应该压倒一切paintComponent并确保您正在打电话super.paintComponent以确保paintComponent完成需要做的工作......

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    Drawable dib;
    Iterator it;
    it = v.iterator();
    while (it.hasNext()) {
        dib = (Drawable) it.next();
        dib.draw(g);
    }
}

查看在 AWT 和 Swing中执行自定义绘画和绘画以了解更多详细信息。

不鼓励使用setPreferredSize,主要问题是这很容易被程序的另一部分更改。相反,您应该覆盖getPreferredSize自定义组件。

更多附加

我建议放弃使用,Vector而是使用List,实例化ArrayList. 的唯一好处Vector是它是synchronized,但我看不出你想要这个的任何理由。

我还建议阅读Generics。这将使您在长期错误中的生活更轻松,因为您不需要投射对象。

使用和泛型的DrawingPanelVector面板的可能实现......List

public class DrawingPanelVector extends JPanel {

    private List<Drawable> drawables;

    public DrawingPanelVector() {
        drawables = new ArrayList<>(25);
    }

    public void add(Drawable drawable) {
        drawables.add(drawable);
        repaint();
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(600, 350);
    }

    // redefinición del método paint()
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        for (Drawable drawable : drawables) {
            drawable.draw(g);
        }
    }
}
于 2013-05-29T00:17:00.413 回答