Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- public class CambiaColore extends JPanel implements Runnable{
- protected Automobile automobile;
- protected Thread th;
- protected boolean bordo=false;
- protected double lato = 100;
- protected boolean didInit=false;
- protected boolean bang=false;
- ////////////////////// COSTRUTTORE ////////////////////
- public CambiaColore(){
- //imposto le qualità specifiche del pannello appena costruito dal richiamo del costruttore
- setBackground(Color.white);
- setPreferredSize(new Dimension(600,600));
- //creo la finestra che ospiterà il lavoro
- JFrame f = new JFrame("Cambiamo il colore");
- //creo il pannello che ospiterà i bottoni
- JPanel p = new JPanel();
- p.setBackground(Color.black);
- //creo bottone star con rispettivo ascoltatore
- JButton bottonestart = new JButton("Start");
- ActionListener listenstart = new ActionListener(){
- public void actionPerformed(ActionEvent ae){
- //questo verrà eseguito alla pressione del bottone start!
- if(!bang){
- start();
- bang=true;
- }
- }
- };
- //aggiungo l'ascoltatore al bottone e il bottone al pannello p
- bottonestart.addActionListener(listenstart);
- p.add(bottonestart);
- //creo e aggiungo il bottone per lo stop
- JButton bottonestop = new JButton("Stop");
- ActionListener listenstop = new ActionListener(){
- public void actionPerformed(ActionEvent ae){
- //questo è quello che succede quando si preme Stop
- if(bang){
- automobile.x=0;
- stop();
- }
- bang=false;
- repaint();
- }
- };
- //creo bottone colore con rispettivo ascoltatore per il colore
- JButton bottonecolore = new JButton("Colore");
- ActionListener listencolore = new ActionListener(){
- public void actionPerformed(ActionEvent e) {
- //questo verrà eseguito alla pressione del bottone colore
- Color colore = new Color((int)(Math.random()*255),(int)(Math.random()*255),(int)(Math.random()*255));
- automobile.setColor(colore);
- System.out.println("Sto cambiando il colore della macchina :) il colore ora è "+colore);
- repaint();
- }
- };
- //aggiungo l'ascoltatore al bottone e il bottone al pannello p
- bottonecolore.addActionListener(listencolore);
- p.add(bottonecolore);
- //setto il Layout della finestra e aggiungo gli elementi
- f.setLayout(new BorderLayout());
- f.add(this,BorderLayout.CENTER);
- f.add(p,BorderLayout.SOUTH);
- f.pack();
- f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- f.setResizable(false);
- f.setVisible(true);
- }
- /////////////////////// METODO DI INIZIALIZZAZIONE DELL'OGGETTO CHE POI MUOVEREMO ///////////////////////
- public void init(){
- automobile = new Automobile(0, 250, lato, 0, 0, Color.red);
- didInit=true;
- }
- /////////////////////// METODI DEL THREAD ///////////////////////
- public void stop(){
- System.out.println("Fermo il thread "+th);
- th = null;
- }
- public boolean isRunning(){
- return th != null;
- }
- public void start(){
- if(!isRunning()){
- th = new Thread(this);
- System.out.println("Parte il Thread "+th);
- th.start();
- }
- }
- /////////////////////// METODO RUN ///////////////////////
- public void run(){
- while(isRunning()){ // se il thread Ë partito
- if(automobile.x == 380) bordo = true;
- else if (automobile.x == 0) bordo = false;
- if(!bordo) automobile.x+=10;
- else if(bordo) automobile.x-=10;
- System.out.println("Mi muovo...le mie cordinate sono la y:"+automobile.y+" e la x :"+automobile.x);
- repaint();
- try{
- Thread.sleep(1000);
- }catch(InterruptedException e){
- e.printStackTrace();
- }
- }
- }
- /////////////////////// METODO PAINT ///////////////////////
- public void paint (Graphics g){
- super.paint(g);
- Graphics2D g2 = (Graphics2D) g;
- g2.setRenderingHint( RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON); // antialiasing
- if(!didInit){ // se ancora non ho inizializzato, inizializzo gli oggetti, ovvero le automobili
- System.out.println("Non ho inizializzato....sto inizializzando ora :) nel metodo paint richiamo Init");
- init();
- }
- automobile.draw(g2);
- }
- /////////////////////// MAIN ///////////////////////
- public static void main(String[] args) {
- new CambiaColore();
- }
- }
- ////////////////////////********* CLASSE AUTOMOBILE ********////////////////////////
- class Automobile extends Rectangle.Double {
- protected double y,x,vx,vy;
- protected Color colore;
- public Automobile(double x, double y, double lato, double vx, double vy, Color colore){
- super(x,y,lato,lato);
- this.vx=vx;
- this.vy=vy;
- setColor(colore);
- }
- public void draw(Graphics2D g2){
- System.out.println("Ridisegno la macchina");
- g2.setColor(colore);
- g2.fill(this);
- }
- public void setColor(Color colore){
- this.colore=colore;
- }
- }
Add Comment
Please, Sign In to add comment