gpt4 book ai didi

java - Canvas 显示问题

转载 作者:行者123 更新时间:2023-11-30 06:00:26 24 4
gpt4 key购买 nike

目前我正在尝试拥有 JFrame其中包含两个 JPanel组件-第一个JPanel组件旨在容纳用户可以单击以与程序交互并将图像绘制到屏幕上的各种按钮。第二个,这就是麻烦开始的地方,包含实际的Canvas。我试图绘制的对象。

我遇到的问题是第二个面板实际上并未被吸引。我尝试了各种其他方法和检查,但一切都表明当前代码应该可以工作。

作为测试,我只是想做 fillRect()到绘图面板上,但无济于事。这是我的代码:

//创建组件和JFrame:

    public static void createDisplay() {

JFrame f = new JFrame();//When JFrame flowlayout manager removed, the rectangle was drawn.
c = new Canvas();

width = 500;
height = 500;

f.setLayout(new FlowLayout());

f.add(addButtons()); //private method that covers the button panel

JPanel drawPanel = new JPanel();


drawPanel.add(c);

c.setFocusable(false);

f.add(drawPanel);

f.setPreferredSize(new Dimension(height, width));

lstnr = new Listeners();

f.addKeyListener(lstnr);
f.addMouseListener(lstnr);
f.addMouseMotionListener(lstnr);

f.setLocationRelativeTo(null);

f.pack();
f.setVisible(true);


}

以及在屏幕上实际绘图的代码:

private void render() {

bs = c.getBufferStrategy();

if(bs == null) {
c.createBufferStrategy(3);
return;
}


do {
do {
g = bs.getDrawGraphics();
l.setGraphicsObj(g);
g.clearRect(0, 0, width, height);

for(Symbols s : l.getSymbolsArray());

s.renderSymbols();
}
g.fillRect(0, 0, 50, 50); //Just to test & make sure it works

g.dispose();

}while(bs.contentsRestored());

bs.show();
}while(bs.contentsLost());

}

现在我想再次强调,当我删除 f.setLayoutManager(new FlowLayout()); 时,绘制了矩形摘自createDisplay()方法。非常感谢任何帮助。

最佳答案

  1. 你们的绘画系统相互冲突。那个被用来画BufferStrategy Swing 使用它来绘制其组件
  2. 即使在最好的情况下,混合重型和轻型组件也会出现问题,您应该尽一切努力减少重叠
  3. 一个Canvas默认首选大小为0x0 ,这就是FlowLayout将用于确定如何最好地布局

“可能”的解决方案可能是专注于放置 Canvas在它自己的位置内并分隔 Swing 元素。

您仍然遇到麻烦,因为您需要管理两个不同线程上下文之间的数据

import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.image.BufferStrategy;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

public static void main(String[] args) {
new Test();
}

public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}

PaintPane paintPane = new PaintPane();

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new ButtonPane(), BorderLayout.WEST);
frame.add(paintPane);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);

paintPane.start();
}
});
}

public class ButtonPane extends JPanel {

public ButtonPane() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.weightx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = GridBagConstraints.REMAINDER;
for (int index =0; index < 5; index++) {
add(new JButton(Integer.toString(index)), gbc);
}
}

}

public class PaintPane extends Canvas {

private Thread renderThread;
private AtomicBoolean running = new AtomicBoolean(true);

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

public void start() {
if (renderThread != null) {
return;
}

running.set(true);
renderThread = new Thread(new Runnable() {
@Override
public void run() {
while (running.get()) {
render();
try {
Thread.sleep(5);
} catch (InterruptedException ex) {
}
}
}
});
renderThread.start();
}

public void stop() {
if (renderThread != null) {
return;
}
running.set(false);
renderThread.interrupt();
try {
renderThread.join();
} catch (InterruptedException ex) {
}
renderThread = null;
}

protected void render() {
BufferStrategy bs = getBufferStrategy();

if (bs == null) {
createBufferStrategy(3);
return;
}

do {
do {
Graphics g = bs.getDrawGraphics();
// :/
//l.setGraphicsObj(g);
g.clearRect(0, 0, getWidth(), getHeight());

//for (Symbols s : l.getSymbolsArray()) {
// // This is where g should be passed
// s.renderSymbols();
//}
g.setColor(Color.BLUE);
g.fillRect(0, 0, 50, 50); //Just to test & make sure it works
g.dispose();
} while (bs.contentsRestored());
bs.show();
} while (bs.contentsLost());
}

}

}

关于java - Canvas 显示问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52351931/

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