Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.geom.*;
- import javax.swing.*;
- import javax.swing.*;
- class NewJFrame extends javax.swing.JFrame {
- int z = 100;
- public NewJFrame() {
- initComponents();
- body.setIcon(new Car(100));
- }
- @SuppressWarnings("unchecked")
- // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
- private void initComponents() {
- jScrollPane1 = new javax.swing.JScrollPane();
- body = new javax.swing.JLabel();
- jButton1 = new javax.swing.JButton();
- jButton2 = new javax.swing.JButton();
- setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
- jScrollPane1.setViewportView(body);
- jButton1.setText("IN");
- jButton1.setForeground(Color.RED);
- jButton1.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton1ActionPerformed(evt);
- }
- });
- jButton2.setText("OUT");
- jButton2.setForeground(Color.BLUE);
- jButton2.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton2ActionPerformed(evt);
- }
- });
- javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
- getContentPane().setLayout(layout);
- layout.setHorizontalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addContainerGap()
- .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGap(32, 32, 32)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(jButton1))
- .addContainerGap(88, Short.MAX_VALUE))
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addContainerGap()
- .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGroup(layout.createSequentialGroup()
- .addGap(27, 27, 27)
- .addComponent(jButton1)
- .addGap(18, 18, 18)
- .addComponent(jButton2)))
- .addContainerGap(22, Short.MAX_VALUE))
- );
- pack();
- }// </editor-fold>//GEN-END:initComponents
- private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
- z+=10;
- body.setIcon(new Car(z));
- }//GEN-LAST:event_jButton1ActionPerformed
- private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
- z-=10;
- body.setIcon(new Car(z));
- }//GEN-LAST:event_jButton2ActionPerformed
- /**
- * @param args the command line arguments
- */
- public static void main(String args[]) {
- /* Set the Nimbus look and feel */
- //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
- /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
- * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
- */
- try {
- for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
- if ("Nimbus".equals(info.getName())) {
- javax.swing.UIManager.setLookAndFeel(info.getClassName());
- break;
- }
- }
- } catch (ClassNotFoundException ex) {
- java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (InstantiationException ex) {
- java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (IllegalAccessException ex) {
- java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (javax.swing.UnsupportedLookAndFeelException ex) {
- java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- }
- //</editor-fold>
- /* Create and display the form */
- java.awt.EventQueue.invokeLater(new Runnable() {
- public void run() {
- new NewJFrame().setVisible(true);
- }
- });
- }
- // Variables declaration - do not modify//GEN-BEGIN:variables
- private javax.swing.JLabel body;
- private javax.swing.JButton jButton1;
- private javax.swing.JButton jButton2;
- private javax.swing.JScrollPane jScrollPane1;
- // End of variables declaration//GEN-END:variables
- }
- class Car implements Icon {
- public Car(int aWidth){
- width = aWidth;
- }
- public int getIconWidth(){
- return width;
- }
- public int getIconHeight(){
- return width/2;
- }
- public void paintIcon(Component e, Graphics g, int x, int y){
- Graphics2D g2 = (Graphics2D) g;
- Rectangle2D.Double body = new Rectangle2D.Double(x,y+width/6,width-2,width/5);
- Ellipse2D.Double frontTire = new Ellipse2D.Double(x+width/6,y+width/3,width/6,width/6);
- Ellipse2D.Double rearTire = new Ellipse2D.Double(x+width*2/3,y+width/3,width/6,width/6);
- Point2D.Double r1 = new Point2D.Double(x+width/6,y+width/6);
- Point2D.Double r2 = new Point2D.Double(x+width/3,y);
- Point2D.Double r3 = new Point2D.Double(x+width*2/3,y);
- Point2D.Double r4 = new Point2D.Double(x+width*5/6,y+width/6);
- Line2D.Double frontWindshield = new Line2D.Double(r1,r2);
- Line2D.Double roofTop = new Line2D.Double(r2,r3);
- Line2D.Double rearWindshield = new Line2D.Double(r3,r4);
- g2.fill(frontTire);
- g2.fill(rearTire);
- g2.setColor(Color.black);
- g2.fill(body);
- g2.draw(frontWindshield);
- g2.draw(roofTop);
- g2.draw(rearWindshield);
- }
- private int width;
- }
Advertisement
Add Comment
Please, Sign In to add comment