Witam, jak się można przekonać po temacie, albo nie, bo może być trochę skomplikowany, potrzebuję pomocy, w utworzeniu listy, Obiektów mojej klasy, która rozszerza JComponent, oraz posiada metodę paintComponent, oto ona:
import javax.swing.*;
import javax.swing.Timer;
import java.awt.*;
import java.util.*;
import java.util.List;
public class MainShipShoot extends JComponent {
int x, y, size;
Game game;
boolean isAlive;
List shoots;
public MainShipShoot(List shoots, Game game, int X, int Y){
this.game = game;
this.shoots = shoots;
this.x = X;
this.y = Y;
size = 10;
isAlive = true;
java.util.Timer shotTimer = new java.util.Timer();
shotTimer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
if(!isAlive || y < 20){
shotTimer.cancel();
shotTimer.purge();
shoots.remove(this);
}
y--;
repaint();
}
}, 0, 2);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D shot = (Graphics2D) g;
shot.setPaint(game.colorShot);
shot.fillRect(x-5, y-20, size, size*4);
}
}
Dalej, próbuję utworzyć tą listę w innej klasie:
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.util.*;
import java.util.List;
import java.util.Set;
import java.util.Timer;
public class MainShip extends JComponent implements MouseMotionListener {
Game game;
int x, y, size;
LinkedList<MainShipShoot> shoots = new LinkedList<>();
public MainShip(Game game){
this.game = game;
addMouseMotionListener(this);
x = game.WIDTH/2;
y = game.HEIGHT/2+100;
size = 10;
Timer shootingTimer = new Timer();
shootingTimer.scheduleAtFixedRate(new TimerTask() {
public void run() {
shoots.add(new MainShipShoot(shoots, game, x-25, y-25));
shoots.add(new MainShipShoot(shoots, game, x+25, y-25));
}
}, 0, 500);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D main = (Graphics2D) g;
Graphics2D fire = (Graphics2D) g;
main.setPaint(game.colorShip);
main.fillRect(x-5, y-45, size, size*6);
main.fillRect(x-25, y-5, size*2, size);
main.fillRect(x+5, y-5, size*2, size);
main.fillRect(x-25, y+5, size, size);
main.fillRect(x+15, y+5, size, size);
main.fillRect(x-15, y-25, size*3, size*2);
fire.setPaint(game.colorFire);
fire.fillRect(x-5, y+5, size, size);
}
@Override
public void mouseMoved(MouseEvent e) {
if(e.getX()+25 <= game.WIDTH && e.getX()-25 >= 0){
x = e.getX();
repaint();
}
if(e.getY()-45 >= 0 && e.getY()+15 <= game.HEIGHT){
y = e.getY();
repaint();
}
}
Jednakże, pojawia się problem, natury takiej, że obiekty, nie rysują mi się na ekranie. Gdybym zamiast dodawania obiektów do listy w taki sposób:
shoots.add(new MainShipShoot(shoots, game, x-25, y-25));
shoots.add(new MainShipShoot(shoots, game, x+25, y-25));
Zrobił to w taki sposób:
add(new MainShipShoot(shoots, game, x-25, y-25));
add(new MainShipShoot(shoots, game, x+25, y-25));
To wszystko by się poprawnie wyświetliło, jednakże muszę dodać je do tej listy.
PS. Obiekt game, ma w sobie wszystkie niezbędne zmienne, typu Wysokość i szerokość okna, różne kolory itp.