当前,我正在尝试拥有一个包含两个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()
片段时,绘制了矩形 。非常感谢您的帮助。
答案 0 :(得分:1)
BufferStrategy
,另一种用于Swing绘制其组件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());
}
}
}