Il programma apre una finestra che genera linee con alcune proprietà randomizzate. Vorrei però far disegnare una linea ogni secondo, come devo usare il timer? non riesco a farlo
codice:import java.awt.*; import javax.swing.*; import javax.swing.event.*; import java.awt.event.*; public class Ex3 extends JFrame{ public int WIDTH = 400; public int HEIGHT = 400; public Ex3(){ Container c = this.getContentPane(); this.setBackground(Color.WHITE); Toolkit tk = Toolkit.getDefaultToolkit(); int SCREEN_W = tk.getScreenSize().width; int SCREEN_H = tk.getScreenSize().height; this.setBounds((SCREEN_W - WIDTH)/2,(SCREEN_H - HEIGHT)/2,WIDTH,HEIGHT); this.setTitle("Esercizio 1"); this.setVisible(true); Dimension size = getSize(); } public void paint(Graphics g){ Dimension size = getSize(); int x,y,x1,y1; float[] stroke = {2.0f,3.0f,4.0f,5.0f}; for(int i = 0;i<=100;i++) { x = (int)(Math.random()*(size.width-30)); y = (int)(Math.random()*(size.width-30)); Graphics2D g2d = (Graphics2D)g; /* Per ottenere le linee con gradienti Color g1 = new Color((int)(Math.random()*256),(int)(Math.random()*256),(int)(Math.random()*256)); Color g2 = new Color((int)(Math.random()*256),(int)(Math.random()*256),(int)(Math.random()*256)); g2d.setPaint(new GradientPaint(x,y,g1,(size.width-30)-x,(size.width-30)-y,g2,true)); */ g.setColor(new Color((int)(Math.random()*256),(int)(Math.random()*256),(int)(Math.random()*256))); float s = stroke[(int)(Math.random()*3)]; g2d.setStroke(new BasicStroke(s)); g.drawLine(x,y,(size.width-30)-x,(size.width-30)-y); } } public static void main(String[] args) { Ex3 app = new Ex3(); app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }


Rispondi quotando