JFrame 中的图像闪烁

发布于 2024-12-08 09:05:10 字数 3032 浏览 1 评论 0原文

我非常沮丧,因为我使用从一本关于 Java 的书中获得的知识来为我的程序设置和绘制 JFrame,但似乎宇宙中没有其他人以相同的方式设置它,也是修复它的唯一方法这将是大量的重新编码。所以帮忙吧!

我的程序出现可怕的图像闪烁。我不知道如何摆脱它。我知道如何双缓冲小程序,但这不是小程序,所以它对我没有任何好处。我有一组永远不会改变的图像(P1_xxx),然后我有一个“背景”(不确定这是正确的术语)图像,当其他变量发生变化时,该图像也会发生变化。即,当玩家完成圈数时。它们都闪烁。很多。这是代码。

我读过的所有内容几乎都回答了我的问题,但旨在以不同的方式设置 JFrame,在 JPanel 或其他类似的东西中。

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class Game extends JFrame {
//all images
    URL url1=null, url2=null, url3=null, url4=null, urlhome1=null;
    Image img1,img2,img3,img4,home1;
    //screen width
    final int WIDTH = 1080, HEIGHT = 726;
    //car
    Rectangle p1 = new Rectangle(390,620,WIDTH/35,WIDTH/35);
    //constructor
    public Game() {
        //create the JFrame
        super("Racer Doom Squared");
        setSize(WIDTH,HEIGHT);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        //load the urls
        try {
            url1 = this.getClass().getResource("Images/P1_Up.png");
            url2 = this.getClass().getResource("Images/P1_Right.png");
            url3 = this.getClass().getResource("Images/P1_Down.png");
            url4 = this.getClass().getResource("Images/P1_Left.png");
            urlhome1 = this.getClass().getResource("Images/Home1.png");
        }
        catch(Exception e){}
        //attach URLs to Images
        img1 = Toolkit.getDefaultToolkit().getImage(url1);
        img2 = Toolkit.getDefaultToolkit().getImage(url2);
        img3 = Toolkit.getDefaultToolkit().getImage(url3);
        img4 = Toolkit.getDefaultToolkit().getImage(url4);
        home1 = Toolkit.getDefaultToolkit().getImage(urlhome1);
    public void paint(Graphics g){
        super.paint(g);
        if(p1Laps==0) {
        //home1
        g.drawImage(home1,0,0,WIDTH,HEIGHT,this);
        }
        int scale = (WIDTH/HEIGHT)*3;
        //Up
        int p1width1 = img1.getWidth(this);
        int p1height1 = img1.getHeight(this);
        int w1 = scale*p1width1;
        int h1 = scale*p1height1;
        //Right
        int p1width2 = img2.getWidth(this);
        int p1height2 = img2.getHeight(this);
        int w2 = scale*p1width2;
        int h2 = scale*p1height2;
        //Down
        int p1width3 = img3.getWidth(this);
        int p1height3 = img3.getHeight(this);
        int w3 = scale*p1width3;
        int h3 = scale*p1height3;
        //Left
        int p1width4 = img4.getWidth(this);
        int p1height4 = img4.getHeight(this);
        int w4 = scale*p1width4;
        int h4 = scale*p1height4;
        if(p1Direction==UP) {
            g.drawImage(img1,p1.x,p1.y,(int)w1,(int)h1,this);
        }
        if(p1Direction==RIGHT) {
            g.drawImage(img2,p1.x,p1.y,(int)w2,(int)h2,this);
        }
        if(p1Direction==DOWN) {
            g.drawImage(img3,p1.x,p1.y,(int)w3,(int)h3,this);
        }
        if(p1Direction==LEFT) {
            g.drawImage(img4,p1.x,p1.y,(int)w4,(int)h4,this);
        }
    }

I'm very frustrated because I used knowledge I gained from a book on Java to set up and paint to a JFrame for my program, but it seems like no one else in the universe sets it up the same way and the only way to fix it would be a large amount of recoding. So help!

I get horrific image flicker with my program. I have no idea how to get rid of it. I know how to double buffer an applet, but this is not an applet so it does me no good. I have one "set" of images that never changes (P1_xxx) and then I have a "Background" (not sure that's the right terminology) image that changes when other variables change. Namely, when the player completes laps. They both flicker. A lot. Here's the code.

Everything I've read almost answers my question but is intended for the JFrame being set up a different way, within a JPanel or something else like that.

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class Game extends JFrame {
//all images
    URL url1=null, url2=null, url3=null, url4=null, urlhome1=null;
    Image img1,img2,img3,img4,home1;
    //screen width
    final int WIDTH = 1080, HEIGHT = 726;
    //car
    Rectangle p1 = new Rectangle(390,620,WIDTH/35,WIDTH/35);
    //constructor
    public Game() {
        //create the JFrame
        super("Racer Doom Squared");
        setSize(WIDTH,HEIGHT);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        //load the urls
        try {
            url1 = this.getClass().getResource("Images/P1_Up.png");
            url2 = this.getClass().getResource("Images/P1_Right.png");
            url3 = this.getClass().getResource("Images/P1_Down.png");
            url4 = this.getClass().getResource("Images/P1_Left.png");
            urlhome1 = this.getClass().getResource("Images/Home1.png");
        }
        catch(Exception e){}
        //attach URLs to Images
        img1 = Toolkit.getDefaultToolkit().getImage(url1);
        img2 = Toolkit.getDefaultToolkit().getImage(url2);
        img3 = Toolkit.getDefaultToolkit().getImage(url3);
        img4 = Toolkit.getDefaultToolkit().getImage(url4);
        home1 = Toolkit.getDefaultToolkit().getImage(urlhome1);
    public void paint(Graphics g){
        super.paint(g);
        if(p1Laps==0) {
        //home1
        g.drawImage(home1,0,0,WIDTH,HEIGHT,this);
        }
        int scale = (WIDTH/HEIGHT)*3;
        //Up
        int p1width1 = img1.getWidth(this);
        int p1height1 = img1.getHeight(this);
        int w1 = scale*p1width1;
        int h1 = scale*p1height1;
        //Right
        int p1width2 = img2.getWidth(this);
        int p1height2 = img2.getHeight(this);
        int w2 = scale*p1width2;
        int h2 = scale*p1height2;
        //Down
        int p1width3 = img3.getWidth(this);
        int p1height3 = img3.getHeight(this);
        int w3 = scale*p1width3;
        int h3 = scale*p1height3;
        //Left
        int p1width4 = img4.getWidth(this);
        int p1height4 = img4.getHeight(this);
        int w4 = scale*p1width4;
        int h4 = scale*p1height4;
        if(p1Direction==UP) {
            g.drawImage(img1,p1.x,p1.y,(int)w1,(int)h1,this);
        }
        if(p1Direction==RIGHT) {
            g.drawImage(img2,p1.x,p1.y,(int)w2,(int)h2,this);
        }
        if(p1Direction==DOWN) {
            g.drawImage(img3,p1.x,p1.y,(int)w3,(int)h3,this);
        }
        if(p1Direction==LEFT) {
            g.drawImage(img4,p1.x,p1.y,(int)w4,(int)h4,this);
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黄昏下泛黄的笔记 2024-12-15 09:05:10

您正在阅读的内容是告诉您在 JPanel 的 PaintComponent 方法中绘制,而不是在 JFrame 中绘制。否则,您将失去双缓冲和其他 Swing 优点的好处,所以我会相信它并遵循它。然后将 JPanel 添加到您的 JFrame 中。

如果这仍然令人困惑,请询问,但也请提供更多信息。

What you are reading is telling to draw in the paintComponent method of a JPanel, not in a JFrame. Otherwise you lose the benefits of double buffering and other Swing goodies, so I'd believe it and follow it. Then add the JPanel to your JFrame.

If this still confuses, please ask, but also supply more information.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文