Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8952657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:55:22+00:00 2026-06-15T13:55:22+00:00

I’m having a problem with correctly starting graphics in an executable jar. The jar

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T13:55:23+00:00Added an answer on June 15, 2026 at 1:55 pm

    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 the paintComponent(...) 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 calling getGraphics() on the image, but then you will draw the image in the paintComponent(...) 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I've got a string that has curly quotes in it. I'd like to replace
I am currently running into a problem where an element is coming back from
In my XML file chapters tag has more chapter tag.i need to display chapters
I am doing a simple coin flipping experiment for class that involves flipping a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.