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

  • SEARCH
  • Home
  • 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 7825505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T09:01:30+00:00 2026-06-02T09:01:30+00:00

Here are my classes that do the rendering: Game.java package poke; import java.awt.BorderLayout; import

  • 0

Here are my classes that do the rendering:

Game.java

package poke;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;

import poke.GamePanel;

import poke.graphics.GraphicsHandler;
import poke.player.Player;
import poke.player.PlayerHandler;
import poke.world.WorldHandler;

public class Game extends JFrame implements MouseListener, MouseMotionListener, KeyListener {

    private GamePanel gamePanel = new GamePanel();
    public static Toolkit toolkit = Toolkit.getDefaultToolkit();
    public static final int gameWidth = 1280;
    public static final int gameHeight = 720;

    public static String version = "Indev v0.1";
    private boolean running = false;

       private int fps = 60;
       private int frameCount = 0;

    // GAME INITIALIZATION

    public Game() {
        super("PokéRealms Client " + version);
        JFrame f = new JFrame();
        setVisible(true);
        running = true;
        setSize(new Dimension(gameWidth, gameHeight));
        setResizable(false);
        setBackground(Color.BLACK);

        Image icon = toolkit.getImage("./data/graphics/misc/icon.png");
        setIconImage(icon);

        addMouseListener(this);
        addMouseMotionListener(this);
        addKeyListener(this);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        runGameLoop();
    }

    // RUN THREAD

    public void runGameLoop() {
        Thread loop = new Thread() {
            public void run() {
                gameLoop();
            }
        };
        loop.start();
    }

    // GAME LOOP

    private void gameLoop() {

        final double GAME_HERTZ = 30.0;
        final double TIME_BETWEEN_UPDATES = 1000000000 / GAME_HERTZ;
        final int MAX_UPDATES_BEFORE_RENDER = 5;
        double lastUpdateTime = System.nanoTime();
        double lastRenderTime = System.nanoTime();
        final double TARGET_FPS = 60;
        final double TARGET_TIME_BETWEEN_RENDERS = 1000000000 / TARGET_FPS;

        int lastSecondTime = (int) (lastUpdateTime / 1000000000);

        while (running) {

            double now = System.nanoTime();
            int updateCount = 0;

            while( now - lastUpdateTime > TIME_BETWEEN_UPDATES && updateCount < MAX_UPDATES_BEFORE_RENDER ) {
                updateGame();
                lastUpdateTime += TIME_BETWEEN_UPDATES;
                updateCount++;
            }

            if (lastUpdateTime - now > TIME_BETWEEN_UPDATES) {
                lastUpdateTime = now - TIME_BETWEEN_UPDATES;
            }

            float interpolation = Math.min(1.0f, (float) ((now - lastUpdateTime) / TIME_BETWEEN_UPDATES) );
            drawGame(interpolation);
            lastRenderTime = now;

            int thisSecond = (int) (lastUpdateTime / 1000000000);
            if(thisSecond > lastSecondTime) {
                System.out.println("NEW SECOND " + thisSecond + " " + frameCount);
                fps = frameCount;
                frameCount = 0;
                lastSecondTime = thisSecond;
            }

            while(now - lastRenderTime < TARGET_TIME_BETWEEN_RENDERS && now - lastUpdateTime < TIME_BETWEEN_UPDATES) {
                Thread.yield();
                try {
                    Thread.sleep(1);
                } catch(Exception e) {
                    System.err.println(e);
                }

                now = System.nanoTime();
            }
        }
    }

    // UPDATES AND DRAWS

    private void updateGame() {
        gamePanel.update();
    }

    private void drawGame(float interpolation) {
        gamePanel.setInterpolation(interpolation);
        gamePanel.repaint();
    }

    // MAIN METHOD

    public static void main(String[] args) {
        Game game = new Game();
        System.out.println("Game initialized.");
    }
}

GamePanel.java

package poke;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;

import javax.swing.JPanel;

import poke.graphics.GraphicsHandler;
import poke.player.PlayerHandler;
import poke.util.ClockTime;
import poke.world.WorldHandler;

public class GamePanel extends JPanel {

    private static final long serialVersionUID = 1L;
    Image image;
    Graphics graphics;
    float interpolation;
    private int fps = 60;
    private int frameCount = 0;
    public boolean time = false;
    public int lastSecond = 0;

    public GamePanel() {
        PlayerHandler.addPlayer(0);
        WorldHandler.loadTiles();
        WorldHandler.loadObjects();
    }

    public void setInterpolation(float interp) {
        interpolation = interp;
    }

    public void update() {
        PlayerHandler.processWalking();
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        if(lastSecond != ClockTime.getSecond()) {
            lastSecond = ClockTime.getSecond();
            time = !time;
        }

        GraphicsHandler.drawWorld(g, time);
        PlayerHandler.process(g);

        image = createImage(getWidth(), getHeight());
        graphics = image.getGraphics();
        g.drawImage(image, 0, 0, this);

        g.setColor(Color.yellow);
        g.drawString("FPS: " + fps, 5, 10);

        frameCount++;
    }
}

My game had been working fine but I needed to add a time tick function because the game logic would tick at the same rate as the graphics, and I eventually intend to add multiplayer support, so I obviously need a fixed time tick rate for the logic. I’ve tried to implement a loop here, but I can’t figure out why my paintComponent() method isn’t getting called. Any help is appreciated.

  • 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-02T09:01:37+00:00Added an answer on June 2, 2026 at 9:01 am

    Swing paint methods need to be invoked by the event handler thread. See http://java.sun.com/products/jfc/tsc/articles/threads/threads1.html#single_thread_rule

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

Sidebar

Related Questions

I am trying to generate java classes that describe the web service offered here
Here is the two classes that I'm use to express a bidirectional OneToMany relationship.
I often have classes that are mostly just wrappers around some STL container, like
What I'm talking about here are nested classes. Essentially, I have two classes that
So here is the scenario: i have a series of different repository classes that
In my application, there are 10-20 classes that are instantiated once[*]. Here's an example:
I need to export big amount of data from database. Here is classes that
Simple question here. I have to define classes that will contain sets of books,
Taking the suggestion provided here , I've implemented my own RoleVoter classes that extends
Having used optional parameters in a few classes here and there, I'm starting to

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.