Dodaj wiele obiektów Wielokąt na tej samej ramce JPanel
Mam więc klasę DrawStar, która rysuje gwiazdę za pomocą wielokąta w ten sposób:
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
int[] cX = new int[] {x, x+5, x+20, x+8, x+16, x, x-16, x-8, x-20, x-5, x};
int[] cY = new int[] {y, y+14, y+14, y+22, y+39, y+29, y+39, y+22, y+14, y+14, y};
Polygon pol = new Polygon(cX, cY, 11);
g2.setColor(this.color);
g2.draw(pol);
g2.fillPolygon(pol);
}
Następnie w mojej klasie głównej tworzę ramkę JPanel, aby narysować gwiazdy:
...
JFrame starsframe = new JFrame();
starsframe.setTitle("Random stars...");
starsframe.setSize(600, 400);
starsframe.setLocationRelativeTo(null);
starsframe.setVisible(true);
starsframe.setResizable(false);
starsframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
DrawStar star1 = new DrawStar(300, 200, CreateColor(color));
starsframe.add(star1);
DrawStar star2 = new DrawStar(400, 300, CreateColor(color));
starsframe.add(star2);
...
Jednak działa tylko z jedną gwiazdką. Jeśli dodam drugi (jak wyżej), nie zostanie narysowany (CreateColor to funkcja, którą implementuję dla koloru gwiazdy). Jak mogę dodać je cały czas w ramce? I jeszcze jedno, nawet jeśli ustawię kolor tła ramki na Czarny, po narysowaniu gwiazdy będzie on szary.
Dzięki!