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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:01:47+00:00 2026-06-16T04:01:47+00:00

This is the screen that im trying to repaint but it is not repainting

  • 0

This is the screen that im trying to repaint but it is not repainting properly.

 public class arenaScreenBuild extends JPanel{
     int pX=200, pY=150;
     public void updateScreen(){
         repaint();
     }
     public void paintComponent(Graphics g) {
         g.drawString("x:"+pX, 535, 525);
         g.drawString("y:"+pY, 535, 545);       
     }
     public void refreshXY(int x, int y){
         pX=x;
         pY=y;
         System.out.println("Refreshed X&Y");
         updateScreen();
     }
 }

This is the screen displaying the graphics. When run, every time i move(press the right arrow key), it displays “Refreshed X&Y” but even though it calls the updateScreen() method, the displayed items are not redrawn. The code, if it had worked, should display x:XVALUE, y:YVALUE after the “refreshed X&Y”.

 public class ArenaKeys extends KeyAdapter {
     arenaScreenBuild arenaBG = new arenaScreenBuild();
     int xPos = 0, playerFace = 4,xPPos = 200, yPPos = 150;

     public void keyPressed(KeyEvent e) {
         int keyCode = e.getKeyCode();
         if (keyCode == e.VK_RIGHT) {
             if (xPos <= 3250) 
               if (((xPos + xPPos) >= 825) && ((xPos + xPPos) <= 910)
                       && (yPPos >= 170) && (yPPos <= 255)) {
               } else if (((xPos + xPPos) >= 1325) && ((xPos + xPPos)<= 1410)
                       && (yPPos >= 170) && (yPPos <= 255)) {
               } else
                   xPos += 5;
           }
           arenaBG.refreshXY(xPPos+xPos,yPPos);
       }
    }

EDIT: *Turns out that it does work but what i was doing was adding a Drawpanel on top of another drawpanel and this code was for the one underneath so it wasn’t letting the bottom code update, i solved this by merging together the two codes for both drawpanels.*

  • 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-16T04:01:48+00:00Added an answer on June 16, 2026 at 4:01 am

    This seems to work for me (it’s an SSCCE created out of your code). Since it works and there’s virtually no modifications to your code, the problem probably doesn’t lay in the code you posted, but in some other code. Also, generally you’d want to use Key Bindings instead of a KeyListener for these sorts of things.

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class Temp extends JPanel{
         int pX=200, pY=150;
         Dimension preferredSize = new Dimension(500,300);
        public Temp(){
            addKeyListener(new KeyAdapter() {
             int xPos = 0, playerFace = 4,xPPos = 200, yPPos = 150;
    
             public void keyPressed(KeyEvent e) {
                 int keyCode = e.getKeyCode();
                 if (keyCode == e.VK_RIGHT) {
                     if (xPos <= 3250) 
                       if (((xPos + xPPos) >= 825) && ((xPos + xPPos) <= 910)
                               && (yPPos >= 170) && (yPPos <= 255)) {
                       } else if (((xPos + xPPos) >= 1325) && ((xPos + xPPos)<= 1410)
                               && (yPPos >= 170) && (yPPos <= 255)) {
                       } else
                           xPos += 5;
                   }
                   refreshXY(xPPos+xPos,yPPos);
               }
            });
    
            setFocusable(true);
            requestFocus();
        }
    
        public Dimension getPreferredSize(){
            return preferredSize;
        }
    
         public void updateScreen(){
             repaint();
         }
         public void paintComponent(Graphics g) {
             super.paintComponent(g);
             g.drawString("x:"+pX, 0, 20);
             g.drawString("y:"+pY, 0, 40);       
         }
         public void refreshXY(int x, int y){
             pX=x;
             pY=y;
             System.out.println("Refreshed X&Y");
             updateScreen();
         }
    
        public static void main(String[] args) {
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setContentPane(new Temp());
            frame.pack();
            frame.setVisible(true);
        }
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been trying to make this graphics panel repaint but every time i call
I'm trying to write a script that contains this screen -S demo -d -m
I'm trying to create a screen that displays multiples items. For this, I've created
I have a window that uses a viewmodel. This screen contains 2 listviews on
This is some simple code that draws to the screen. GLuint vbo; glGenBuffers(1, &vbo);
I'm trying to print in the screen this, given any number like 12: 0+12
I have a application with a screen manager class that is causing me some
I'm trying to draw a 2D image on the screen that is always facing
I am trying to make a help screen that pulls up an html file
I'm working on writing a splash screen that returns a game mode (int) and

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.