I’m having a problem with correctly starting graphics in an executable jar. The jar has all the files needed (main.class, main$1.class, HighScore.txt, META-INF and all that) and the program itself is starting correctly. The graphics just don’t appear.
main:
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();
}
}
}
}
});
}
}
(I know the listener isn’t the best way to work with this; I will change it.)
I added the “Working” part to check if the code was stopping and it’s working before the graphics are drawn, but not afterwards. Does anyone have any idea what would stop it up?
You almost never want to use a Graphics object obtained by calling
getGraphics()on a Swing component. Instead you should read the Swing Graphics tutorials and do what they suggest: doing your drawing in thepaintComponent(...)method of a class that extend JComponent or one of its children, and using the Graphics object provided to the method by the JVM. You can improve efficiency of drawing background images in a BufferedImage, and with these you can use a Graphics object obtained by callinggetGraphics()on the image, but then you will draw the image in thepaintComponent(...)method as well.Edit
On further review, I see that your code has other major problems including using a while (true) loop and Thread.sleep(…) on the Swing event thread as well as over-use of static members. You’ve got a bit of work cut out for you to fix this thing, but we can help.
Consider using a Swing Timer for your game loop, consider removing all static anythings except for the main method, and never call
Thread.sleep(...)on the event thread.