Guest User

Untitled

a guest
Dec 10th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.07 KB | None | 0 0
  1. import javax.swing.*;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.*;
  5.  
  6. public class CambiaColore extends JPanel implements Runnable{
  7.  
  8. protected Automobile automobile;
  9. protected Thread th;
  10. protected boolean bordo=false;
  11. protected double lato = 100;
  12. protected boolean didInit=false;
  13. protected boolean bang=false;
  14.  
  15. ////////////////////// COSTRUTTORE ////////////////////
  16. public CambiaColore(){
  17. //imposto le qualità specifiche del pannello appena costruito dal richiamo del costruttore
  18. setBackground(Color.white);
  19. setPreferredSize(new Dimension(600,600));
  20. //creo la finestra che ospiterà il lavoro
  21. JFrame f = new JFrame("Cambiamo il colore");
  22. //creo il pannello che ospiterà i bottoni
  23. JPanel p = new JPanel();
  24. p.setBackground(Color.black);
  25. //creo bottone star con rispettivo ascoltatore
  26. JButton bottonestart = new JButton("Start");
  27. ActionListener listenstart = new ActionListener(){
  28. public void actionPerformed(ActionEvent ae){
  29. //questo verrà eseguito alla pressione del bottone start!
  30. if(!bang){
  31. start();
  32. bang=true;
  33. }
  34. }
  35. };
  36. //aggiungo l'ascoltatore al bottone e il bottone al pannello p
  37. bottonestart.addActionListener(listenstart);
  38. p.add(bottonestart);
  39. //creo e aggiungo il bottone per lo stop
  40. JButton bottonestop = new JButton("Stop");
  41. ActionListener listenstop = new ActionListener(){
  42. public void actionPerformed(ActionEvent ae){
  43. //questo è quello che succede quando si preme Stop
  44. if(bang){
  45. automobile.x=0;
  46. stop();
  47. }
  48. bang=false;
  49. repaint();
  50. }
  51. };
  52. //creo bottone colore con rispettivo ascoltatore per il colore
  53. JButton bottonecolore = new JButton("Colore");
  54. ActionListener listencolore = new ActionListener(){
  55. public void actionPerformed(ActionEvent e) {
  56. //questo verrà eseguito alla pressione del bottone colore
  57. Color colore = new Color((int)(Math.random()*255),(int)(Math.random()*255),(int)(Math.random()*255));
  58. automobile.setColor(colore);
  59. System.out.println("Sto cambiando il colore della macchina :) il colore ora è "+colore);
  60. repaint();
  61. }
  62. };
  63. //aggiungo l'ascoltatore al bottone e il bottone al pannello p
  64. bottonecolore.addActionListener(listencolore);
  65. p.add(bottonecolore);
  66. //setto il Layout della finestra e aggiungo gli elementi
  67. f.setLayout(new BorderLayout());
  68. f.add(this,BorderLayout.CENTER);
  69. f.add(p,BorderLayout.SOUTH);
  70. f.pack();
  71. f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  72. f.setResizable(false);
  73. f.setVisible(true);
  74.  
  75. }
  76.  
  77. /////////////////////// METODO DI INIZIALIZZAZIONE DELL'OGGETTO CHE POI MUOVEREMO ///////////////////////
  78. public void init(){
  79. automobile = new Automobile(0, 250, lato, 0, 0, Color.red);
  80. didInit=true;
  81. }
  82.  
  83. /////////////////////// METODI DEL THREAD ///////////////////////
  84. public void stop(){
  85. System.out.println("Fermo il thread "+th);
  86. th = null;
  87.  
  88. }
  89.  
  90. public boolean isRunning(){
  91.  
  92. return th != null;
  93.  
  94. }
  95.  
  96. public void start(){
  97.  
  98. if(!isRunning()){
  99.  
  100. th = new Thread(this);
  101. System.out.println("Parte il Thread "+th);
  102. th.start();
  103.  
  104. }
  105.  
  106.  
  107.  
  108. }
  109.  
  110. /////////////////////// METODO RUN ///////////////////////
  111. public void run(){
  112.  
  113.  
  114. while(isRunning()){ // se il thread Ë partito
  115.  
  116. if(automobile.x == 380) bordo = true;
  117. else if (automobile.x == 0) bordo = false;
  118.  
  119. if(!bordo) automobile.x+=10;
  120. else if(bordo) automobile.x-=10;
  121. System.out.println("Mi muovo...le mie cordinate sono la y:"+automobile.y+" e la x :"+automobile.x);
  122.  
  123.  
  124. repaint();
  125.  
  126.  
  127. try{
  128.  
  129. Thread.sleep(1000);
  130.  
  131. }catch(InterruptedException e){
  132.  
  133. e.printStackTrace();
  134.  
  135. }
  136.  
  137. }
  138.  
  139. }
  140. /////////////////////// METODO PAINT ///////////////////////
  141. public void paint (Graphics g){
  142. super.paint(g);
  143. Graphics2D g2 = (Graphics2D) g;
  144. g2.setRenderingHint( RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON); // antialiasing
  145. if(!didInit){ // se ancora non ho inizializzato, inizializzo gli oggetti, ovvero le automobili
  146. System.out.println("Non ho inizializzato....sto inizializzando ora :) nel metodo paint richiamo Init");
  147. init();
  148. }
  149.  
  150. automobile.draw(g2);
  151.  
  152. }
  153.  
  154. /////////////////////// MAIN ///////////////////////
  155. public static void main(String[] args) {
  156. new CambiaColore();
  157.  
  158. }
  159.  
  160. }
  161.  
  162.  
  163.  
  164. ////////////////////////********* CLASSE AUTOMOBILE ********////////////////////////
  165. class Automobile extends Rectangle.Double {
  166. protected double y,x,vx,vy;
  167. protected Color colore;
  168.  
  169. public Automobile(double x, double y, double lato, double vx, double vy, Color colore){
  170. super(x,y,lato,lato);
  171. this.vx=vx;
  172. this.vy=vy;
  173. setColor(colore);
  174. }
  175.  
  176. public void draw(Graphics2D g2){
  177. System.out.println("Ridisegno la macchina");
  178. g2.setColor(colore);
  179. g2.fill(this);
  180. }
  181. public void setColor(Color colore){
  182. this.colore=colore;
  183. }
  184.  
  185. }
Add Comment
Please, Sign In to add comment