/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MainMenu.java
 *
 * Created on Feb 22, 2012, 7:28:37 PM
 */
package f1carracing;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

/**
 *
 * @author User
 */
public class MainMenu extends javax.swing.JApplet {
   Icon icon1 = new ImageIcon("classes/image/car1.png");
   private Image imageCar1;
   private Image imageCar2;
   private Image imageCar3;
   private Image imageCar4;
   private Image imageCar5;   
   private int xcoo1 = 740, xcoo2 = 640, xcoo3 = 540, xcoo4 = 440, xcoo5 = 340;
   private int ycoo1 = 110,  ycoo2 = 160, ycoo3 = 210, ycoo4 = 260, ycoo5 = 310;
   private int number1, number2, number3, number4, number5;
   private int baseX = 0;
   private int car1, car2, car3, car4, car5;
   private int sleepTime1, sleepTime2, sleepTime3, sleepTime4, sleepTime5;
   private int base1 = 0, base2 = 0, base3 = 0, base4 = 0, base5 = 0;
   Thread t1 = new Thread(new Thread1());

    /** Initializes the applet MainMenu */
    @Override
    public void init() {
        /* 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(MainMenu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MainMenu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MainMenu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MainMenu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        
       try{ 
        initComponents();
        JOptionPane.showMessageDialog(null, "Please enter 1 - 5 for the speed.(1 = slowest)");
        imageCar1 = getImage(getDocumentBase(), "car1.png");
        imageCar2 = getImage(getDocumentBase(), "car2.png");
        imageCar3 = getImage(getDocumentBase(), "car3.png");
        imageCar4 = getImage(getDocumentBase(), "car4.png");
        imageCar5 = getImage(getDocumentBase(), "car5.png");
       }catch(Exception ex){
           JOptionPane.showMessageDialog(null, ex);
       }
    }
    
    public void start(){
        resize(1200,900);
    }
    
    public void stop(){
        super.stop();
    }
    
    public void paint(Graphics g){
        super.paint(g);
        
        g.setColor(Color.DARK_GRAY);
        
        g.drawRect(50,  100, 1000, 600);
        g.drawRect(100, 150, 900, 500);
        g.drawRect(150, 200, 800, 400);
        g.drawRect(200, 250, 700, 300);
        g.drawRect(250, 300, 600, 200);
        g.drawRect(300, 350, 500, 100);
        
        g.drawLine(350, 350, 850, 100);
        
        g.setColor(Color.red);
        g.drawLine(350, 350, 350, 100);
        
        g.drawImage(imageCar1, xcoo1, ycoo1, this);
        g.drawImage(imageCar2, xcoo2, ycoo2, this);
        g.drawImage(imageCar3, xcoo3, ycoo3, this);
        g.drawImage(imageCar4, xcoo4, ycoo4, this);
        g.drawImage(imageCar5, xcoo5, ycoo5, this);
       // baseX = baseX + (car1);
        base1 += car1;
    }
    
    public void update(Graphics g){
        
    }
    
    class Thread1 implements Runnable{
        
        public void run(){     
            while(true){
    
                if(xcoo1 >= 350 && xcoo1 < 1000 && ycoo1 < 650){ // First "duan " road(moving right)
                    xcoo1 += 1;
                }else if(xcoo1 >= 1000 && ycoo1 >= 50 && ycoo1 < 650){ // second "duan" road(moving down)
                    ycoo1 +=1 ;
                    
                }else if(ycoo1 >= 650 && xcoo1 > 50){// third "duan" road (moving left)
                    xcoo1 -=1 ;
                    
                }else if(ycoo1 > 100 && xcoo1 == 50 ){
                    
                    ycoo1 -=1 ;// last "duan" road (moving up)
                }else if(ycoo1 == 100 && xcoo1 <300){
                    
                    xcoo1 +=1 ; // going back to starting point
                }else{
                    
                }
 /*                
                if(xcoo1==350 && ycoo1==850){

                }else{
                        xcoo1 =  xcoo1+ (base1);

                    if (xcoo1 >950) {
                        ycoo1 = ycoo1 + 1;
                        base1 = 0;
                    if(ycoo1>500)
                        base1 = -1;
                    }
                    
                    if(xcoo1<650){
                        ycoo1 = ycoo1-1;
                        base1=0;
                    if(ycoo1<=700)
                        base1=+1;
                    }
                }
*/
            try {            
                t1.sleep(sleepTime1);
            }catch (Exception ex) {
            }
            repaint();
            }
        }
    }   
    /** This method is called from within the init() method to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jComboBox2 = new javax.swing.JComboBox();
        jComboBox3 = new javax.swing.JComboBox();
        jComboBox4 = new javax.swing.JComboBox();
        jComboBox6 = new javax.swing.JComboBox();
        jComboBox7 = new javax.swing.JComboBox();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabel9 = new javax.swing.JLabel();
        jComboBox5 = new javax.swing.JComboBox();
        jComboBox8 = new javax.swing.JComboBox();
        jComboBox9 = new javax.swing.JComboBox();
        jComboBox10 = new javax.swing.JComboBox();
        jComboBox11 = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();

        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        getContentPane().setLayout(null);

        jLabel2.setFont(new java.awt.Font("OCR A Extended", 1, 18)); // NOI18N
        jLabel2.setForeground(new java.awt.Color(255, 0, 0));
        jLabel2.setText("Player 5");
        getContentPane().add(jLabel2);
        jLabel2.setBounds(790, 0, 130, 50);

        jLabel3.setFont(new java.awt.Font("OCR A Extended", 1, 18)); // NOI18N
        jLabel3.setForeground(new java.awt.Color(255, 0, 0));
        jLabel3.setText("Player 1");
        getContentPane().add(jLabel3);
        jLabel3.setBounds(80, 0, 130, 50);

        jLabel4.setFont(new java.awt.Font("OCR A Extended", 1, 18)); // NOI18N
        jLabel4.setForeground(new java.awt.Color(255, 0, 0));
        jLabel4.setText("Player 2");
        getContentPane().add(jLabel4);
        jLabel4.setBounds(250, 0, 130, 50);

        jLabel5.setFont(new java.awt.Font("OCR A Extended", 1, 18)); // NOI18N
        jLabel5.setForeground(new java.awt.Color(255, 0, 0));
        jLabel5.setText("Player 3");
        getContentPane().add(jLabel5);
        jLabel5.setBounds(440, 0, 130, 50);

        jLabel6.setFont(new java.awt.Font("OCR A Extended", 1, 18)); // NOI18N
        jLabel6.setForeground(new java.awt.Color(255, 0, 0));
        jLabel6.setText("Player 4");
        getContentPane().add(jLabel6);
        jLabel6.setBounds(620, 0, 130, 50);

        jLabel8.setFont(new java.awt.Font("OCR A Extended", 1, 14)); // NOI18N
        jLabel8.setText("Speed: ");
        getContentPane().add(jLabel8);
        jLabel8.setBounds(10, 60, 70, 40);

        jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Car 1", "Car 2", "Car 3", "Car 4", "Car 5" }));
        jComboBox2.setToolTipText("Click to select a car.");
        jComboBox2.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        jComboBox2.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jComboBox2ItemStateChanged(evt);
            }
        });
        jComboBox2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox2ActionPerformed(evt);
            }
        });
        getContentPane().add(jComboBox2);
        jComboBox2.setBounds(70, 40, 120, 20);

        jComboBox3.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Car 1", "Car 2", "Car 3", "Car 4", "Car 5" }));
        jComboBox3.setSelectedIndex(1);
        jComboBox3.setToolTipText("Click to select a car.");
        jComboBox3.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jComboBox3ItemStateChanged(evt);
            }
        });
        jComboBox3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox3ActionPerformed(evt);
            }
        });
        getContentPane().add(jComboBox3);
        jComboBox3.setBounds(240, 40, 120, 20);

        jComboBox4.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Car 1", "Car 2", "Car 3", "Car 4", "Car 5" }));
        jComboBox4.setSelectedIndex(4);
        jComboBox4.setToolTipText("Click to select a car.");
        jComboBox4.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jComboBox4ItemStateChanged(evt);
            }
        });
        jComboBox4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox4ActionPerformed(evt);
            }
        });
        getContentPane().add(jComboBox4);
        jComboBox4.setBounds(780, 40, 120, 20);

        jComboBox6.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Car 1", "Car 2", "Car 3", "Car 4", "Car 5" }));
        jComboBox6.setSelectedIndex(3);
        jComboBox6.setToolTipText("Click to select a car.");
        jComboBox6.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jComboBox6ItemStateChanged(evt);
            }
        });
        jComboBox6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox6ActionPerformed(evt);
            }
        });
        getContentPane().add(jComboBox6);
        jComboBox6.setBounds(610, 40, 120, 20);

        jComboBox7.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Car 1", "Car 2", "Car 3", "Car 4", "Car 5" }));
        jComboBox7.setSelectedIndex(2);
        jComboBox7.setToolTipText("Click to select a car.");
        jComboBox7.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jComboBox7ItemStateChanged(evt);
            }
        });
        jComboBox7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox7ActionPerformed(evt);
            }
        });
        getContentPane().add(jComboBox7);
        jComboBox7.setBounds(430, 40, 120, 20);

        jButton1.setText("Start");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton1);
        jButton1.setBounds(960, 60, 90, 30);

        jButton2.setText("Exit");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton2);
        jButton2.setBounds(1000, 0, 50, 30);

        jLabel9.setFont(new java.awt.Font("OCR A Extended", 1, 14)); // NOI18N
        jLabel9.setText("Car: ");
        getContentPane().add(jLabel9);
        jLabel9.setBounds(10, 40, 70, 40);

        jComboBox5.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4", "5" }));
        jComboBox5.setToolTipText("Click to select a car.");
        jComboBox5.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        jComboBox5.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jComboBox5ItemStateChanged(evt);
            }
        });
        jComboBox5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox5ActionPerformed(evt);
            }
        });
        getContentPane().add(jComboBox5);
        jComboBox5.setBounds(70, 70, 120, 20);

        jComboBox8.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4", "5" }));
        jComboBox8.setToolTipText("Click to select a car.");
        jComboBox8.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jComboBox8ItemStateChanged(evt);
            }
        });
        jComboBox8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox8ActionPerformed(evt);
            }
        });
        getContentPane().add(jComboBox8);
        jComboBox8.setBounds(240, 70, 120, 20);

        jComboBox9.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4", "5" }));
        jComboBox9.setToolTipText("Click to select a car.");
        jComboBox9.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jComboBox9ItemStateChanged(evt);
            }
        });
        jComboBox9.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox9ActionPerformed(evt);
            }
        });
        getContentPane().add(jComboBox9);
        jComboBox9.setBounds(430, 70, 120, 20);

        jComboBox10.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4", "5" }));
        jComboBox10.setToolTipText("Click to select a car.");
        jComboBox10.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jComboBox10ItemStateChanged(evt);
            }
        });
        jComboBox10.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox10ActionPerformed(evt);
            }
        });
        getContentPane().add(jComboBox10);
        jComboBox10.setBounds(610, 70, 120, 20);

        jComboBox11.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4", "5" }));
        jComboBox11.setSelectedIndex(4);
        jComboBox11.setToolTipText("Click to select a car.");
        jComboBox11.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jComboBox11ItemStateChanged(evt);
            }
        });
        jComboBox11.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox11ActionPerformed(evt);
            }
        });
        getContentPane().add(jComboBox11);
        jComboBox11.setBounds(780, 70, 120, 20);

        jLabel1.setIcon(new javax.swing.ImageIcon("C:\\Users\\User\\Documents\\NetBeansProjects\\F1CarRacing\\src\\f1carracing\\F1_logo.png")); // NOI18N
        getContentPane().add(jLabel1);
        jLabel1.setBounds(80, 0, 1060, 810);
    }// </editor-fold>                        

    private void jComboBox2ActionPerformed(java.awt.event.ActionEvent evt) {                                           
        
    }                                          

    private void jComboBox2ItemStateChanged(java.awt.event.ItemEvent evt) {                                            
        int a = jComboBox2.getSelectedIndex();
        imageCar1 = getImage(getDocumentBase(), "car"+ (a+1) + ".png");
        repaint();
    }                                           

    private void jComboBox3ActionPerformed(java.awt.event.ActionEvent evt) {                                           
        // TODO add your handling code here:
    }                                          

    private void jComboBox3ItemStateChanged(java.awt.event.ItemEvent evt) {                                            
        int b = jComboBox3.getSelectedIndex();
        imageCar2 = getImage(getDocumentBase(), "car" + (b+1) + ".png");
        repaint();
        
    }                                           

    private void jComboBox4ItemStateChanged(java.awt.event.ItemEvent evt) {                                            
        int c = jComboBox4.getSelectedIndex();
        imageCar5 = getImage(getDocumentBase(), "car" + (c + 1) + ".png");
        repaint();
    }                                           

    private void jComboBox4ActionPerformed(java.awt.event.ActionEvent evt) {                                           
        // TODO add your handling code here:
    }                                          

    private void jComboBox6ItemStateChanged(java.awt.event.ItemEvent evt) {                                            
        int d = jComboBox6.getSelectedIndex();
        imageCar4 = getImage(getDocumentBase(), "car" + (d + 1) + ".png");
        repaint();
    }                                           

    private void jComboBox6ActionPerformed(java.awt.event.ActionEvent evt) {                                           
        // TODO add your handling code here:
    }                                          

    private void jComboBox7ItemStateChanged(java.awt.event.ItemEvent evt) {                                            
        int e = jComboBox7.getSelectedIndex();
        imageCar3 = getImage(getDocumentBase(), "car" + (e + 1) + ".png");
        repaint();
    }                                           

    private void jComboBox7ActionPerformed(java.awt.event.ActionEvent evt) {                                           
        // TODO add your handling code here:
    }                                          

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        
        try{

        car1 = number1;
        car2 = number2;
        car3 = number3;
        car4 = number4;
        car5 = number5;
        }
        catch(Exception ex){
            car1 = 0;
            car2 = 0;
            car3 = 0;
            car4 = 0;
            car5 = 0;
         }
        
        if(car1 == 1){
            sleepTime1 = 150;
            //System.out.print(sleepTime1);
        }else if(car2 == 2){
            sleepTime1 = 110;
        }else if(car3 == 3){
            sleepTime1 = 80;
        }else if(car4 == 4){
            sleepTime1 = 50;
        }else{
            sleepTime1 = 20;
        }
        t1.start();
    }                                        

    private void jComboBox5ItemStateChanged(java.awt.event.ItemEvent evt) {                                            
        number1 = (jComboBox5.getSelectedIndex() + 1);
       // System.out.println(number1);
        repaint();
    }                                           

    private void jComboBox5ActionPerformed(java.awt.event.ActionEvent evt) {                                           
        
    }                                          

    private void jComboBox8ItemStateChanged(java.awt.event.ItemEvent evt) {                                            
        number2 = (jComboBox8.getSelectedIndex() + 1);
        repaint();
    }                                           

    private void jComboBox8ActionPerformed(java.awt.event.ActionEvent evt) {                                           
        // TODO add your handling code here:
    }                                          

    private void jComboBox9ItemStateChanged(java.awt.event.ItemEvent evt) {                                            
        number3 = (jComboBox9.getSelectedIndex() + 1);
        repaint();
    }                                           

    private void jComboBox9ActionPerformed(java.awt.event.ActionEvent evt) {                                           
        // TODO add your handling code here:
    }                                          

    private void jComboBox10ItemStateChanged(java.awt.event.ItemEvent evt) {                                             
        number4 = (jComboBox10.getSelectedIndex() + 1);
        repaint();
    }                                            

    private void jComboBox10ActionPerformed(java.awt.event.ActionEvent evt) {                                            
        // TODO add your handling code here:
    }                                           

    private void jComboBox11ItemStateChanged(java.awt.event.ItemEvent evt) {                                             
        number5 = (jComboBox11.getSelectedIndex() + 1);
        repaint();
    }                                            

    private void jComboBox11ActionPerformed(java.awt.event.ActionEvent evt) {                                            
        // TODO add your handling code here:
    }                                           

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        JOptionPane.showMessageDialog(null, "Thanks For Playing Car Racing.");
        System.exit(0);
    }                                        

    
    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JComboBox jComboBox10;
    private javax.swing.JComboBox jComboBox11;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JComboBox jComboBox3;
    private javax.swing.JComboBox jComboBox4;
    private javax.swing.JComboBox jComboBox5;
    private javax.swing.JComboBox jComboBox6;
    private javax.swing.JComboBox jComboBox7;
    private javax.swing.JComboBox jComboBox8;
    private javax.swing.JComboBox jComboBox9;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    // End of variables declaration                   
}

Here is my code...and i wanna ask, how to make the image rotate when it support to rotate? Thanks!!!

Recommended Answers

All 2 Replies

Graphics2D object should have transformation properties checkout the api

It will be much easier for anyone to help you if you write a small simple program that compiles and executes with the code that you want to have rotate an image. Most of the code you posted has nothing to do with rotating an image.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.