可执行jar中的图形停止

时间:2012-12-06 16:54:09

标签: java swing graphics executable-jar keylistener

我遇到了在可执行jar中正确启动图形的问题。 jar有所需的所有文件(main.class,主$ 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();
                        }
                    }
            }
        }
    });
}
}

(我知道听众不是最好的解决方法;我会改变它。) 我添加了“工作”部分来检查代码是否正在停止并且在绘制图形之前它是否正常工作,但之后没有。有谁知道什么会阻止它?

1 个答案:

答案 0 :(得分:2)

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

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

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