我正在尝试在JFrame内的MainPanel内的JPanel中绘制一个Rectangle。 在Main Class中,我初始化一个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);
}
然后课堂视图执行:
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);
}
}
AND,最后,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个小时内将这些代码交给老师。
答案 0 :(得分: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);
}
}
请查看Performing Custom Painting和Painting in AWT and Swing了解详情。
不建议使用setPreferredSize
,主要问题是程序的另一部分很容易改变。相反,您应该覆盖自定义组件的getPreferredSize
。
更多附加
我建议停止使用Vector
,而是使用List
,实例化ArrayList
的实现。 Vector
的唯一好处是它是synchronized
,但我看不出你为什么要这样做。
我还建议您阅读Generics。这将使你的生活变得更容易,因为你不需要施放物体。
使用DrawingPanelVector
和泛型的<{1}}小组的可能实施方式......
List