keymasterviriya1150

Untitled

Feb 4th, 2016
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.84 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.geom.*;
  3. import javax.swing.*;
  4. import javax.swing.*;
  5.  
  6.  class NewJFrame extends javax.swing.JFrame {
  7.  
  8.    
  9.     int z = 100;
  10.     public NewJFrame() {
  11.         initComponents();
  12.         body.setIcon(new Car(100));
  13.     }
  14.  
  15.  
  16.     @SuppressWarnings("unchecked")
  17.     // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  18.     private void initComponents() {
  19.  
  20.         jScrollPane1 = new javax.swing.JScrollPane();
  21.         body = new javax.swing.JLabel();
  22.         jButton1 = new javax.swing.JButton();
  23.         jButton2 = new javax.swing.JButton();
  24.  
  25.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  26.  
  27.         jScrollPane1.setViewportView(body);
  28.        
  29.  
  30.         jButton1.setText("IN");
  31.         jButton1.setForeground(Color.RED);
  32.         jButton1.addActionListener(new java.awt.event.ActionListener() {
  33.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  34.                 jButton1ActionPerformed(evt);
  35.             }
  36.         });
  37.  
  38.         jButton2.setText("OUT");
  39.         jButton2.setForeground(Color.BLUE);
  40.         jButton2.addActionListener(new java.awt.event.ActionListener() {
  41.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  42.                 jButton2ActionPerformed(evt);
  43.             }
  44.         });
  45.  
  46.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  47.         getContentPane().setLayout(layout);
  48.         layout.setHorizontalGroup(
  49.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  50.             .addGroup(layout.createSequentialGroup()
  51.                 .addContainerGap()
  52.                 .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE)
  53.                 .addGap(32, 32, 32)
  54.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  55.                     .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
  56.                     .addComponent(jButton1))
  57.                 .addContainerGap(88, Short.MAX_VALUE))
  58.         );
  59.         layout.setVerticalGroup(
  60.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  61.             .addGroup(layout.createSequentialGroup()
  62.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  63.                     .addGroup(layout.createSequentialGroup()
  64.                         .addContainerGap()
  65.                         .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE))
  66.                     .addGroup(layout.createSequentialGroup()
  67.                         .addGap(27, 27, 27)
  68.                         .addComponent(jButton1)
  69.                         .addGap(18, 18, 18)
  70.                         .addComponent(jButton2)))
  71.                 .addContainerGap(22, Short.MAX_VALUE))
  72.         );
  73.  
  74.         pack();
  75.     }// </editor-fold>//GEN-END:initComponents
  76.  
  77.     private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
  78.        z+=10;
  79.         body.setIcon(new Car(z));
  80.     }//GEN-LAST:event_jButton1ActionPerformed
  81.  
  82.     private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
  83.  z-=10;
  84.         body.setIcon(new Car(z));      
  85.     }//GEN-LAST:event_jButton2ActionPerformed
  86.  
  87.     /**
  88.      * @param args the command line arguments
  89.      */
  90.     public static void main(String args[]) {
  91.         /* Set the Nimbus look and feel */
  92.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  93.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  94.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  95.          */
  96.         try {
  97.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  98.                 if ("Nimbus".equals(info.getName())) {
  99.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  100.                     break;
  101.                 }
  102.             }
  103.         } catch (ClassNotFoundException ex) {
  104.             java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  105.         } catch (InstantiationException ex) {
  106.             java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  107.         } catch (IllegalAccessException ex) {
  108.             java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  109.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  110.             java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  111.         }
  112.         //</editor-fold>
  113.  
  114.         /* Create and display the form */
  115.         java.awt.EventQueue.invokeLater(new Runnable() {
  116.             public void run() {
  117.          
  118.                 new NewJFrame().setVisible(true);
  119.             }
  120.         });
  121.     }
  122.  
  123.     // Variables declaration - do not modify//GEN-BEGIN:variables
  124.     private javax.swing.JLabel body;
  125.     private javax.swing.JButton jButton1;
  126.     private javax.swing.JButton jButton2;
  127.     private javax.swing.JScrollPane jScrollPane1;
  128.     // End of variables declaration//GEN-END:variables
  129. }
  130.  
  131.  class Car implements Icon {
  132.     public Car(int aWidth){
  133.         width = aWidth;
  134.         }
  135.     public int getIconWidth(){
  136.         return width;
  137.         }
  138.     public int getIconHeight(){
  139.         return width/2;
  140.     }
  141.    
  142.     public void paintIcon(Component e, Graphics g, int x, int y){
  143.         Graphics2D g2 = (Graphics2D) g;
  144.         Rectangle2D.Double body = new Rectangle2D.Double(x,y+width/6,width-2,width/5);
  145.         Ellipse2D.Double frontTire = new Ellipse2D.Double(x+width/6,y+width/3,width/6,width/6);
  146.         Ellipse2D.Double rearTire = new Ellipse2D.Double(x+width*2/3,y+width/3,width/6,width/6);
  147.    
  148.         Point2D.Double r1 = new Point2D.Double(x+width/6,y+width/6);
  149.         Point2D.Double r2 = new Point2D.Double(x+width/3,y);
  150.         Point2D.Double r3 = new Point2D.Double(x+width*2/3,y);
  151.         Point2D.Double r4 = new Point2D.Double(x+width*5/6,y+width/6);
  152.        
  153.         Line2D.Double frontWindshield = new Line2D.Double(r1,r2);
  154.         Line2D.Double roofTop = new Line2D.Double(r2,r3);
  155.         Line2D.Double rearWindshield = new Line2D.Double(r3,r4);
  156.        
  157.         g2.fill(frontTire);
  158.         g2.fill(rearTire);
  159.         g2.setColor(Color.black);
  160.         g2.fill(body);
  161.         g2.draw(frontWindshield);
  162.         g2.draw(roofTop);
  163.         g2.draw(rearWindshield);
  164.        
  165.        
  166.     }
  167.     private int width;
  168.  
  169. }
Advertisement
Add Comment
Please, Sign In to add comment