ciao a tutti io ho questo code :
Classe characters:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Characters extends Thread {
private int x;
private int y;
private JPanel frame;
private Image im;
public Characters(int a,int b) {
x = a;
y = b;
}
public Characters(int i, int j, JPanel frame,String path) {
this(i,j);
this.frame = frame;
im = Toolkit.getDefaultToolkit().getImage (path);
}
@Override
public void run() {
for (int i = 0; i < 22; i = i + 1) {
y += i;
try {
Thread.sleep(200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
System.out.println(i);
}
}
}
public void draw(Graphics graphics){
graphics.setColor(Color.red);
//qui metti l imagel
//graphics.drawImage(.....)
graphics.drawImage(im, x, y,frame);
}
}
Classe Main:
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.util.LinkedList;
import javax.swing.JFrame;
public class Main {
private JFrame frame;
private static final String[] paths = {"/Users/federicobarassi/NetBeansProjects/OK/src/ok/java1.gif",
"/Users/federicobarassi/NetBeansProjects/OK/src/ok/java2.gif",
"/Users/federicobarassi/NetBeansProjects/OK/src/ok/java3.gif",
"/Users/federicobarassi/NetBeansProjects/OK/src/ok/java4.gif"};
public Main() {
frame = new JFrame("Piove");
frame.setSize(new Dimension(300,300));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.getContentPane().add(getFilledCPanel(),BorderLayout.CENTER);
frame.setVisible(true);
}
private Component getFilledCPanel() {
CustomPanel panel = null;
LinkedList<Characters> list = new LinkedList<Characters>();
for (int i = 0; i < paths.length; i++) {
list.add(new Characters((i*80),0,panel,paths[i]));
}
panel = new CustomPanel(list);
for (Characters characters : list) {
characters.start();
}
return panel;
}
public static void main(String[] args) {
new Main();
}
}
Classe CustomPanel:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.util.LinkedList;
import javax.swing.JPanel;
public class CustomPanel extends JPanel {
private LinkedList<Characters> list;
public CustomPanel() {
setSize(new Dimension(300,300));
setBackground(Color.white);
}
public CustomPanel(LinkedList<Characters> a){
this();
list = a;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
for (Characters c : list) {
c.draw(g);
}
repaint();
}
}
Tenendo conto che sono abbastanza ignorante in materia ; Vorrei mandare l'azione che fa andare dall'alto verso il basso le immagini(e non viceversa) in loop infinito. Poi non riesco ad aggiungere un bottone che da il play al tutto. Qualcuno riuscirebbe a darmi una mano??
Grazie mille