如何阻止自动重绘(),当我调整的JFrame [英] How to stop the auto-repaint() when I resize the Jframe

查看:251
本文介绍了如何阻止自动重绘(),当我调整的JFrame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仍然在学习Java中,如果有人能帮助我,我会很开心!

对不起,英文不好,我是西班牙!
我想提出一个瓷砖游戏,游戏采用了经典的游戏循环的上限发动机在60fps
循环睡眠,然后调用重绘();
这工作得很好!但是..

问题是,当在JFrame调整大小或最大化的重绘事件被称为!
例如,当JFrame的最大化/调整游戏呈现在10000fps,但是当他们不,比赛呈现在我设定的速度,所以有办法禁用自动重绘,并使其只工作当component.repaint() 从我的code叫什么名字?

这里的问题是不是游戏循环中,问题是,重绘称为自动当调整大小/最大化渲染游戏的更多FPS!

 公共类处理{
静态私人诠释FPS = 0;
静态私人INT fpsfinal = 0;
静态INT帧= 60;
静态INT frames_skip = 1000 /帧;
静态长ticknext =的GetTickCount();
静态长ticksleep = 0;公共静态无效的run(){
    对于(;;){
        Main.getDevice()重绘()。
        FPS ++;
        ticknext + = frames_skip;
        ticksleep = ticknext - 的GetTickCount();
        如果(ticksleep> = 0){
            尝试{
                视频下载(ticksleep);
            }赶上(InterruptedException的E){
                e.printStackTrace();
            }
        }
    }
}公共静态长的GetTickCount(){
    返回System.currentTimeMillis的();
}公共静态INT GetFPS(){
    fpsfinal = FPS;
    FPS = 0;
    返回fpsfinal-2;
}进口java.awt中的*。
进口java.awt.geom.AffineTransform中;
进口java.awt.image.ImageObserver中;进口的javax.swing *。
公共类游戏继承JPanel {
私有静态最后的serialVersionUID长1L =;
静态布尔DIR = TRUE;@覆盖
公共无效的paintComponent(图形G){
    super.paintComponent方法(G);
    INT布局= Main.layout;
    Graphics2D的G2D =(Graphics2D的)克;
    g.setFont(Resources.GetFont(0));
    Camera.UpdateCameraAxis();
    Camera.DoCamera();
    Cursor.MouseLoop();
    如果(布局== 0){
        this.setBackground(新颜色(210247255));
        对于(INT XX = Math.max(等....


解决方案

  

所以当JFrame的未最大化


我不知道为什么会导致问题,因为只有一个重画请求应生成。


  

或(调整)


我可以看到这是一个问题,因为重绘将被调用为您调整由帧的每一个像素。在这种情况下,也许你可以使用:

  Toolkit.getDefaultToolkit()setDynamicLayout(假)。

现在调整大小完成后的组件将验证。


  

this.setBackground(新颜色(210247255));


不要使用code像上面因为改变的原因重绘背景()被调用,这将再次调用的paintComponent()方法。绘画方法仅用于绘画。

I am still learning Java, if someone can help me I will be very happy!

Sorry for bad english, I am spanish! I am making a tile game, the game uses the classic "game loop" that cap the engine at 60fps The loop sleep and then call repaint(); This works fine! But..

The problem is that repaint event is called when the JFrame is resized or maximized! For example when the JFrame is maximized/resized the game render at 10000fps but when they dont, the game render at the speed I set, so there is a way to disable Automatic repaint and make it work ONLY when the "component.repaint()" is called from my code?

The problem here is not the "game loop", the problem is that the repaint is called automatic when resize/maximized rendering the game at more fps!

public class Handling {
static private int fps=0;
static private int fpsfinal=0;
static int frames = 60;
static int frames_skip = 1000 / frames;
static long ticknext = GetTickCount();
static long ticksleep = 0;

public static void Run() {
    for(;;){
        Main.getDevice().repaint();
        fps++;
        ticknext += frames_skip;
        ticksleep = ticknext - GetTickCount();
        if(ticksleep >= 0) {
            try {
                Thread.sleep(ticksleep);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

public static long GetTickCount(){
    return System.currentTimeMillis();
}

public static int GetFPS(){
    fpsfinal=fps;
    fps=0;
    return fpsfinal-2;
}

import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.ImageObserver;

import javax.swing.*;
public class Game extends JPanel{
private static final long serialVersionUID = 1L;
static boolean dir=true;

@Override 
public void paintComponent(Graphics g){
    super.paintComponent(g);        
    int layout=Main.layout;
    Graphics2D g2d = (Graphics2D) g;
    g.setFont(Resources.GetFont(0));    
    Camera.UpdateCameraAxis();
    Camera.DoCamera();
    Cursor.MouseLoop(); 
    if (layout==0){
        this.setBackground(new Color(210,247,255));
        for(int xx=Math.max( etc....

解决方案

so when the JFrame isn't maximized

I don't know why this would cause a problem since only one repaint request should be generated.

or (resizing)

I can see this being a problem since repaint will be invoked for every pixel you resize the frame by. In this case maybe you can use:

Toolkit.getDefaultToolkit().setDynamicLayout(false); 

Now the components will validated after resizing is complete.

this.setBackground(new Color(210,247,255));

Don't use code like the above since changing the background causes repaint() to be invoked which will invoke the paintComponent() method again. Painting methods are for painting only.

这篇关于如何阻止自动重绘(),当我调整的JFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆