gpt4 book ai didi

java - 基本绘画代码。这不起作用

转载 作者:行者123 更新时间:2023-12-01 14:31:59 26 4
gpt4 key购买 nike

我试图在 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);
}
}
}

它不执行任何操作,主面板上也不会显示任何内容。
这让我抓狂,我必须在 7 小时内将这段可以运行的代码交给我的老师。

最佳答案

据我所知,最初的问题是......

你打电话...

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 中,这意味着不会绘制任何内容...

附加

你违反了油漆链。绘制过程是一个复杂的系统,涉及到许多子方法和委托(delegate)。通过重写 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 PaintingPainting in AWT and Swing了解更多详情。

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

更多附加内容

我建议放弃使用Vector,而是使用List,实例化ArrayList的实现。 Vector 的唯一好处是它是同步的,但我看不出您想要这样做的任何理由。

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

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

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);
}
}
}

关于java - 基本绘画代码。这不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16803445/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com