Java 这个代码赢了';不显示矩形,但它应该
我正在做一个JFrame并在上面画一个矩形。 它不起作用,有时完全是黑色的,有时完全是白色的,以下是我的方法 因此,render方法被调用两次,因为它第一次创建缓冲区时,也忽略了帧率,它现在是不相关的 编辑1:我解决了一个问题: 它现在正在绘制一个矩形,但有时它只是显示一个白色屏幕。我仍然需要解决这个问题 Edit2:我不仅在寻找解决方案,也在寻找问题发生的原因,所以我不仅仅是盲目地编写代码Java 这个代码赢了';不显示矩形,但它应该,java,swing,jframe,bufferedimage,graphics2d,Java,Swing,Jframe,Bufferedimage,Graphics2d,我正在做一个JFrame并在上面画一个矩形。 它不起作用,有时完全是黑色的,有时完全是白色的,以下是我的方法 因此,render方法被调用两次,因为它第一次创建缓冲区时,也忽略了帧率,它现在是不相关的 编辑1:我解决了一个问题: 它现在正在绘制一个矩形,但有时它只是显示一个白色屏幕。我仍然需要解决这个问题 Edit2:我不仅在寻找解决方案,也在寻找问题发生的原因,所以我不仅仅是盲目地编写代码 public MyFrame(String title,int width,int height
public MyFrame(String title,int width,int height)
{
super(title);
this.setSize(width,height);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setUndecorated(true);
this.addKeyListener(new KeyInput());
this.setVisible(true);
}
public void draw(Graphics2D g,int arg)
{
g.setColor(new Color(0,0,0,255));
g.fillRect(0,0,SIZE,SIZE);
g.setColor(new Color(255,255,255));
g.fillRect(0,0,50,50);
}
public void render(int arg)
{
BufferStrategy bs=this.getBufferStrategy();
if(null==bs)
{
this.createBufferStrategy(3);
}
else
{
Graphics2D g=(Graphics2D)bs.getDrawGraphics();
g.setColor(new Color(0,0,0,255));
g.fillRect(0,0,this.getWidth(),this.getHeight());
BufferedImage canvas=new BufferedImage(SIZE,SIZE,2);
int s=Math.min(this.getWidth(),this.getHeight());
g.drawImage(canvas,(this.getWidth()-s)/2,(this.getHeight()-s)/2,s,s,this);
Graphics2D g2=canvas.createGraphics();
this.draw(g2,arg);
bs.show();
g.dispose();
g2.dispose();
}
}
public static void main(String[] args)
{
Dimension d=Toolkit.getDefaultToolkit().getScreenSize();
FRAME=new MyFrame("Insert name here!",d.width,d.height,1);
FRAME.render(0);
FRAME.render(0);
}
编辑:这不再是必须的,我已经设法解决了这个问题,无论如何谢谢你的帮助。你需要的所有信息都可以在气垫船上找到
下面的代码演示了如何绘制一个全屏大小的黑色矩形
JFrame
,以及有关的一些附加信息
1) “在此处插入\u框架\u名称\u”也不应如此。请学习常见的Java命名法(命名约定-例如
EachWordUpperCaseClass
,firstWordLowerCaseMethod()
,firstWordLowerCaseAttribute
,除非它是大写常量
),并一致使用它。即使在示例代码中也是如此。2) 为了更快地获得更好的帮助,发布一个or。好的,我会更改它。请签出::Swing Graphics的入门教程我签出了,但它没有给我太多信息,我的问题是它有时显示完全黑色的屏幕,有时显示完全白色。就像鳗鱼说的那样..有点。。创建一个扩展JPanel并重写paintComponent方法的类。将类的实例添加到JFrame contentpane。正如在示例中一样,谢谢,我一定会在某个时候尝试JPanel方式。尽管我仍然需要知道为什么它会发生在JFrame中。
import java.awt.Color; //mcve should include imports
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class MyFrame extends JFrame{
public MyFrame()
{
super();
//this.setSize(width,height); use :
setExtendedState(JFrame.MAXIMIZED_BOTH);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//comment 1 : see http://docs.oracle.com/javase/tutorial/uiswing/painting/index.html
//as Hovercraft suggested
JPanel customJPanel = new MyPanel();
add(customJPanel);
// not needed to demonstrate the issue. Remove to make mcve
/*
this.setLocationRelativeTo(null);
this.setUndecorated(true); also removes frame title
this.addKeyListener(new KeyInput());
*/
pack();
setVisible(true);
}
//comment 1
class MyPanel extends JPanel {
public MyPanel() { super(); }
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
int x = getRectangleXpos(); //if postion and / or size
int y = getRectangleYPos(); //do not change, no need to
int width = getRectangleWidth(); //recalculate : move from this method
int height = getRectangleHeight();
g.drawRect(x, y , width,height);
g.setColor(Color.BLACK);
g.fillRect(x, y , width,height);
}
private int getRectangleXpos() {
//apply calculation logic if needed
return 200;
}
private int getRectangleYPos() {
//apply calculation logic if needed
return 300;
}
private int getRectangleWidth() {
//apply calculation logic if needed
return 500;
}
private int getRectangleHeight() {
//apply calculation logic if needed
return 400;
}
}
public static void main(String[] args)
{
//compilation error: The constructor MyFrame(String, int, int, int) is undefined
//new MyFrame("Insert name here!",d.width,d.height,1);
//comment 1
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new MyFrame();
}
});
}
}