repaint method inside run()How do I paint multiple objetcs that move at different speeds in Java?Java Swing revalidate() vs repaint()'Must Override a Superclass Method' Errors after importing a project into EclipseRun a single test method with mavenHow to make mock to void methods with MockitoJava: when to use static methodsHow can I use abstract method in J2ME?How to reference a method in javadoc?Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?Drawing an image in JScrollPane within scaleHow does double buffering in applets work?

A word that means "blending into a community too much"

Did Apple bundle a specific monitor with the Apple II+ for schools?

How to write a convincing religious myth?

CircuiTikZ: How to draw contactor coil?

Why is Na5 not played in this line of the French Defense, Advance Variation?

How to add arrows in smartdiagram (descriptive diagram)

Why do radiation hardened IC packages often have long leads?

C++ logging library

First sign that you should look for another job?

UTC timestamp format for launch vehicles

Analogy between an unknown in an argument, and a contradiction in the principle of explosion

Ordinal analysis and proofs of consistency

What is the color of artificial intelligence?

tabular: caption and align problem

Is there a DSLR/mirorless camera with minimal options like a classic, simple SLR?

Can the removal of a duty-free sales trolley result in a measurable reduction in emissions?

Should I refuse to be named as co-author of a low quality paper?

How creative should the DM let an artificer be in terms of what they can build?

Live action TV show where High school Kids go into the virtual world and have to clear levels

Who won a Game of Bar Dice?

What would prevent chimeras from reproducing with each other?

What is the logic behind charging tax _in the form of money_ for owning property when the property does not produce money?

Are polynomials with the same roots identical?

I've been given a project I can't complete, what should I do?



repaint method inside run()


How do I paint multiple objetcs that move at different speeds in Java?Java Swing revalidate() vs repaint()'Must Override a Superclass Method' Errors after importing a project into EclipseRun a single test method with mavenHow to make mock to void methods with MockitoJava: when to use static methodsHow can I use abstract method in J2ME?How to reference a method in javadoc?Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?Drawing an image in JScrollPane within scaleHow does double buffering in applets work?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Timer;
import java.util.TimerTask;

public class Hunter extends JFrame implements KeyListener


public class helper extends TimerTask

@Override
public void run()
int randomm=(int)(Math.random()*4);
int randommm = (int) ((Math.random() * 490) + 10);
obj obj1 = new Hunter().new obj(randommm, 0);
MyThreadTS k = new MyThreadTS(obj1, getGraphics());
MyThreadS k1 = new MyThreadS(obj1, getGraphics());
MyThreadM k2= new MyThreadM(obj1, getGraphics());
MyThreadF k3 = new MyThreadF(obj1, getGraphics());
MyThreadTF k4 = new MyThreadTF(obj1, getGraphics());

switch (randomm)
case 0:
k.start();
break;
case 1:
k1.start();
break;
case 2:
k2.start();
break;
case 3:
k3.start();
break;
case 4:
k4.start();




public class obj
int x,y;
public obj(int x,int y)
this.x=x;
this.y=y;


public class MyThreadTS extends Thread
Graphics g;
int x,y;
public MyThreadTS(obj obj,Graphics g)
this.x=obj.x;
this.y=obj.y;
this.g=g;

public void run()
try
while (y<491)
if ((x>=positionX && x<=positionX+50)&&
(y>=positionY && y<=positionY+50)


catch (InterruptedException e)
e.printStackTrace();




public class MyThreadS extends Thread
Graphics g;
int x,y;
public MyThreadS(obj obj,Graphics g)
this.x=obj.x;
this.y=obj.y;
this.g=g;

public void run()
try
while (y<491)

catch (InterruptedException e)
e.printStackTrace();



public class MyThreadM extends Thread
Graphics g;
int x,y;
public MyThreadM(obj obj,Graphics g)
this.x=obj.x;
this.y=obj.y;
this.g=g;

public void run()
try
while (y<491) (x+10>=positionX &&
x+10<=positionX+50)&&(y+10>=positionY &&
y+10<=positionY+50))

stop();


Thread.sleep(50);
y++;
g.setColor(Color.red);
g.fillRect(x, y, 10, 10);
//repaint();


catch (InterruptedException e)
e.printStackTrace();


public class MyThreadF extends Thread
int x,y;
Graphics g;
public MyThreadF(obj obj,Graphics g)
this.x=obj.x;
this.y=obj.y;
this.g=g;

public void run()
try
while (y<491)
if ((x>=positionX && x<=positionX+50)&&
(y>=positionY && y<=positionY+50)


catch (InterruptedException e)
e.printStackTrace();


public class MyThreadTF extends Thread
int x,y;
Graphics g;
public MyThreadTF(obj obj,Graphics g)
this.x=obj.x;
this.y=obj.y;
this.g=g;

public void run()
try
while (y <491)

catch (InterruptedException e)
e.printStackTrace();






public Hunter()
setSize(500, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
addKeyListener(this);

int positionX=250,positionY=250;
public void paint(Graphics g)
super.paint(g);
g.setColor(Color.blue);
g.fillRect(positionX,positionY,50,50);
g.setColor(Color.red);



public static void main(String [] args)

Hunter game=new Hunter();
game.setVisible(true);
Hunter.helper helper=game.new helper();
Timer timer=new Timer();
timer.schedule(helper,0, 300);

@Override
public void keyTyped(KeyEvent keyEvent)
@Override
public void keyPressed(KeyEvent keyEvent)
int keyCode=keyEvent.getKeyCode();

if (keyCode==38)
if(positionY<10)


else

positionY -= 10;
repaint();


else if(keyCode==40)
if (positionY>440)


else
positionY += 10;
repaint();


else if(keyCode==37)
if (positionX<10)


else
positionX -= 10;
repaint();


else if(keyCode==39)
if (positionX>440)


else
positionX += 10;
repaint();



@Override
public void keyReleased(KeyEvent keyEvent)



in my code i have a prey-hunter game.Blue square eat red and black rectangles.They are created randomly each 0.5 sec and they are falling down in a specific time.But i have a trouble with red and black rectangles because if i use repaint method for each new paint it doesn't work correctly.They looks cloudy.How can i fix this?



i want to create my red and black rectangles like that:



https://i.stack.imgur.com/4DPBL.png










share|improve this question



















  • 1





    Welcome to Stack Overflow. Please provide a Minimal, Complete and Verifiable example. This will increase your chance to get an appropriate answer. Furthermore it also helps you solving the question yourself. You will find more information here: stackoverflow.com/help/mcve

    – Mathias
    Mar 24 at 13:19






  • 1





    Great suggestion @Mathias, and a tip: [mcve] in a comment, automatically expands to minimal reproducible example.

    – Andrew Thompson
    Mar 24 at 14:04











  • Don't use getGraphics() for painting on a frame. Don't override paint() on a frame. Instead you should be doing custom painting on a JPanel by overriding the paintComponent() method. Then you keep an ArrayList of objects to paint. Then in the paintComponent() you iterate through the ArrayList to paint each object. Also, instead of having multiple threads you should invoke repaint() at a constant time interval. But each object can have a different velocity so it will appear to move faster/slower than other objects. Check out: stackoverflow.com/a/42358637/131872

    – camickr
    Mar 24 at 20:52

















0















import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Timer;
import java.util.TimerTask;

public class Hunter extends JFrame implements KeyListener


public class helper extends TimerTask

@Override
public void run()
int randomm=(int)(Math.random()*4);
int randommm = (int) ((Math.random() * 490) + 10);
obj obj1 = new Hunter().new obj(randommm, 0);
MyThreadTS k = new MyThreadTS(obj1, getGraphics());
MyThreadS k1 = new MyThreadS(obj1, getGraphics());
MyThreadM k2= new MyThreadM(obj1, getGraphics());
MyThreadF k3 = new MyThreadF(obj1, getGraphics());
MyThreadTF k4 = new MyThreadTF(obj1, getGraphics());

switch (randomm)
case 0:
k.start();
break;
case 1:
k1.start();
break;
case 2:
k2.start();
break;
case 3:
k3.start();
break;
case 4:
k4.start();




public class obj
int x,y;
public obj(int x,int y)
this.x=x;
this.y=y;


public class MyThreadTS extends Thread
Graphics g;
int x,y;
public MyThreadTS(obj obj,Graphics g)
this.x=obj.x;
this.y=obj.y;
this.g=g;

public void run()
try
while (y<491)
if ((x>=positionX && x<=positionX+50)&&
(y>=positionY && y<=positionY+50)


catch (InterruptedException e)
e.printStackTrace();




public class MyThreadS extends Thread
Graphics g;
int x,y;
public MyThreadS(obj obj,Graphics g)
this.x=obj.x;
this.y=obj.y;
this.g=g;

public void run()
try
while (y<491)

catch (InterruptedException e)
e.printStackTrace();



public class MyThreadM extends Thread
Graphics g;
int x,y;
public MyThreadM(obj obj,Graphics g)
this.x=obj.x;
this.y=obj.y;
this.g=g;

public void run()
try
while (y<491) (x+10>=positionX &&
x+10<=positionX+50)&&(y+10>=positionY &&
y+10<=positionY+50))

stop();


Thread.sleep(50);
y++;
g.setColor(Color.red);
g.fillRect(x, y, 10, 10);
//repaint();


catch (InterruptedException e)
e.printStackTrace();


public class MyThreadF extends Thread
int x,y;
Graphics g;
public MyThreadF(obj obj,Graphics g)
this.x=obj.x;
this.y=obj.y;
this.g=g;

public void run()
try
while (y<491)
if ((x>=positionX && x<=positionX+50)&&
(y>=positionY && y<=positionY+50)


catch (InterruptedException e)
e.printStackTrace();


public class MyThreadTF extends Thread
int x,y;
Graphics g;
public MyThreadTF(obj obj,Graphics g)
this.x=obj.x;
this.y=obj.y;
this.g=g;

public void run()
try
while (y <491)

catch (InterruptedException e)
e.printStackTrace();






public Hunter()
setSize(500, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
addKeyListener(this);

int positionX=250,positionY=250;
public void paint(Graphics g)
super.paint(g);
g.setColor(Color.blue);
g.fillRect(positionX,positionY,50,50);
g.setColor(Color.red);



public static void main(String [] args)

Hunter game=new Hunter();
game.setVisible(true);
Hunter.helper helper=game.new helper();
Timer timer=new Timer();
timer.schedule(helper,0, 300);

@Override
public void keyTyped(KeyEvent keyEvent)
@Override
public void keyPressed(KeyEvent keyEvent)
int keyCode=keyEvent.getKeyCode();

if (keyCode==38)
if(positionY<10)


else

positionY -= 10;
repaint();


else if(keyCode==40)
if (positionY>440)


else
positionY += 10;
repaint();


else if(keyCode==37)
if (positionX<10)


else
positionX -= 10;
repaint();


else if(keyCode==39)
if (positionX>440)


else
positionX += 10;
repaint();



@Override
public void keyReleased(KeyEvent keyEvent)



in my code i have a prey-hunter game.Blue square eat red and black rectangles.They are created randomly each 0.5 sec and they are falling down in a specific time.But i have a trouble with red and black rectangles because if i use repaint method for each new paint it doesn't work correctly.They looks cloudy.How can i fix this?



i want to create my red and black rectangles like that:



https://i.stack.imgur.com/4DPBL.png










share|improve this question



















  • 1





    Welcome to Stack Overflow. Please provide a Minimal, Complete and Verifiable example. This will increase your chance to get an appropriate answer. Furthermore it also helps you solving the question yourself. You will find more information here: stackoverflow.com/help/mcve

    – Mathias
    Mar 24 at 13:19






  • 1





    Great suggestion @Mathias, and a tip: [mcve] in a comment, automatically expands to minimal reproducible example.

    – Andrew Thompson
    Mar 24 at 14:04











  • Don't use getGraphics() for painting on a frame. Don't override paint() on a frame. Instead you should be doing custom painting on a JPanel by overriding the paintComponent() method. Then you keep an ArrayList of objects to paint. Then in the paintComponent() you iterate through the ArrayList to paint each object. Also, instead of having multiple threads you should invoke repaint() at a constant time interval. But each object can have a different velocity so it will appear to move faster/slower than other objects. Check out: stackoverflow.com/a/42358637/131872

    – camickr
    Mar 24 at 20:52













0












0








0








import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Timer;
import java.util.TimerTask;

public class Hunter extends JFrame implements KeyListener


public class helper extends TimerTask

@Override
public void run()
int randomm=(int)(Math.random()*4);
int randommm = (int) ((Math.random() * 490) + 10);
obj obj1 = new Hunter().new obj(randommm, 0);
MyThreadTS k = new MyThreadTS(obj1, getGraphics());
MyThreadS k1 = new MyThreadS(obj1, getGraphics());
MyThreadM k2= new MyThreadM(obj1, getGraphics());
MyThreadF k3 = new MyThreadF(obj1, getGraphics());
MyThreadTF k4 = new MyThreadTF(obj1, getGraphics());

switch (randomm)
case 0:
k.start();
break;
case 1:
k1.start();
break;
case 2:
k2.start();
break;
case 3:
k3.start();
break;
case 4:
k4.start();




public class obj
int x,y;
public obj(int x,int y)
this.x=x;
this.y=y;


public class MyThreadTS extends Thread
Graphics g;
int x,y;
public MyThreadTS(obj obj,Graphics g)
this.x=obj.x;
this.y=obj.y;
this.g=g;

public void run()
try
while (y<491)
if ((x>=positionX && x<=positionX+50)&&
(y>=positionY && y<=positionY+50)


catch (InterruptedException e)
e.printStackTrace();




public class MyThreadS extends Thread
Graphics g;
int x,y;
public MyThreadS(obj obj,Graphics g)
this.x=obj.x;
this.y=obj.y;
this.g=g;

public void run()
try
while (y<491)

catch (InterruptedException e)
e.printStackTrace();



public class MyThreadM extends Thread
Graphics g;
int x,y;
public MyThreadM(obj obj,Graphics g)
this.x=obj.x;
this.y=obj.y;
this.g=g;

public void run()
try
while (y<491) (x+10>=positionX &&
x+10<=positionX+50)&&(y+10>=positionY &&
y+10<=positionY+50))

stop();


Thread.sleep(50);
y++;
g.setColor(Color.red);
g.fillRect(x, y, 10, 10);
//repaint();


catch (InterruptedException e)
e.printStackTrace();


public class MyThreadF extends Thread
int x,y;
Graphics g;
public MyThreadF(obj obj,Graphics g)
this.x=obj.x;
this.y=obj.y;
this.g=g;

public void run()
try
while (y<491)
if ((x>=positionX && x<=positionX+50)&&
(y>=positionY && y<=positionY+50)


catch (InterruptedException e)
e.printStackTrace();


public class MyThreadTF extends Thread
int x,y;
Graphics g;
public MyThreadTF(obj obj,Graphics g)
this.x=obj.x;
this.y=obj.y;
this.g=g;

public void run()
try
while (y <491)

catch (InterruptedException e)
e.printStackTrace();






public Hunter()
setSize(500, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
addKeyListener(this);

int positionX=250,positionY=250;
public void paint(Graphics g)
super.paint(g);
g.setColor(Color.blue);
g.fillRect(positionX,positionY,50,50);
g.setColor(Color.red);



public static void main(String [] args)

Hunter game=new Hunter();
game.setVisible(true);
Hunter.helper helper=game.new helper();
Timer timer=new Timer();
timer.schedule(helper,0, 300);

@Override
public void keyTyped(KeyEvent keyEvent)
@Override
public void keyPressed(KeyEvent keyEvent)
int keyCode=keyEvent.getKeyCode();

if (keyCode==38)
if(positionY<10)


else

positionY -= 10;
repaint();


else if(keyCode==40)
if (positionY>440)


else
positionY += 10;
repaint();


else if(keyCode==37)
if (positionX<10)


else
positionX -= 10;
repaint();


else if(keyCode==39)
if (positionX>440)


else
positionX += 10;
repaint();



@Override
public void keyReleased(KeyEvent keyEvent)



in my code i have a prey-hunter game.Blue square eat red and black rectangles.They are created randomly each 0.5 sec and they are falling down in a specific time.But i have a trouble with red and black rectangles because if i use repaint method for each new paint it doesn't work correctly.They looks cloudy.How can i fix this?



i want to create my red and black rectangles like that:



https://i.stack.imgur.com/4DPBL.png










share|improve this question
















import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Timer;
import java.util.TimerTask;

public class Hunter extends JFrame implements KeyListener


public class helper extends TimerTask

@Override
public void run()
int randomm=(int)(Math.random()*4);
int randommm = (int) ((Math.random() * 490) + 10);
obj obj1 = new Hunter().new obj(randommm, 0);
MyThreadTS k = new MyThreadTS(obj1, getGraphics());
MyThreadS k1 = new MyThreadS(obj1, getGraphics());
MyThreadM k2= new MyThreadM(obj1, getGraphics());
MyThreadF k3 = new MyThreadF(obj1, getGraphics());
MyThreadTF k4 = new MyThreadTF(obj1, getGraphics());

switch (randomm)
case 0:
k.start();
break;
case 1:
k1.start();
break;
case 2:
k2.start();
break;
case 3:
k3.start();
break;
case 4:
k4.start();




public class obj
int x,y;
public obj(int x,int y)
this.x=x;
this.y=y;


public class MyThreadTS extends Thread
Graphics g;
int x,y;
public MyThreadTS(obj obj,Graphics g)
this.x=obj.x;
this.y=obj.y;
this.g=g;

public void run()
try
while (y<491)
if ((x>=positionX && x<=positionX+50)&&
(y>=positionY && y<=positionY+50)


catch (InterruptedException e)
e.printStackTrace();




public class MyThreadS extends Thread
Graphics g;
int x,y;
public MyThreadS(obj obj,Graphics g)
this.x=obj.x;
this.y=obj.y;
this.g=g;

public void run()
try
while (y<491)

catch (InterruptedException e)
e.printStackTrace();



public class MyThreadM extends Thread
Graphics g;
int x,y;
public MyThreadM(obj obj,Graphics g)
this.x=obj.x;
this.y=obj.y;
this.g=g;

public void run()
try
while (y<491) (x+10>=positionX &&
x+10<=positionX+50)&&(y+10>=positionY &&
y+10<=positionY+50))

stop();


Thread.sleep(50);
y++;
g.setColor(Color.red);
g.fillRect(x, y, 10, 10);
//repaint();


catch (InterruptedException e)
e.printStackTrace();


public class MyThreadF extends Thread
int x,y;
Graphics g;
public MyThreadF(obj obj,Graphics g)
this.x=obj.x;
this.y=obj.y;
this.g=g;

public void run()
try
while (y<491)
if ((x>=positionX && x<=positionX+50)&&
(y>=positionY && y<=positionY+50)


catch (InterruptedException e)
e.printStackTrace();


public class MyThreadTF extends Thread
int x,y;
Graphics g;
public MyThreadTF(obj obj,Graphics g)
this.x=obj.x;
this.y=obj.y;
this.g=g;

public void run()
try
while (y <491)

catch (InterruptedException e)
e.printStackTrace();






public Hunter()
setSize(500, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
addKeyListener(this);

int positionX=250,positionY=250;
public void paint(Graphics g)
super.paint(g);
g.setColor(Color.blue);
g.fillRect(positionX,positionY,50,50);
g.setColor(Color.red);



public static void main(String [] args)

Hunter game=new Hunter();
game.setVisible(true);
Hunter.helper helper=game.new helper();
Timer timer=new Timer();
timer.schedule(helper,0, 300);

@Override
public void keyTyped(KeyEvent keyEvent)
@Override
public void keyPressed(KeyEvent keyEvent)
int keyCode=keyEvent.getKeyCode();

if (keyCode==38)
if(positionY<10)


else

positionY -= 10;
repaint();


else if(keyCode==40)
if (positionY>440)


else
positionY += 10;
repaint();


else if(keyCode==37)
if (positionX<10)


else
positionX -= 10;
repaint();


else if(keyCode==39)
if (positionX>440)


else
positionX += 10;
repaint();



@Override
public void keyReleased(KeyEvent keyEvent)



in my code i have a prey-hunter game.Blue square eat red and black rectangles.They are created randomly each 0.5 sec and they are falling down in a specific time.But i have a trouble with red and black rectangles because if i use repaint method for each new paint it doesn't work correctly.They looks cloudy.How can i fix this?



i want to create my red and black rectangles like that:



https://i.stack.imgur.com/4DPBL.png







java multithreading swing awt paint






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 24 at 20:41









camickr

280k16130243




280k16130243










asked Mar 24 at 13:08









Leo2Leo2

11




11







  • 1





    Welcome to Stack Overflow. Please provide a Minimal, Complete and Verifiable example. This will increase your chance to get an appropriate answer. Furthermore it also helps you solving the question yourself. You will find more information here: stackoverflow.com/help/mcve

    – Mathias
    Mar 24 at 13:19






  • 1





    Great suggestion @Mathias, and a tip: [mcve] in a comment, automatically expands to minimal reproducible example.

    – Andrew Thompson
    Mar 24 at 14:04











  • Don't use getGraphics() for painting on a frame. Don't override paint() on a frame. Instead you should be doing custom painting on a JPanel by overriding the paintComponent() method. Then you keep an ArrayList of objects to paint. Then in the paintComponent() you iterate through the ArrayList to paint each object. Also, instead of having multiple threads you should invoke repaint() at a constant time interval. But each object can have a different velocity so it will appear to move faster/slower than other objects. Check out: stackoverflow.com/a/42358637/131872

    – camickr
    Mar 24 at 20:52












  • 1





    Welcome to Stack Overflow. Please provide a Minimal, Complete and Verifiable example. This will increase your chance to get an appropriate answer. Furthermore it also helps you solving the question yourself. You will find more information here: stackoverflow.com/help/mcve

    – Mathias
    Mar 24 at 13:19






  • 1





    Great suggestion @Mathias, and a tip: [mcve] in a comment, automatically expands to minimal reproducible example.

    – Andrew Thompson
    Mar 24 at 14:04











  • Don't use getGraphics() for painting on a frame. Don't override paint() on a frame. Instead you should be doing custom painting on a JPanel by overriding the paintComponent() method. Then you keep an ArrayList of objects to paint. Then in the paintComponent() you iterate through the ArrayList to paint each object. Also, instead of having multiple threads you should invoke repaint() at a constant time interval. But each object can have a different velocity so it will appear to move faster/slower than other objects. Check out: stackoverflow.com/a/42358637/131872

    – camickr
    Mar 24 at 20:52







1




1





Welcome to Stack Overflow. Please provide a Minimal, Complete and Verifiable example. This will increase your chance to get an appropriate answer. Furthermore it also helps you solving the question yourself. You will find more information here: stackoverflow.com/help/mcve

– Mathias
Mar 24 at 13:19





Welcome to Stack Overflow. Please provide a Minimal, Complete and Verifiable example. This will increase your chance to get an appropriate answer. Furthermore it also helps you solving the question yourself. You will find more information here: stackoverflow.com/help/mcve

– Mathias
Mar 24 at 13:19




1




1





Great suggestion @Mathias, and a tip: [mcve] in a comment, automatically expands to minimal reproducible example.

– Andrew Thompson
Mar 24 at 14:04





Great suggestion @Mathias, and a tip: [mcve] in a comment, automatically expands to minimal reproducible example.

– Andrew Thompson
Mar 24 at 14:04













Don't use getGraphics() for painting on a frame. Don't override paint() on a frame. Instead you should be doing custom painting on a JPanel by overriding the paintComponent() method. Then you keep an ArrayList of objects to paint. Then in the paintComponent() you iterate through the ArrayList to paint each object. Also, instead of having multiple threads you should invoke repaint() at a constant time interval. But each object can have a different velocity so it will appear to move faster/slower than other objects. Check out: stackoverflow.com/a/42358637/131872

– camickr
Mar 24 at 20:52





Don't use getGraphics() for painting on a frame. Don't override paint() on a frame. Instead you should be doing custom painting on a JPanel by overriding the paintComponent() method. Then you keep an ArrayList of objects to paint. Then in the paintComponent() you iterate through the ArrayList to paint each object. Also, instead of having multiple threads you should invoke repaint() at a constant time interval. But each object can have a different velocity so it will appear to move faster/slower than other objects. Check out: stackoverflow.com/a/42358637/131872

– camickr
Mar 24 at 20:52












0






active

oldest

votes












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
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55324112%2frepaint-method-inside-run%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































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.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55324112%2frepaint-method-inside-run%23new-answer', 'question_page');

);

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







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript