How to fix flickering of dynamic moving images over a JPanel that is on top of another pre-painted JPanel?Is calling repaint from paintComponent a good practiceHow to add an image to a JPanel?“cannot find symbol method drawImage(java.awt.image.BufferedImage,<nulltype>,int,int)”Why can't I draw an ellipse with this code?Java swing animation looks choppy. How to make it look pro?problem in setting scrollpane for canvasJPanel added but not displayed “in time”Java Swing Not Rendering CorrectlyDrawing an image in JScrollPane within scaleWorking on a java based chatting application using threadingContinue JTable data on another JTable beside it instead of scrolling
Does Peach's float negate shorthop knockback multipliers?
Is there any Biblical Basis for 400 years of silence between Old and New Testament?
Why was it possible to cause an Apple //e to shut down with SHIFT and paddle button 2?
California: "For quality assurance, this phone call is being recorded"
Explain Ant-Man's "not it" scene from Avengers: Endgame
Anyone teach web development? How do you assess it?
Metal bar on DMM PCB
Is the decompression of compressed and encrypted data without decryption also theoretically impossible?
If a problem only occurs randomly once in every N times on average, how many tests do I have to perform to be certain that it's now fixed?
Is there a rule that prohibits us from using 2 possessives in a row?
Can The Malloreon be read without first reading The Belgariad?
Can a class take a different class's spell in their ritual book?
Creating Fictional Slavic Place Names
Sucuri detects malware on wordpress but I can't find the malicious code
Computing the differentials in the Adams spectral sequence
Accidentally cashed a check twice
Could a guilty Boris Johnson be used to cancel Brexit?
How to provide realism without making readers think grimdark
The term for the person/group a political party aligns themselves with to appear concerned about the general public
What is the Process for Re-certifying Flight Hardware?
Access to all elements on the page
Why is Colorado so different politically from nearby states?
Unorthodox way of solving Einstein field equations
When leasing/renting out an owned property, is there a standard ratio between monthly rent and the mortgage?
How to fix flickering of dynamic moving images over a JPanel that is on top of another pre-painted JPanel?
Is calling repaint from paintComponent a good practiceHow to add an image to a JPanel?“cannot find symbol method drawImage(java.awt.image.BufferedImage,<nulltype>,int,int)”Why can't I draw an ellipse with this code?Java swing animation looks choppy. How to make it look pro?problem in setting scrollpane for canvasJPanel added but not displayed “in time”Java Swing Not Rendering CorrectlyDrawing an image in JScrollPane within scaleWorking on a java based chatting application using threadingContinue JTable data on another JTable beside it instead of scrolling
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I have made 2 JPanels, Panel and BackGround, in a JFrame. I am dynamically painting the Panel after 10ms(using a Timer), but the BackGround is only painted once at the beginning of the game. The Panel is responsible for the displaying of the fighters(spacecrafts), the projectiles and the aliens. The BackGround is responsible for the displaying of the background scene which is non-dynamic. The paintComponent(Graphics) method does paint the fighters and the projectiles, but flickers when they are updating. Can someone find the cause.
This is my Frame:
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class Frame extends JFrame
private static final long serialVersionUID = 1L;
public static final int WIDTH = 1280;
public static final int HEIGHT = 720;
public static final int DELAY = 10;
private Panel panel;
private Background bg;
public Frame()
panel = new Panel();
bg = new Background();
initComponents();
public void initComponents()
this.setSize(WIDTH, HEIGHT);
this.add(bg);
this.add(panel);
this.setLocationRelativeTo(null);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.addKeyListener(new KeyAdapter()
@Override
public void keyPressed(KeyEvent e)
if(e.getKeyCode() == KeyEvent.VK_LEFT) panel.moveLeft();
else if(e.getKeyCode() == KeyEvent.VK_RIGHT) panel.moveRight();
);
public static void main(String args[])
SwingUtilities.invokeLater(new Runnable()
@Override
public void run()
new Frame().setVisible(true);
);
This is my Panel:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Panel extends JPanel implements ActionListener
private static final long serialVersionUID = 1L;
public static final int DELAY = Frame.DELAY;
private Timer timer;
private BufferedImage fighter;
int x, y;
public Panel()
timer = new Timer(DELAY, this);
try
fighter = ImageIO.read(this.getClass().getResource("fighter.png"));
catch (IOException e)
e.printStackTrace();
initComponents();
timer.start();
public void initComponents()
this.setSize(Frame.WIDTH, Frame.HEIGHT);
this.setDoubleBuffered(true);
this.setBackground(new Color(0, 0, 0, 0));
x = 150;
y = 200;
@Override
public void paintComponent(Graphics g)
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
doDrawing(g2d);
private void doDrawing(Graphics2D g2d)
g2d.drawImage(fighter, x, y, null);
@Override
public void actionPerformed(ActionEvent arg0)
this.repaint();
public void moveLeft()
x -= 10;
public void moveRight()
x += 10;
This is the BackGround:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Background extends JPanel implements ActionListener
private static final long serialVersionUID = 1L;
private BufferedImage backGround;
private Timer timer;
public Background()
this.setSize(Frame.WIDTH, Frame.HEIGHT);
this.setBackground(new Color(0, 0, 0, 0));
timer = new Timer(Frame.DELAY, this);
try
backGround = ImageIO.read(this.getClass().getResource("background.png"));
catch (IOException e)
e.printStackTrace();
timer.start();
@Override
public void paintComponent(Graphics g)
super.paintComponent(g);
g.drawImage(backGround, 0, 0, null);
@Override
public void actionPerformed(ActionEvent e)
this.repaint();
I expect the sprites not to flicker and not to lag(which is not happening after lots of trials).
java swing user-interface jframe jpanel
|
show 5 more comments
I have made 2 JPanels, Panel and BackGround, in a JFrame. I am dynamically painting the Panel after 10ms(using a Timer), but the BackGround is only painted once at the beginning of the game. The Panel is responsible for the displaying of the fighters(spacecrafts), the projectiles and the aliens. The BackGround is responsible for the displaying of the background scene which is non-dynamic. The paintComponent(Graphics) method does paint the fighters and the projectiles, but flickers when they are updating. Can someone find the cause.
This is my Frame:
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class Frame extends JFrame
private static final long serialVersionUID = 1L;
public static final int WIDTH = 1280;
public static final int HEIGHT = 720;
public static final int DELAY = 10;
private Panel panel;
private Background bg;
public Frame()
panel = new Panel();
bg = new Background();
initComponents();
public void initComponents()
this.setSize(WIDTH, HEIGHT);
this.add(bg);
this.add(panel);
this.setLocationRelativeTo(null);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.addKeyListener(new KeyAdapter()
@Override
public void keyPressed(KeyEvent e)
if(e.getKeyCode() == KeyEvent.VK_LEFT) panel.moveLeft();
else if(e.getKeyCode() == KeyEvent.VK_RIGHT) panel.moveRight();
);
public static void main(String args[])
SwingUtilities.invokeLater(new Runnable()
@Override
public void run()
new Frame().setVisible(true);
);
This is my Panel:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Panel extends JPanel implements ActionListener
private static final long serialVersionUID = 1L;
public static final int DELAY = Frame.DELAY;
private Timer timer;
private BufferedImage fighter;
int x, y;
public Panel()
timer = new Timer(DELAY, this);
try
fighter = ImageIO.read(this.getClass().getResource("fighter.png"));
catch (IOException e)
e.printStackTrace();
initComponents();
timer.start();
public void initComponents()
this.setSize(Frame.WIDTH, Frame.HEIGHT);
this.setDoubleBuffered(true);
this.setBackground(new Color(0, 0, 0, 0));
x = 150;
y = 200;
@Override
public void paintComponent(Graphics g)
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
doDrawing(g2d);
private void doDrawing(Graphics2D g2d)
g2d.drawImage(fighter, x, y, null);
@Override
public void actionPerformed(ActionEvent arg0)
this.repaint();
public void moveLeft()
x -= 10;
public void moveRight()
x += 10;
This is the BackGround:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Background extends JPanel implements ActionListener
private static final long serialVersionUID = 1L;
private BufferedImage backGround;
private Timer timer;
public Background()
this.setSize(Frame.WIDTH, Frame.HEIGHT);
this.setBackground(new Color(0, 0, 0, 0));
timer = new Timer(Frame.DELAY, this);
try
backGround = ImageIO.read(this.getClass().getResource("background.png"));
catch (IOException e)
e.printStackTrace();
timer.start();
@Override
public void paintComponent(Graphics g)
super.paintComponent(g);
g.drawImage(backGround, 0, 0, null);
@Override
public void actionPerformed(ActionEvent e)
this.repaint();
I expect the sprites not to flicker and not to lag(which is not happening after lots of trials).
java swing user-interface jframe jpanel
1
Get therepaint()
call out of your painting method as it never belongs there
– Hovercraft Full Of Eels
Mar 24 at 12:31
@HovercraftFullOfEels I did not get that.
– Aniket Das
Mar 24 at 12:37
1
stackoverflow.com/questions/30561690/…
– Hovercraft Full Of Eels
Mar 24 at 12:42
1
1) For better help sooner, edit to add a minimal reproducible example or Short, Self Contained, Correct Example. 2) Use a logical and consistent form of indenting code lines and blocks. The indentation is intended to make the flow of the code easier to follow! 3) EveryJComponent
is anImageObserver
, sog.drawImage(backGround, 0, 0, null);
should better beg.drawImage(backGround, 0, 0, this);
– Andrew Thompson
Mar 24 at 14:06
1
@AndrewThompson is not asking for the whole program but rather a MCVE/SSCCE. You will want to read the links that he gave you as they will explain exactly what he is (we are) requesting.
– Hovercraft Full Of Eels
Mar 24 at 14:59
|
show 5 more comments
I have made 2 JPanels, Panel and BackGround, in a JFrame. I am dynamically painting the Panel after 10ms(using a Timer), but the BackGround is only painted once at the beginning of the game. The Panel is responsible for the displaying of the fighters(spacecrafts), the projectiles and the aliens. The BackGround is responsible for the displaying of the background scene which is non-dynamic. The paintComponent(Graphics) method does paint the fighters and the projectiles, but flickers when they are updating. Can someone find the cause.
This is my Frame:
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class Frame extends JFrame
private static final long serialVersionUID = 1L;
public static final int WIDTH = 1280;
public static final int HEIGHT = 720;
public static final int DELAY = 10;
private Panel panel;
private Background bg;
public Frame()
panel = new Panel();
bg = new Background();
initComponents();
public void initComponents()
this.setSize(WIDTH, HEIGHT);
this.add(bg);
this.add(panel);
this.setLocationRelativeTo(null);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.addKeyListener(new KeyAdapter()
@Override
public void keyPressed(KeyEvent e)
if(e.getKeyCode() == KeyEvent.VK_LEFT) panel.moveLeft();
else if(e.getKeyCode() == KeyEvent.VK_RIGHT) panel.moveRight();
);
public static void main(String args[])
SwingUtilities.invokeLater(new Runnable()
@Override
public void run()
new Frame().setVisible(true);
);
This is my Panel:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Panel extends JPanel implements ActionListener
private static final long serialVersionUID = 1L;
public static final int DELAY = Frame.DELAY;
private Timer timer;
private BufferedImage fighter;
int x, y;
public Panel()
timer = new Timer(DELAY, this);
try
fighter = ImageIO.read(this.getClass().getResource("fighter.png"));
catch (IOException e)
e.printStackTrace();
initComponents();
timer.start();
public void initComponents()
this.setSize(Frame.WIDTH, Frame.HEIGHT);
this.setDoubleBuffered(true);
this.setBackground(new Color(0, 0, 0, 0));
x = 150;
y = 200;
@Override
public void paintComponent(Graphics g)
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
doDrawing(g2d);
private void doDrawing(Graphics2D g2d)
g2d.drawImage(fighter, x, y, null);
@Override
public void actionPerformed(ActionEvent arg0)
this.repaint();
public void moveLeft()
x -= 10;
public void moveRight()
x += 10;
This is the BackGround:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Background extends JPanel implements ActionListener
private static final long serialVersionUID = 1L;
private BufferedImage backGround;
private Timer timer;
public Background()
this.setSize(Frame.WIDTH, Frame.HEIGHT);
this.setBackground(new Color(0, 0, 0, 0));
timer = new Timer(Frame.DELAY, this);
try
backGround = ImageIO.read(this.getClass().getResource("background.png"));
catch (IOException e)
e.printStackTrace();
timer.start();
@Override
public void paintComponent(Graphics g)
super.paintComponent(g);
g.drawImage(backGround, 0, 0, null);
@Override
public void actionPerformed(ActionEvent e)
this.repaint();
I expect the sprites not to flicker and not to lag(which is not happening after lots of trials).
java swing user-interface jframe jpanel
I have made 2 JPanels, Panel and BackGround, in a JFrame. I am dynamically painting the Panel after 10ms(using a Timer), but the BackGround is only painted once at the beginning of the game. The Panel is responsible for the displaying of the fighters(spacecrafts), the projectiles and the aliens. The BackGround is responsible for the displaying of the background scene which is non-dynamic. The paintComponent(Graphics) method does paint the fighters and the projectiles, but flickers when they are updating. Can someone find the cause.
This is my Frame:
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class Frame extends JFrame
private static final long serialVersionUID = 1L;
public static final int WIDTH = 1280;
public static final int HEIGHT = 720;
public static final int DELAY = 10;
private Panel panel;
private Background bg;
public Frame()
panel = new Panel();
bg = new Background();
initComponents();
public void initComponents()
this.setSize(WIDTH, HEIGHT);
this.add(bg);
this.add(panel);
this.setLocationRelativeTo(null);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.addKeyListener(new KeyAdapter()
@Override
public void keyPressed(KeyEvent e)
if(e.getKeyCode() == KeyEvent.VK_LEFT) panel.moveLeft();
else if(e.getKeyCode() == KeyEvent.VK_RIGHT) panel.moveRight();
);
public static void main(String args[])
SwingUtilities.invokeLater(new Runnable()
@Override
public void run()
new Frame().setVisible(true);
);
This is my Panel:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Panel extends JPanel implements ActionListener
private static final long serialVersionUID = 1L;
public static final int DELAY = Frame.DELAY;
private Timer timer;
private BufferedImage fighter;
int x, y;
public Panel()
timer = new Timer(DELAY, this);
try
fighter = ImageIO.read(this.getClass().getResource("fighter.png"));
catch (IOException e)
e.printStackTrace();
initComponents();
timer.start();
public void initComponents()
this.setSize(Frame.WIDTH, Frame.HEIGHT);
this.setDoubleBuffered(true);
this.setBackground(new Color(0, 0, 0, 0));
x = 150;
y = 200;
@Override
public void paintComponent(Graphics g)
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
doDrawing(g2d);
private void doDrawing(Graphics2D g2d)
g2d.drawImage(fighter, x, y, null);
@Override
public void actionPerformed(ActionEvent arg0)
this.repaint();
public void moveLeft()
x -= 10;
public void moveRight()
x += 10;
This is the BackGround:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Background extends JPanel implements ActionListener
private static final long serialVersionUID = 1L;
private BufferedImage backGround;
private Timer timer;
public Background()
this.setSize(Frame.WIDTH, Frame.HEIGHT);
this.setBackground(new Color(0, 0, 0, 0));
timer = new Timer(Frame.DELAY, this);
try
backGround = ImageIO.read(this.getClass().getResource("background.png"));
catch (IOException e)
e.printStackTrace();
timer.start();
@Override
public void paintComponent(Graphics g)
super.paintComponent(g);
g.drawImage(backGround, 0, 0, null);
@Override
public void actionPerformed(ActionEvent e)
this.repaint();
I expect the sprites not to flicker and not to lag(which is not happening after lots of trials).
java swing user-interface jframe jpanel
java swing user-interface jframe jpanel
edited Mar 24 at 16:05
Aniket Das
asked Mar 24 at 12:29
Aniket DasAniket Das
32
32
1
Get therepaint()
call out of your painting method as it never belongs there
– Hovercraft Full Of Eels
Mar 24 at 12:31
@HovercraftFullOfEels I did not get that.
– Aniket Das
Mar 24 at 12:37
1
stackoverflow.com/questions/30561690/…
– Hovercraft Full Of Eels
Mar 24 at 12:42
1
1) For better help sooner, edit to add a minimal reproducible example or Short, Self Contained, Correct Example. 2) Use a logical and consistent form of indenting code lines and blocks. The indentation is intended to make the flow of the code easier to follow! 3) EveryJComponent
is anImageObserver
, sog.drawImage(backGround, 0, 0, null);
should better beg.drawImage(backGround, 0, 0, this);
– Andrew Thompson
Mar 24 at 14:06
1
@AndrewThompson is not asking for the whole program but rather a MCVE/SSCCE. You will want to read the links that he gave you as they will explain exactly what he is (we are) requesting.
– Hovercraft Full Of Eels
Mar 24 at 14:59
|
show 5 more comments
1
Get therepaint()
call out of your painting method as it never belongs there
– Hovercraft Full Of Eels
Mar 24 at 12:31
@HovercraftFullOfEels I did not get that.
– Aniket Das
Mar 24 at 12:37
1
stackoverflow.com/questions/30561690/…
– Hovercraft Full Of Eels
Mar 24 at 12:42
1
1) For better help sooner, edit to add a minimal reproducible example or Short, Self Contained, Correct Example. 2) Use a logical and consistent form of indenting code lines and blocks. The indentation is intended to make the flow of the code easier to follow! 3) EveryJComponent
is anImageObserver
, sog.drawImage(backGround, 0, 0, null);
should better beg.drawImage(backGround, 0, 0, this);
– Andrew Thompson
Mar 24 at 14:06
1
@AndrewThompson is not asking for the whole program but rather a MCVE/SSCCE. You will want to read the links that he gave you as they will explain exactly what he is (we are) requesting.
– Hovercraft Full Of Eels
Mar 24 at 14:59
1
1
Get the
repaint()
call out of your painting method as it never belongs there– Hovercraft Full Of Eels
Mar 24 at 12:31
Get the
repaint()
call out of your painting method as it never belongs there– Hovercraft Full Of Eels
Mar 24 at 12:31
@HovercraftFullOfEels I did not get that.
– Aniket Das
Mar 24 at 12:37
@HovercraftFullOfEels I did not get that.
– Aniket Das
Mar 24 at 12:37
1
1
stackoverflow.com/questions/30561690/…
– Hovercraft Full Of Eels
Mar 24 at 12:42
stackoverflow.com/questions/30561690/…
– Hovercraft Full Of Eels
Mar 24 at 12:42
1
1
1) For better help sooner, edit to add a minimal reproducible example or Short, Self Contained, Correct Example. 2) Use a logical and consistent form of indenting code lines and blocks. The indentation is intended to make the flow of the code easier to follow! 3) Every
JComponent
is an ImageObserver
, so g.drawImage(backGround, 0, 0, null);
should better be g.drawImage(backGround, 0, 0, this);
– Andrew Thompson
Mar 24 at 14:06
1) For better help sooner, edit to add a minimal reproducible example or Short, Self Contained, Correct Example. 2) Use a logical and consistent form of indenting code lines and blocks. The indentation is intended to make the flow of the code easier to follow! 3) Every
JComponent
is an ImageObserver
, so g.drawImage(backGround, 0, 0, null);
should better be g.drawImage(backGround, 0, 0, this);
– Andrew Thompson
Mar 24 at 14:06
1
1
@AndrewThompson is not asking for the whole program but rather a MCVE/SSCCE. You will want to read the links that he gave you as they will explain exactly what he is (we are) requesting.
– Hovercraft Full Of Eels
Mar 24 at 14:59
@AndrewThompson is not asking for the whole program but rather a MCVE/SSCCE. You will want to read the links that he gave you as they will explain exactly what he is (we are) requesting.
– Hovercraft Full Of Eels
Mar 24 at 14:59
|
show 5 more comments
1 Answer
1
active
oldest
votes
- Do not call repaint within a painting method
- Get rid of the Background class and do all drawing in one JPanel. For example:
- See example below of both as well as of MCVE design
- whole thing can be copy/pasted into IDE and run
- uses images available to all online, not on disk
- I would also remove the Timer that simply calls
repaint()
and instead either- call repaint from within the KeyListener
- or use the timer to do the actual sprite movement code (with
repaint()
). This would be useful if you want continuous movement
Example MCVE:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;
public class Frame1 extends JFrame
// Image attribution:
// By Adam Evans - M31, the Andromeda Galaxy (now with h-alpha)
// Uploaded by NotFromUtrecht, CC BY 2.0,
// https://commons.wikimedia.org/w/index.php?curid=12654493
public static final String ANDROMEDA_IMAGE = "https://upload.wikimedia.org/wikipedia/commons/"
+ "thumb/9/98/Andromeda_Galaxy_%28with_h-alpha%29.jpg/"
+ "1280px-Andromeda_Galaxy_%28with_h-alpha%29.jpg";
public static final String SPRITE_IMAGE = "https://upload.wikimedia.org/wikipedia/commons/"
+ "thumb/a/a1/Glossy_3d_blue_blue2.png/" + "120px-Glossy_3d_blue_blue2.png";
private static final long serialVersionUID = 1L;
public static final int WIDTH = 1280;
public static final int HEIGHT = 720;
public static final int DELAY = 10;
private Panel1 panel;
// private Background bg;
public Frame1()
panel = new Panel1();
// bg = new Background();
initComponents();
public void initComponents()
this.setSize(WIDTH, HEIGHT);
// this.add(bg);
this.add(panel);
this.setLocationRelativeTo(null);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.addKeyListener(new KeyAdapter()
@Override
public void keyPressed(KeyEvent e)
if (e.getKeyCode() == KeyEvent.VK_LEFT)
panel.moveLeft();
else if (e.getKeyCode() == KeyEvent.VK_RIGHT)
panel.moveRight();
);
public static void main(String args[])
SwingUtilities.invokeLater(new Runnable()
@Override
public void run()
new Frame1().setVisible(true);
);
class Panel1 extends JPanel implements ActionListener
private static final long serialVersionUID = 1L;
public static final int DELAY = Frame1.DELAY;
private Timer timer;
private BufferedImage fighter;
private BufferedImage background;
int x, y;
public Panel1()
timer = new Timer(DELAY, this);
try
URL url = new URL(Frame1.SPRITE_IMAGE);
// fighter = ImageIO.read(this.getClass().getResource("fighter.png"));
fighter = ImageIO.read(url);
url = new URL(Frame1.ANDROMEDA_IMAGE);
background = ImageIO.read(url);
catch (IOException e)
e.printStackTrace();
initComponents();
timer.start();
public void initComponents()
this.setSize(Frame1.WIDTH, Frame1.HEIGHT);
this.setDoubleBuffered(true);
this.setBackground(new Color(0, 0, 0, 0));
x = 150;
y = 200;
@Override
public void paintComponent(Graphics g)
super.paintComponent(g);
g.drawImage(background, 0, 0, this);
g.drawImage(fighter, x, y, this);
@Override
public void actionPerformed(ActionEvent arg0)
this.repaint();
public void moveLeft()
x -= 10;
public void moveRight()
x += 10;
I know that I can do this, but the repaint method causes lag, and I don't wanted to repaint the entire screen. So I decided to make another JPanel where the background is painted permanently, so that I only had to repaint some specific areas in the upper panel. Is this even possible?
– Aniket Das
Mar 24 at 16:31
@AniketDas: possible but not desirable, and most certainly will not improve your animation efficiency or quality. If you're concerned about repaints, then a better solution is to call a different repaint overload method, one that accepts 4 int parameters so you repaint a limited region.
– Hovercraft Full Of Eels
Mar 24 at 16:35
"will not improve your animation efficiency or quality", okay I got you. Thank You for your suggestions.
– Aniket Das
Mar 24 at 16:46
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55323809%2fhow-to-fix-flickering-of-dynamic-moving-images-over-a-jpanel-that-is-on-top-of-a%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
- Do not call repaint within a painting method
- Get rid of the Background class and do all drawing in one JPanel. For example:
- See example below of both as well as of MCVE design
- whole thing can be copy/pasted into IDE and run
- uses images available to all online, not on disk
- I would also remove the Timer that simply calls
repaint()
and instead either- call repaint from within the KeyListener
- or use the timer to do the actual sprite movement code (with
repaint()
). This would be useful if you want continuous movement
Example MCVE:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;
public class Frame1 extends JFrame
// Image attribution:
// By Adam Evans - M31, the Andromeda Galaxy (now with h-alpha)
// Uploaded by NotFromUtrecht, CC BY 2.0,
// https://commons.wikimedia.org/w/index.php?curid=12654493
public static final String ANDROMEDA_IMAGE = "https://upload.wikimedia.org/wikipedia/commons/"
+ "thumb/9/98/Andromeda_Galaxy_%28with_h-alpha%29.jpg/"
+ "1280px-Andromeda_Galaxy_%28with_h-alpha%29.jpg";
public static final String SPRITE_IMAGE = "https://upload.wikimedia.org/wikipedia/commons/"
+ "thumb/a/a1/Glossy_3d_blue_blue2.png/" + "120px-Glossy_3d_blue_blue2.png";
private static final long serialVersionUID = 1L;
public static final int WIDTH = 1280;
public static final int HEIGHT = 720;
public static final int DELAY = 10;
private Panel1 panel;
// private Background bg;
public Frame1()
panel = new Panel1();
// bg = new Background();
initComponents();
public void initComponents()
this.setSize(WIDTH, HEIGHT);
// this.add(bg);
this.add(panel);
this.setLocationRelativeTo(null);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.addKeyListener(new KeyAdapter()
@Override
public void keyPressed(KeyEvent e)
if (e.getKeyCode() == KeyEvent.VK_LEFT)
panel.moveLeft();
else if (e.getKeyCode() == KeyEvent.VK_RIGHT)
panel.moveRight();
);
public static void main(String args[])
SwingUtilities.invokeLater(new Runnable()
@Override
public void run()
new Frame1().setVisible(true);
);
class Panel1 extends JPanel implements ActionListener
private static final long serialVersionUID = 1L;
public static final int DELAY = Frame1.DELAY;
private Timer timer;
private BufferedImage fighter;
private BufferedImage background;
int x, y;
public Panel1()
timer = new Timer(DELAY, this);
try
URL url = new URL(Frame1.SPRITE_IMAGE);
// fighter = ImageIO.read(this.getClass().getResource("fighter.png"));
fighter = ImageIO.read(url);
url = new URL(Frame1.ANDROMEDA_IMAGE);
background = ImageIO.read(url);
catch (IOException e)
e.printStackTrace();
initComponents();
timer.start();
public void initComponents()
this.setSize(Frame1.WIDTH, Frame1.HEIGHT);
this.setDoubleBuffered(true);
this.setBackground(new Color(0, 0, 0, 0));
x = 150;
y = 200;
@Override
public void paintComponent(Graphics g)
super.paintComponent(g);
g.drawImage(background, 0, 0, this);
g.drawImage(fighter, x, y, this);
@Override
public void actionPerformed(ActionEvent arg0)
this.repaint();
public void moveLeft()
x -= 10;
public void moveRight()
x += 10;
I know that I can do this, but the repaint method causes lag, and I don't wanted to repaint the entire screen. So I decided to make another JPanel where the background is painted permanently, so that I only had to repaint some specific areas in the upper panel. Is this even possible?
– Aniket Das
Mar 24 at 16:31
@AniketDas: possible but not desirable, and most certainly will not improve your animation efficiency or quality. If you're concerned about repaints, then a better solution is to call a different repaint overload method, one that accepts 4 int parameters so you repaint a limited region.
– Hovercraft Full Of Eels
Mar 24 at 16:35
"will not improve your animation efficiency or quality", okay I got you. Thank You for your suggestions.
– Aniket Das
Mar 24 at 16:46
add a comment |
- Do not call repaint within a painting method
- Get rid of the Background class and do all drawing in one JPanel. For example:
- See example below of both as well as of MCVE design
- whole thing can be copy/pasted into IDE and run
- uses images available to all online, not on disk
- I would also remove the Timer that simply calls
repaint()
and instead either- call repaint from within the KeyListener
- or use the timer to do the actual sprite movement code (with
repaint()
). This would be useful if you want continuous movement
Example MCVE:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;
public class Frame1 extends JFrame
// Image attribution:
// By Adam Evans - M31, the Andromeda Galaxy (now with h-alpha)
// Uploaded by NotFromUtrecht, CC BY 2.0,
// https://commons.wikimedia.org/w/index.php?curid=12654493
public static final String ANDROMEDA_IMAGE = "https://upload.wikimedia.org/wikipedia/commons/"
+ "thumb/9/98/Andromeda_Galaxy_%28with_h-alpha%29.jpg/"
+ "1280px-Andromeda_Galaxy_%28with_h-alpha%29.jpg";
public static final String SPRITE_IMAGE = "https://upload.wikimedia.org/wikipedia/commons/"
+ "thumb/a/a1/Glossy_3d_blue_blue2.png/" + "120px-Glossy_3d_blue_blue2.png";
private static final long serialVersionUID = 1L;
public static final int WIDTH = 1280;
public static final int HEIGHT = 720;
public static final int DELAY = 10;
private Panel1 panel;
// private Background bg;
public Frame1()
panel = new Panel1();
// bg = new Background();
initComponents();
public void initComponents()
this.setSize(WIDTH, HEIGHT);
// this.add(bg);
this.add(panel);
this.setLocationRelativeTo(null);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.addKeyListener(new KeyAdapter()
@Override
public void keyPressed(KeyEvent e)
if (e.getKeyCode() == KeyEvent.VK_LEFT)
panel.moveLeft();
else if (e.getKeyCode() == KeyEvent.VK_RIGHT)
panel.moveRight();
);
public static void main(String args[])
SwingUtilities.invokeLater(new Runnable()
@Override
public void run()
new Frame1().setVisible(true);
);
class Panel1 extends JPanel implements ActionListener
private static final long serialVersionUID = 1L;
public static final int DELAY = Frame1.DELAY;
private Timer timer;
private BufferedImage fighter;
private BufferedImage background;
int x, y;
public Panel1()
timer = new Timer(DELAY, this);
try
URL url = new URL(Frame1.SPRITE_IMAGE);
// fighter = ImageIO.read(this.getClass().getResource("fighter.png"));
fighter = ImageIO.read(url);
url = new URL(Frame1.ANDROMEDA_IMAGE);
background = ImageIO.read(url);
catch (IOException e)
e.printStackTrace();
initComponents();
timer.start();
public void initComponents()
this.setSize(Frame1.WIDTH, Frame1.HEIGHT);
this.setDoubleBuffered(true);
this.setBackground(new Color(0, 0, 0, 0));
x = 150;
y = 200;
@Override
public void paintComponent(Graphics g)
super.paintComponent(g);
g.drawImage(background, 0, 0, this);
g.drawImage(fighter, x, y, this);
@Override
public void actionPerformed(ActionEvent arg0)
this.repaint();
public void moveLeft()
x -= 10;
public void moveRight()
x += 10;
I know that I can do this, but the repaint method causes lag, and I don't wanted to repaint the entire screen. So I decided to make another JPanel where the background is painted permanently, so that I only had to repaint some specific areas in the upper panel. Is this even possible?
– Aniket Das
Mar 24 at 16:31
@AniketDas: possible but not desirable, and most certainly will not improve your animation efficiency or quality. If you're concerned about repaints, then a better solution is to call a different repaint overload method, one that accepts 4 int parameters so you repaint a limited region.
– Hovercraft Full Of Eels
Mar 24 at 16:35
"will not improve your animation efficiency or quality", okay I got you. Thank You for your suggestions.
– Aniket Das
Mar 24 at 16:46
add a comment |
- Do not call repaint within a painting method
- Get rid of the Background class and do all drawing in one JPanel. For example:
- See example below of both as well as of MCVE design
- whole thing can be copy/pasted into IDE and run
- uses images available to all online, not on disk
- I would also remove the Timer that simply calls
repaint()
and instead either- call repaint from within the KeyListener
- or use the timer to do the actual sprite movement code (with
repaint()
). This would be useful if you want continuous movement
Example MCVE:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;
public class Frame1 extends JFrame
// Image attribution:
// By Adam Evans - M31, the Andromeda Galaxy (now with h-alpha)
// Uploaded by NotFromUtrecht, CC BY 2.0,
// https://commons.wikimedia.org/w/index.php?curid=12654493
public static final String ANDROMEDA_IMAGE = "https://upload.wikimedia.org/wikipedia/commons/"
+ "thumb/9/98/Andromeda_Galaxy_%28with_h-alpha%29.jpg/"
+ "1280px-Andromeda_Galaxy_%28with_h-alpha%29.jpg";
public static final String SPRITE_IMAGE = "https://upload.wikimedia.org/wikipedia/commons/"
+ "thumb/a/a1/Glossy_3d_blue_blue2.png/" + "120px-Glossy_3d_blue_blue2.png";
private static final long serialVersionUID = 1L;
public static final int WIDTH = 1280;
public static final int HEIGHT = 720;
public static final int DELAY = 10;
private Panel1 panel;
// private Background bg;
public Frame1()
panel = new Panel1();
// bg = new Background();
initComponents();
public void initComponents()
this.setSize(WIDTH, HEIGHT);
// this.add(bg);
this.add(panel);
this.setLocationRelativeTo(null);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.addKeyListener(new KeyAdapter()
@Override
public void keyPressed(KeyEvent e)
if (e.getKeyCode() == KeyEvent.VK_LEFT)
panel.moveLeft();
else if (e.getKeyCode() == KeyEvent.VK_RIGHT)
panel.moveRight();
);
public static void main(String args[])
SwingUtilities.invokeLater(new Runnable()
@Override
public void run()
new Frame1().setVisible(true);
);
class Panel1 extends JPanel implements ActionListener
private static final long serialVersionUID = 1L;
public static final int DELAY = Frame1.DELAY;
private Timer timer;
private BufferedImage fighter;
private BufferedImage background;
int x, y;
public Panel1()
timer = new Timer(DELAY, this);
try
URL url = new URL(Frame1.SPRITE_IMAGE);
// fighter = ImageIO.read(this.getClass().getResource("fighter.png"));
fighter = ImageIO.read(url);
url = new URL(Frame1.ANDROMEDA_IMAGE);
background = ImageIO.read(url);
catch (IOException e)
e.printStackTrace();
initComponents();
timer.start();
public void initComponents()
this.setSize(Frame1.WIDTH, Frame1.HEIGHT);
this.setDoubleBuffered(true);
this.setBackground(new Color(0, 0, 0, 0));
x = 150;
y = 200;
@Override
public void paintComponent(Graphics g)
super.paintComponent(g);
g.drawImage(background, 0, 0, this);
g.drawImage(fighter, x, y, this);
@Override
public void actionPerformed(ActionEvent arg0)
this.repaint();
public void moveLeft()
x -= 10;
public void moveRight()
x += 10;
- Do not call repaint within a painting method
- Get rid of the Background class and do all drawing in one JPanel. For example:
- See example below of both as well as of MCVE design
- whole thing can be copy/pasted into IDE and run
- uses images available to all online, not on disk
- I would also remove the Timer that simply calls
repaint()
and instead either- call repaint from within the KeyListener
- or use the timer to do the actual sprite movement code (with
repaint()
). This would be useful if you want continuous movement
Example MCVE:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;
public class Frame1 extends JFrame
// Image attribution:
// By Adam Evans - M31, the Andromeda Galaxy (now with h-alpha)
// Uploaded by NotFromUtrecht, CC BY 2.0,
// https://commons.wikimedia.org/w/index.php?curid=12654493
public static final String ANDROMEDA_IMAGE = "https://upload.wikimedia.org/wikipedia/commons/"
+ "thumb/9/98/Andromeda_Galaxy_%28with_h-alpha%29.jpg/"
+ "1280px-Andromeda_Galaxy_%28with_h-alpha%29.jpg";
public static final String SPRITE_IMAGE = "https://upload.wikimedia.org/wikipedia/commons/"
+ "thumb/a/a1/Glossy_3d_blue_blue2.png/" + "120px-Glossy_3d_blue_blue2.png";
private static final long serialVersionUID = 1L;
public static final int WIDTH = 1280;
public static final int HEIGHT = 720;
public static final int DELAY = 10;
private Panel1 panel;
// private Background bg;
public Frame1()
panel = new Panel1();
// bg = new Background();
initComponents();
public void initComponents()
this.setSize(WIDTH, HEIGHT);
// this.add(bg);
this.add(panel);
this.setLocationRelativeTo(null);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.addKeyListener(new KeyAdapter()
@Override
public void keyPressed(KeyEvent e)
if (e.getKeyCode() == KeyEvent.VK_LEFT)
panel.moveLeft();
else if (e.getKeyCode() == KeyEvent.VK_RIGHT)
panel.moveRight();
);
public static void main(String args[])
SwingUtilities.invokeLater(new Runnable()
@Override
public void run()
new Frame1().setVisible(true);
);
class Panel1 extends JPanel implements ActionListener
private static final long serialVersionUID = 1L;
public static final int DELAY = Frame1.DELAY;
private Timer timer;
private BufferedImage fighter;
private BufferedImage background;
int x, y;
public Panel1()
timer = new Timer(DELAY, this);
try
URL url = new URL(Frame1.SPRITE_IMAGE);
// fighter = ImageIO.read(this.getClass().getResource("fighter.png"));
fighter = ImageIO.read(url);
url = new URL(Frame1.ANDROMEDA_IMAGE);
background = ImageIO.read(url);
catch (IOException e)
e.printStackTrace();
initComponents();
timer.start();
public void initComponents()
this.setSize(Frame1.WIDTH, Frame1.HEIGHT);
this.setDoubleBuffered(true);
this.setBackground(new Color(0, 0, 0, 0));
x = 150;
y = 200;
@Override
public void paintComponent(Graphics g)
super.paintComponent(g);
g.drawImage(background, 0, 0, this);
g.drawImage(fighter, x, y, this);
@Override
public void actionPerformed(ActionEvent arg0)
this.repaint();
public void moveLeft()
x -= 10;
public void moveRight()
x += 10;
edited Mar 24 at 16:24
answered Mar 24 at 16:17
Hovercraft Full Of EelsHovercraft Full Of Eels
264k20214320
264k20214320
I know that I can do this, but the repaint method causes lag, and I don't wanted to repaint the entire screen. So I decided to make another JPanel where the background is painted permanently, so that I only had to repaint some specific areas in the upper panel. Is this even possible?
– Aniket Das
Mar 24 at 16:31
@AniketDas: possible but not desirable, and most certainly will not improve your animation efficiency or quality. If you're concerned about repaints, then a better solution is to call a different repaint overload method, one that accepts 4 int parameters so you repaint a limited region.
– Hovercraft Full Of Eels
Mar 24 at 16:35
"will not improve your animation efficiency or quality", okay I got you. Thank You for your suggestions.
– Aniket Das
Mar 24 at 16:46
add a comment |
I know that I can do this, but the repaint method causes lag, and I don't wanted to repaint the entire screen. So I decided to make another JPanel where the background is painted permanently, so that I only had to repaint some specific areas in the upper panel. Is this even possible?
– Aniket Das
Mar 24 at 16:31
@AniketDas: possible but not desirable, and most certainly will not improve your animation efficiency or quality. If you're concerned about repaints, then a better solution is to call a different repaint overload method, one that accepts 4 int parameters so you repaint a limited region.
– Hovercraft Full Of Eels
Mar 24 at 16:35
"will not improve your animation efficiency or quality", okay I got you. Thank You for your suggestions.
– Aniket Das
Mar 24 at 16:46
I know that I can do this, but the repaint method causes lag, and I don't wanted to repaint the entire screen. So I decided to make another JPanel where the background is painted permanently, so that I only had to repaint some specific areas in the upper panel. Is this even possible?
– Aniket Das
Mar 24 at 16:31
I know that I can do this, but the repaint method causes lag, and I don't wanted to repaint the entire screen. So I decided to make another JPanel where the background is painted permanently, so that I only had to repaint some specific areas in the upper panel. Is this even possible?
– Aniket Das
Mar 24 at 16:31
@AniketDas: possible but not desirable, and most certainly will not improve your animation efficiency or quality. If you're concerned about repaints, then a better solution is to call a different repaint overload method, one that accepts 4 int parameters so you repaint a limited region.
– Hovercraft Full Of Eels
Mar 24 at 16:35
@AniketDas: possible but not desirable, and most certainly will not improve your animation efficiency or quality. If you're concerned about repaints, then a better solution is to call a different repaint overload method, one that accepts 4 int parameters so you repaint a limited region.
– Hovercraft Full Of Eels
Mar 24 at 16:35
"will not improve your animation efficiency or quality", okay I got you. Thank You for your suggestions.
– Aniket Das
Mar 24 at 16:46
"will not improve your animation efficiency or quality", okay I got you. Thank You for your suggestions.
– Aniket Das
Mar 24 at 16:46
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55323809%2fhow-to-fix-flickering-of-dynamic-moving-images-over-a-jpanel-that-is-on-top-of-a%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Get the
repaint()
call out of your painting method as it never belongs there– Hovercraft Full Of Eels
Mar 24 at 12:31
@HovercraftFullOfEels I did not get that.
– Aniket Das
Mar 24 at 12:37
1
stackoverflow.com/questions/30561690/…
– Hovercraft Full Of Eels
Mar 24 at 12:42
1
1) For better help sooner, edit to add a minimal reproducible example or Short, Self Contained, Correct Example. 2) Use a logical and consistent form of indenting code lines and blocks. The indentation is intended to make the flow of the code easier to follow! 3) Every
JComponent
is anImageObserver
, sog.drawImage(backGround, 0, 0, null);
should better beg.drawImage(backGround, 0, 0, this);
– Andrew Thompson
Mar 24 at 14:06
1
@AndrewThompson is not asking for the whole program but rather a MCVE/SSCCE. You will want to read the links that he gave you as they will explain exactly what he is (we are) requesting.
– Hovercraft Full Of Eels
Mar 24 at 14:59