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 8232701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T17:55:04+00:00 2026-06-07T17:55:04+00:00

I’m creating a video game, and when I call the repaint() method, the entire

  • 0

I’m creating a video game, and when I call the repaint() method, the entire screen updates and flashes irregularly. I have slightly fixed that problem, by specifying what’s repainted (charRect, which is a Rectangle object) in this line of code: repaint(charRect.x, charRect.y, 20, 32); The entire screen stops flashing, but the character is incredibly glitchy. I just need help to get the animation from moving (a and d) and jumping (w) to run smoothly.

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.*;
import java.io.*;
import java.net.*;
import java.lang.*;
import static java.lang.Math.*;
import static java.lang.System.out;
import java.util.Random.*;
import javax.swing.JApplet;
import java.awt.Graphics;

public class UltimaBlade extends JApplet implements KeyListener
{
public final int        WIDTH = 900;
public final int        HEIGHT = 650;

public  int             score = 0;
private boolean         jumping = false;
private boolean         moveRight = false;
private boolean         moveLeft = false;
private boolean         jumpRight = false;
private boolean         jumpLeft = false;
private boolean         moveRightWhileJumping = false;
private boolean         moveLeftWhileJumping = false;

public  JFrame          JTitle = new JFrame("UltimaBlade"); 
        Image           characterImg = null;
        Image           blockImg1 = null;
        Image           floorImg1 = null;
        Image           wallImg1 = null;
             JLabel          scoreLabel = new JLabel("Score: " + score);

        Rectangle       charRect = new Rectangle(25, 450, 20, 32);
        Rectangle       blockRect1 = new Rectangle(25, 482, 30, 30);
        Rectangle       floorRect1 = new Rectangle(25, 482, 200, 1);CHANGE BACK TO 30
        Rectangle       wallRect1 = new Rectangle(55, 490, 1, 30);
        //Rectangle       tempGhost = new Rectangle(charRect.x, charRect.y, 20, 32);

public  JMenuBar        mb = new JMenuBar();
public  JMenu           menuFile = new JMenu("File");
public  JMenuItem       menuItemSave = new JMenuItem("Save");
public  JMenuItem       menuItemLoad = new JMenuItem("Load");
        Container       cont;
        Runner          runner;

public void init()
{
    setSize(WIDTH, HEIGHT);

    JTitle.setJMenuBar(mb);
    menuFile.add(menuItemSave);
    menuFile.add(menuItemLoad);
    mb.add(menuFile);
    JTitle.getContentPane().setLayout(new BorderLayout());
    menuItemSave.addActionListener(new ListenMenuSave());
    setJMenuBar(mb);
    new UltimaBlade();
}

public UltimaBlade()
{
    setLayout(null);
    setSize(WIDTH, HEIGHT);
    setVisible(true);

    cont = getContentPane();
    cont.setLayout(null);
    addKeyListener(this);
    cont.setBackground(Color.WHITE);

    cont.add(scoreLabel);
    scoreLabel.setBounds(50, 50, 100, 30);
    scoreLabel.setForeground(Color.BLACK);
    cont.setLayout(null);

    repaint();
    cont.validate();

    runner = new Runner();
    runner.start();
    setContentPane(cont);
}

public class ListenMenuSave implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        new Save();
    }
}

public void paint(Graphics g)
{
    super.paint(g);

    try
    {
        URL     url1 = this.getClass().getResource("character.gif");       //Character
                characterImg = Toolkit.getDefaultToolkit().getImage(url1);
        URL     url2 = this.getClass().getResource("block.gif");           //Block
                blockImg1 = Toolkit.getDefaultToolkit().getImage(url2);
        URL     url3 = this.getClass().getResource("floor_line.gif");      //Floor Line
                floorImg1 = Toolkit.getDefaultToolkit().getImage(url3);
        URL     url4 = this.getClass().getResource("wall_line.gif");      //Wall Line
                wallImg1 = Toolkit.getDefaultToolkit().getImage(url4);
    }
    catch(Exception e)
    {

    }

    g.drawImage(characterImg, charRect.x, charRect.y, this);
    g.drawImage(floorImg1, floorRect1.x, floorRect1.y, this);
    g.drawImage(blockImg1, blockRect1.x, blockRect1.y, this);
    g.drawImage(wallImg1, wallRect1.x, wallRect1.y, this);
}

public void keyPressed(KeyEvent e)
{
    if(e.getKeyChar() == 'd' || e.getKeyChar() == 'D')
    {
        moveRight = true;
        if(jumping)
            moveRightWhileJumping = true;
    }
    if(e.getKeyChar() == 'a' || e.getKeyChar() == 'A')
    {
        moveLeft = true;
        if(jumping)
            moveLeftWhileJumping = true;
    }
}

public void keyReleased(KeyEvent e)
{
    if(e.getKeyChar() == 'd' || e.getKeyChar() == 'D')
    {
        moveRight = false;
        if(!jumping)
            moveRightWhileJumping = false;
    }
    if(e.getKeyChar() == 'a' || e.getKeyChar() == 'A')
    {
        moveLeft = false;
        if(!jumping)
            moveLeftWhileJumping = false;
    }
}

public void keyTyped(KeyEvent e)
{
    if(e.getKeyChar() == 'w' || e.getKeyChar() == 'W')
    {
        jumping = true;
    }
}

public class Runner extends Thread// implements Runnable
{
    public void run()
    {
        while(true)
        {
            try
            {
                int     j = 5;     //Beginning velocity
                double  t = 0;
                double  sum = j/5;
                while(jumping)
                {
                    repaint(charRect.x, charRect.y, 20, 32);
                    cont.validate();
                    int jump = (int)((-4.9 * ((t * t)/10)) + (j * t));
                    if(moveRightWhileJumping)
                    {
                        charRect.x = charRect.x + j; // Move Right while jumping.
                        if(charRect.intersects(wallRect1))
                        {
                            charRect.x = wallRect1.x - 21;
                            moveRightWhileJumping = false;
                        }
                    }
                    if(moveLeftWhileJumping)
                    {
                        charRect.x = charRect.x - j;  //Move Left while jumping.
                        if(charRect.intersects(wallRect1))
                        {
                            charRect.x = wallRect1.x + 1;
                            moveLeftWhileJumping = false;
                        }
                    }
                    if(jump > 0)
                    {
                        charRect.y = charRect.y - Math.abs(jump);
                        if(charRect.intersects(floorRect1))
                        {
                            charRect.y = floorRect1.y - 32;
                            jumping = false;
                            moveRightWhileJumping = false;
                            moveLeftWhileJumping = false;
                            break;
                        }
                    }
                    else if(jump < 0)
                    {
                        charRect.y = charRect.y + Math.abs(jump);
                        if(charRect.intersects(floorRect1))
                        {
                            charRect.y = floorRect1.y - 32;
                            jumping = false;
                            moveRightWhileJumping = false;
                            moveLeftWhileJumping = false;
                            break;
                        }
                    }
                    else
                    {
                        if(charRect.intersects(floorRect1))
                        {
                            charRect.y = floorRect1.y - 32;
                            jumping = false;
                            moveRightWhileJumping = false;
                            moveLeftWhileJumping = false;
                            break;
                        }
                    }

                    t++;
                    Thread.sleep(30);
                }


                while(moveLeft)
                {
                    repaint(charRect.x, charRect.y, 20, 32);
                    cont.validate();
                    charRect.x = charRect.x - j;  //Move Left speed.
                    if(charRect.intersects(wallRect1))
                    {
                        charRect.x = wallRect1.x + 1;
                    }
                    if(charRect.intersects(floorRect1))
                    {
                        charRect.y = floorRect1.y - 32;
                        jumping = false;
                        moveRightWhileJumping = false;
                        moveLeftWhileJumping = false;
                        break;
                    }
                    t++;
                    Thread.sleep(30);
                }


                while(moveRight)
                {
                    repaint(charRect.x, charRect.y, 20, 32);
                    cont.validate();
                    charRect.x = charRect.x + j;  //Move Left speed.
                    if(charRect.intersects(wallRect1))
                    {
                        charRect.x = wallRect1.x + 1;
                    }
                    if(charRect.intersects(floorRect1))
                    {
                        charRect.y = floorRect1.y - 32;
                        jumping = false;
                        moveRightWhileJumping = false;
                        moveLeftWhileJumping = false;
                        break;
                    }
                    t++;
                    Thread.sleep(30);
                }
            }
            catch(Exception e)
            {
                break;
            }
        }
    }
}

}

  • 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-07T17:55:06+00:00Added an answer on June 7, 2026 at 5:55 pm

    One of your main problems is that you’re painting directly in the paint(...) method JApplet itself rather in the paintComponent(...) method of a JPanel or JCompnent that is held by the applet. If you do the latter, you gain the benefit of Swing’s default double buffering, and that can make all the difference. Also, you can repaint a smaller Rectangle of the GUI by using one of the repaint(...) overloads, but this is of secondary importance. First you should solve your lack of double buffering problem.

    Please start with this tutorial to learn the basics: Lesson: Performing Custom Painting

    And then move on up to this article for more advanced information about drawing in Swing: Painting in AWT and Swing

    Edit
    Also, you should never read in files from within the paint(...) or paintComponent(...) methods as this will slow painting down and thereby reduce perceived responsiveness of your GUI several fold. Why not simply read in the files once and on applet startup, and then store their contents into a variable?

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.