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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:46:26+00:00 2026-05-25T12:46:26+00:00

I have a custom, abstract class ‘Panel’ which extends JPanel. There aren’t many differences

  • 0

I have a custom, abstract class ‘Panel’ which extends JPanel. There aren’t many differences with the two when painting. I have a Panel and I’m simulating an animation by updating the x value of an image. I have two animations right now, one that properly repaints and another than does not. This is for the one that does not. The one that works will be labelled A, the one that doesn’t will be B.

A and B follow the same format. Update some variable on the Panel, calls update (a method in Panel which calls PaintComponent) and then calls repaint. It calls repaint after because this issue was with A before and was solved that way.

A: Updates an image variable.
B: Updates the x variable of an image.

The Problem: The repaint doesn’t clear the old image location and so it’s a choppy mess across the screen.

What I’ve tried:

  • I’ve seen the super.PaintComponent(g) mentioned a lot, but this
    hasn’t solved the problem.
  • I’ve tried changing the order for when the repaint/update methods are
    called.
  • Repaint does not update the Panel at all. (Probably because the
    painting is done in PaintComponent)

Any help would be appreciated.

Code:

Panel:

public Panel (boolean visible){
    super();
    this.setLayout(new BorderLayout(640, 416));//sets the Layout type of the panel
    this.setOpaque(false);//Makes it so that the panel underneath can be seen where images aren't drawn
    this.setVisible(visible);
    ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    gs = ge.getDefaultScreenDevice();
    gc = gs.getDefaultConfiguration();
}

public void paintComponent (Graphics g){
    setUp();
    drawOff();
    setDown(g);
}

private void setUp(){
    off_screen = gc.createCompatibleImage(getSize().width, getSize().height, Transparency.TRANSLUCENT);
    buffer = off_screen.createGraphics();
}

protected abstract void drawOff();

private void setDown(Graphics g){
    g.drawImage(off_screen,0,0,this);
    off_screen.flush(); 
}

public void update(){
    paintComponent(this.getGraphics());
}

Animation Methods (mg is the panel in question):

private void battleStart(User user) {
    for (int i = 0; i < user.battle.length; i++) {
        mg.battleStart(user.battleStart(i));
        mg.update();
        try {
            Thread.sleep(150);
        } catch (Exception e) {

        }
        mg.repaint();
    }
}

private void animateStart(User user){
    for (int i = 0; i < 10; i++){
        mg.x = mg.x + 10;
        mg.update();
        try {
            Thread.sleep(100);
        } catch (Exception e) {

        }
        mg.repaint();
    }
}
  • 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-05-25T12:46:26+00:00Added an answer on May 25, 2026 at 12:46 pm

    I think your design is way off and that is why things are not working. I’m not quite sure how your non-abstract JPanels work, but consider making your parent JPanel something more along these lines:

    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.BufferedImage;
    import javax.swing.*;
    
    public class MyPanel extends JPanel {
       private GraphicsEnvironment ge;
       private GraphicsDevice gs;
       private GraphicsConfiguration gc;
       private BufferedImage offScreen;
    
       public MyPanel(boolean visible) {
          super();
          this.setLayout(new BorderLayout(640, 416)); // strange constants for this layout.
          this.setOpaque(false);
          this.setVisible(visible);
          ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
          gs = ge.getDefaultScreenDevice();
          gc = gs.getDefaultConfiguration();
    
          addComponentListener(new ComponentAdapter() {
             @Override
             public void componentResized(ComponentEvent e) {
                setUp();
             }
          });
       }
    
       @Override
       // don't make this public. Keep it protected like the super's
       // just draw in this method. Don't call other methods that create buffers
       // or draw to buffers.
       protected void paintComponent(Graphics g) {
          super.paintComponent(g);
          if (offScreen != null) {
             g.drawImage(offScreen, 0, 0, null);
          }
       }
    
       private void setUp() {
          offScreen = gc.createCompatibleImage(getSize().width, getSize().height,
                Transparency.TRANSLUCENT);
       }
    
       // draw to the buffer outside of the paintComponent
       // and then call repaint() when done
       public void upDateOffScreen() {
          // ?? offScreen.flush(); // I've never used this before, 
                      // so am not sure if you need this here
          Graphics2D osGraphics = offScreen.createGraphics();
          // TODO: do drawing with osGraphics object here
          osGraphics.dispose();
          repaint();
       }
    }
    

    Also and again,

    • Do all long processing methods off of the EDT (Event Dispatch Thread).
    • Never call Thread.sleep(…) on the EDT.
    • Consider using Swing Timers instead of using Thread.sleep for the animations.
    • It’s OK to call repaint on your JPanel off of the EDT, but for the most part that’s about it.
    • All other Swing methods should be called on the EDT.
    • Read, re-read, and study the 2D and Swing graphics tutorials.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a custom type, which is abstract. and will have some class to
I have a custom abstract base class with sub classes that I've made serializable/deseriablizeable
I have an interface IServiceInfo and an abstract class ServiceInfo. There are several classes
I have a custom base page class: //MobileFunnelPage.cs public abstract class MobileFunnelPage : Page
I have a class GridPanel extends JPanel , with a static inner class ToolSelectComboBox
I have an abstract class A and several implementations of it. I expect this
I have a class similar to the following: public abstract class Manager<T, TInterface> :
In one of my aplications I have to use many dictonarys with custom objects
I have a model (simplified for relevance) like this: public abstract class TitleCreateModel :
I'm writing a plug-in for a 3D modeling program. I have a custom class

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.