I saw a Topic about multi thread here : Multi-threading with Swing
So I do as the answer https://stackoverflow.com/a/33908340/11445086
I implement like codes below but there's not anything movement here. I know the problem is about my doInBackGround and process implementation but I don't know how to do.I'm really new with Swing Builder so sorry if the question's silly. The program just makes many circles moving in the Panel. And each circle is a Thread that be made by Swing Worker.
these're my classes:
Robot class :
package com.mycompany.test;
import java.awt.Color;
import java.util.List;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.SwingWorker;
public class Robot extends SwingWorker< Void ,Integer> {
public int x;
public int y;
public Color color;
public final int speed = 10;
Robot(int x , int y , Color color)
{
this.x = x;
this.y = y;
this.color = color;
}
public void move_90()
{
this.y += speed;
}
public void move_270()
{
this.y -= speed;
}
public void move_180()
{
this.x += speed;
}
public void move_0()
{
this.x += speed;
}
public void move_45()
{
this.x += speed;
this.y += speed;
}
public void move_135()
{
this.x -= speed;
this.y += speed;
}
public void move_225()
{
this.x -= speed;
this.y -= speed;
}
public void move_315()
{
this.x += speed;
this.y -= speed;
}
public void move()
{
Random temp = new Random();
int rand = temp.nextInt(8);
switch(rand)
{
case 1: move_0();
break;
case 2: move_135();
break;
case 3: move_180();
break;
case 4: move_225();
break;
case 5: move_270();
break;
case 6: move_315();
break;
case 7: move_45();
break;
case 8: move_90();
break;
}
}
@Override
protected void process(List<Integer> chunks) {
while(true)
{
move();
if(x < 40) x = 40;
if(x > PlayField.width - 40) x = (PlayField.width - 40);
if(y < 40) y = 40;
if(y > PlayField.height - 40) y = (PlayField.height - 40);
try {
Thread.sleep(20);
} catch (InterruptedException ex) {
Logger.getLogger(Robot.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
@Override
protected Void doInBackground() throws Exception {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
RobotModel class :
package com.mycompany.test;
import java.awt.Color;
import java.util.LinkedList;
public class RobotModel {
public static final int MAX = 8;
public LinkedList<Robot> model = new LinkedList<Robot>();
public void add_New_Robot()
{
Robot temp = new Robot( 40 , 40 , Color.BLUE);
model.addFirst(temp);
}
}
PlayField class :
package com.mycompany.test;
import java.awt.Color;
public class PlayField {
public static int width;
public static int height;
public static Color fill_Color;
PlayField(int width , int height , Color fill_Color)
{
this.width = width;
this.height = height;
this.fill_Color = fill_Color;
}
}
RobotWorld class :
package com.mycompany.test;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;
public class RobotWorld extends JPanel {
public RobotModel robot_Model;
public RobotWorld(RobotModel robot_Model) {
super();
this.robot_Model = robot_Model;
this.setSize(PlayField.width , PlayField.height);
this.setVisible(true);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D graphic = (Graphics2D)g;
graphic.setBackground(PlayField.fill_Color);
for(Robot x : robot_Model.model )
{
graphic.setColor(x.color);
graphic.drawOval(x.x, x.y, 40, 40);
}
}
}
And finally , GameMain class :
package com.mycompany.test;
import java.awt.Color;
import javax.swing.JFrame;
public class GameMain extends JFrame {
RobotModel a;
PlayField field;
public void Game_Start()
{
Robot robot = new Robot(100, 100, Color.RED);
a = new RobotModel();
RobotWorld world = new RobotWorld(a);
world.robot_Model.add_New_Robot();
this.setSize(field.width , field.height);
this.add(world);
this.setVisible(true);
world.repaint();
}
// public void gameUpdate(Robot a , PlayField field)
// {
// a.move();
// if(a.x < 40) a.x = 40;
// if(a.x > field.width - 40) a.x = (field.width - 40);
// if(a.y < 40) a.y = 40;
// if(a.y > field.height - 40) a.y = (field.height - 40);
// }
public void gameUpdate(){
Thread gameThread = new Thread(){
public void run(){
while(true){
//execute one time step for the game
// gameUpdate(a , field);
//refresh screen
repaint();
//give other threads time
try{
Thread.sleep(5);
}catch(InterruptedException e){
// e.printStackTrace();
}
}
}
};
gameThread.start();
}
public static void main(String args[])
{
GameMain main = new GameMain();
main.Game_Start();
main.gameUpdate();
}
}