窗框覆盖图形内容 [英] Window frame covering graphics content
问题描述
这是我在这里发表的第一篇文章,我有一个看似真的很吵的问题,但这在过去一小时左右让我很烦恼。
this is my first post here and I have a question that seems really nooby, but this has been troubling me for the past hour or so.
我正在制作一个带有JPanel的简单JFrame,但Windows 7边框似乎阻止了我对面板部分的看法。例如,如果我在坐标0,0处绘制一个小方块,它将不会出现,我怀疑它在窗框后面。
I'm making a simple JFrame with a JPanel in it, but the Windows 7 border frame appears to be blocking my view of parts of the panel. For instance, if I draw a little square at coordinate 0,0, it will not appear and I suspect it's behind the window frame.
我已经尝试过乱搞pack,setsize,setpreferred size,setresizable和不同的布局,但我不能让它显示前20个像素左右!
I've tried messing around with pack, setsize, setpreferred size, setresizable, and different layouts, but I can't get it to show the top 20 pixels or so!
这就是我所拥有的:
public RedSunGame() {
super("Red Sun");
rs = new JPanel(new BorderLayout(), true);
rs.setPreferredSize(new Dimension(WIDTH, HEIGHT));
add(rs, "Center");
setPreferredSize(new Dimension(WIDTH, HEIGHT));
pack();
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
编辑:
感谢您的所有回复,对于缺少信息感到抱歉:)
Thanks for all of your replies, sorry for the lack of info :)
我正在使用双重缓冲策略书。 gameRender和paintScreen属于标准游戏循环。我的RedSunGame类扩展了JFrame。除上述之外,您可以要求的所有相关代码:
I'm using a double buffer strategy I saw in a book. gameRender and paintScreen are in a standard game loop. My RedSunGame class extends JFrame. All the relevant code you could ask for in addition to above:
private static final int WIDTH = 500;
private static final int HEIGHT = 500;
private JPanel rs;
private Graphics2D g2d;
private Image dbImage;
private void gameRender() {
//create buffer
if (dbImage == null){
dbImage = createImage(WIDTH, HEIGHT);
g2d = (Graphics2D)dbImage.getGraphics();
}
//clear screen
g2d.setColor(Color.white);
g2d.fillRect(0, 0, WIDTH, HEIGHT);
g2d.setColor(Color.blue);
g2d.setFont(font);
g2d.drawString("FPS: " + FPS, 0, HEIGHT);
g2d.fillRect(30, 30, 10, 10);
}
private void paintScreen() {
Graphics g;
try {
g = getGraphics();
if ((g != null) && (dbImage != null))
g.drawImage(dbImage, 0, 0, null);
Toolkit.getDefaultToolkit().sync();
g.dispose();
}
catch (Exception e)
{ System.out.println("Graphics context error: " + e); }
}
使用我当前的设置,它看起来像这样。
http://i.imgur.com/qaabC.png
With my current settings it looks like this. http://i.imgur.com/qaabC.png
如果我有g2d.fillRect(30,30,10,10)会发生这种情况,唯一的变化是坐标30,30而不是0,0。它绝对隐藏在边界之上。
http://i.imgur.com/uzfFe.png
This is what happens if I have g2d.fillRect(30, 30, 10, 10), the only change being the coordinates 30,30 instead of 0,0. It's definitely hiding behind the border up top. http://i.imgur.com/uzfFe.png
此外,将其设置为BorderLayout.CENTER似乎在任何这些情况下都没有区别。
Also, setting it to BorderLayout.CENTER doesn't seem to make a difference in any of these cases.
(抱歉它不会让新用户发布图片)
(sorry it won't let new users post images)
编辑:
我想通了。我正在直接绘制到JFrame。 @Guillaume Polet我明白为什么你不应该覆盖JFrames的绘制方法,因为它绘制到框架而不是实际显示内容的面板!谢谢
I figured it out. I was drawing directly to the JFrame. @Guillaume Polet I see why you shouldn't override the paint method of JFrames as it draws to the frame and not the panel that should actually display content!! Thanks
推荐答案
这是一个示例代码,展示了如何实现目标。尝试找出与您的代码的差异,找出错误:
Here is a sample code that shows how your goal can be achieved. Try to spot the differences with your code to find what is wrong:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class RedSunGame {
private static final int SQUARE_SIZE = 20;
private JPanel rs;
private JFrame frame;
private void initUI() {
frame = new JFrame("Red Sun");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
rs = new JPanel(new BorderLayout()) {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.YELLOW);
g.fillRect(0, 0, SQUARE_SIZE, SQUARE_SIZE);
}
@Override
public Dimension getPreferredSize() {
Dimension preferredSize = super.getPreferredSize();
// Let's make sure that we have at least our little square size.
preferredSize.width = Math.max(preferredSize.width, SQUARE_SIZE);
preferredSize.height = Math.max(preferredSize.height, SQUARE_SIZE);
return preferredSize;
}
};
frame.add(rs);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
RedSunGame redSunGame = new RedSunGame();
redSunGame.initUI();
}
});
}
}
这篇关于窗框覆盖图形内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!