【问题标题】:Graphics in an executable jar stopping可执行 jar 中的图形停止
【发布时间】:2012-11-24 18:35:35
【问题描述】:

我在可执行 jar 中正确启动图形时遇到问题。 jar 包含所有需要的文件(main.class、main$1.class、HighScore.txt、META-INF 等),并且程序本身正在正确启动。图形只是没有出现。

主要:

import java.awt.*;
import java.awt.event.*;
import java.awt.Graphics;
import java.io.*;
import java.util.Scanner;
import javax.swing.JFrame;

public class main
{
public static int position = 0;
public static long points = 0;
public static boolean happyInt = true;
public static int highScore;
public static void main(String[] args) throws FileNotFoundException
{
    JFrame app = new JFrame();
    Canvas window = new Canvas();

    window.setVisible(true);
    window.setIgnoreRepaint(true);
    window.setSize(1100, 145);
    app.setResizable(false);
    app.setIgnoreRepaint(true);
    app.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    app.add(window);
    app.pack();
    app.setVisible(true);
    app.setTitle("Shine has made a game!");

    Graphics g = window.getGraphics();

    g.drawString("Working", 10, 20);

    listener(app, window);

    @SuppressWarnings("resource")
    Scanner input = new Scanner(new File("HighScore.txt"));
    while(input.hasNextInt())
    {
        highScore = input.nextInt();
    }
    g.drawString("Working", 10, 20);
    Graphics graphics = null;
    graphics(window);
}
public static void highScore() throws FileNotFoundException
{
    @SuppressWarnings("resource")
    PrintStream output = new PrintStream(new File("HighScore.txt"));
    if(points > highScore)
    {
        output.println(points);
    }
    else
    {
        output.println(highScore);
    }
}
public static void graphics(Canvas window)
{
    Graphics g = window.getGraphics();

    Graphics graphics = null;
    while(happyInt)
    {
        if(position < 11)
        {
            position++;
        }
        else
        {
            position = 1;
        }
        try
        {
            // Draw stuff here using Java's Graphics Object!!!
            g.setColor(Color.black);
            g.fillRect(0, 0, 1100, 145);

            g.setColor(Color.magenta);
            if(position == 1)
            {
                g.setColor(Color.white);
            }
            g.fillRect(0,0,100,100);

            g.setColor(Color.blue);
            if(position == 2)
            {
                g.setColor(Color.white);
            }
            g.fillRect(100,0,100,100);

            g.setColor(Color.green);
            if(position == 3)
            {
                g.setColor(Color.white);
            }
            g.fillRect(200,0,100,100);

            g.setColor(Color.yellow);
            if(position == 4)
            {
                g.setColor(Color.white);
            }
            g.fillRect(300,0,100,100);

            g.setColor(Color.orange);
            if(position == 5)
            {
                g.setColor(Color.white);
            }
            g.fillRect(400,0,100,100);

            g.setColor(Color.red);
            if(position == 6)
            {
                g.setColor(Color.white);
            }
            g.fillRect(500,0,100,100);

            g.setColor(Color.orange);
            if(position == 7)
            {
                g.setColor(Color.white);
            }
            g.fillRect(600,0,100,100);

            g.setColor(Color.yellow);
            if(position == 8)
            {
                g.setColor(Color.white);
            }
            g.fillRect(700,0,100,100);

            g.setColor(Color.green);
            if(position == 9)
            {
                g.setColor(Color.white);
            }
            g.fillRect(800,0,100,100);

            g.setColor(Color.blue);
            if(position == 10)
            {
                g.setColor(Color.white);
            }
            g.fillRect(900,0,100,100);

            g.setColor(Color.magenta);
            if(position == 11)
            {
                g.setColor(Color.white);
            }
            g.fillRect(1000,0,100,100);

            g.setColor(Color.white);
            g.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 20));
            g.drawString("Score: "+points, 5, 120);
            g.drawString("High score: "+highScore, 5, 140);
            //g.drawString("For debug: "+happyInt, 5, 160);
            // Let the OS have a little time...
            //Thread.yield();
            try
            {
                Thread.sleep(100-points/25);
            }
            catch (InterruptedException e)
            {
            e.printStackTrace();
            }
        }
        finally
        {
        if( graphics != null )
            {
                graphics.dispose();
            }
        }
    }
}
public static void listener(JFrame app, Canvas window)
{
    final Graphics g = window.getGraphics();
    app.addKeyListener( new KeyAdapter() {
        public void keyPressed( KeyEvent e ) {
            if( e.getKeyCode() == KeyEvent.VK_SPACE )
            {
                    if(position == 6)
                    {
                        points = points + 100; 
                    }
                    if(position == 5 || position == 7)
                    {
                        points = points + 50;
                    }
                    if(position > 7 || position < 5)
                    {
                        happyInt = false;
                        g.setColor(Color.black);
                        g.fillRect(0, 0, 1100, 100);
                        position = 12;
                        try
                        {
                            highScore();
                        }
                        catch (FileNotFoundException e1)
                        {
                            e1.printStackTrace();
                        }
                    }
            }
        }
    });
}
}

(我知道侦听器不是处理这个问题的最佳方式;我会改变它。) 我添加了“工作”部分来检查代码是否停止并且在绘制图形之前它是否在工作,但不是在之后。有谁知道什么会阻止它?

【问题讨论】:

    标签: java swing graphics executable-jar keylistener


    【解决方案1】:

    您几乎不想使用通过在 Swing 组件上调用 getGraphics() 获得的 Graphics 对象。相反,您应该阅读Swing Graphics tutorials 并按照他们的建议进行操作:在扩展 JComponent 或其子之一的类的 paintComponent(...) 方法中进行绘图,并使用 JVM 提供给该方法的 Graphics 对象。您可以提高在 BufferedImage 中绘制背景图像的效率,通过这些,您可以使用通过在图像上调用 getGraphics() 获得的 Graphics 对象,但随后您将在 paintComponent(...) 中绘制图像方法也一样。

    编辑
    在进一步审查中,我发现您的代码还有其他主要问题,包括在 Swing 事件线程上使用 while (true) 循环和 Thread.sleep(...) 以及过度使用静态成员。你需要做一些工作来解决这个问题,但我们可以提供帮助。

    考虑为您的游戏循环使用Swing Timer,考虑删除除 main 方法之外的所有静态内容,并且永远不要在事件线程上调用 Thread.sleep(...)

    【讨论】:

    • 我认为这允许主动渲染。有时间我会试试的。
    • @playnow254:在图形编程方面你不应该做任何假设,因为如果你像我一样,你的大部分假设都是错误的。
    • @playnow254:您的代码还有其他主要问题,包括在 Swing 事件线程上使用 while (true) 循环和 Thread.sleep(...) 以及过度使用静态成员。你需要做一些工作来解决这个问题,但我们可以提供帮助。
    • 非常感谢。我不是很有经验,因此非常感谢所有帮助。
    猜你喜欢
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 2013-06-14
    • 1970-01-01
    • 2014-10-02
    • 1970-01-01
    • 2021-08-19
    • 2021-11-02
    相关资源
    最近更新 更多