5, ఏప్రిల్ 2012, గురువారం

YOU ARE NOT EXPECTED TO UNDERSTAND THIS CODE YET IT CONTAINS A LOT OF JAVA CONCEPTS THAT WILL NOT BE GONE OVER IN. ONE MUST PRACTICE IT TO ENJOY THE JAVA LANGUAGE AT THE TIME OF LEARNING IT HAS INVOLVED MAXIMUM JAVA CONCEPTS the below code I have taken for my class room explanation


import java.awt.*;            
public class Car {
    private Color color = Color.BLUE;
    private int xpos = 0;
    private int ypos = 0;
    private int fuel = 2000;
    public Car() {
    }

    /*public Car (Color c, int x, int y, int f) {
                                color = c;
                                xpos = x;
                                ypos = y;
                                fuel = f;
    }*/

    public Car (Color c, int x, int y, int f) {
                                setColor (c);
                                setXPos (x);
                                setYPos (y);
                                setFuel (f);
    }

    public void setXPos (int x) {
                                fuel -= Math.abs(getXPos()-x);
                                xpos = x;
    }

    public void setYPos (int y) {
                                fuel -= Math.abs(getYPos()-y);
                                ypos = y;
    }

    public void setPos (int x, int y) {
                                setXPos(x);
                                setYPos(y);
    }

    public void setColor (Color c) {
                                color = c;
    }

    public void setFuel (int f) {
                                fuel = f;
    }

    public int getFuel() {
                                return fuel;
    }

    public int getXPos() {
                                return xpos;
    }

    public int getYPos() {
                                return ypos;
    }

    public Color getColor() {
                                return color;
    }

    private final int CAR_WIDTH = 100;
    private final int CAR_HEIGHT = 200;
    private final int TIRE_WIDTH = 20;
    private final int TIRE_HEIGHT = 40;
    private final int TIRE_OFFSET = 20;

    public void paint (Graphics g) {
                                if ( fuel < 0 ) {
                                    color = Color.RED;
                                }
                                g.setColor (color);
                                g.fillRect (getXPos()-CAR_WIDTH/2, getYPos()-CAR_HEIGHT/2,
                                                    CAR_WIDTH, CAR_HEIGHT);
               
                                // Draw the tires
                                g.setColor (Color.BLACK);
                                g.fillRect (getXPos()-(CAR_WIDTH/2+TIRE_WIDTH),
                                                    getYPos()-(CAR_HEIGHT/2-TIRE_OFFSET), TIRE_WIDTH, TIRE_HEIGHT);
                                g.fillRect (getXPos()-(CAR_WIDTH/2+TIRE_WIDTH),
                                                    getYPos()+(CAR_HEIGHT/2-TIRE_OFFSET-TIRE_HEIGHT),
                                                    TIRE_WIDTH, TIRE_HEIGHT);
                                g.fillRect (getXPos()+(CAR_WIDTH/2),
                                                    getYPos()+(CAR_HEIGHT/2-TIRE_OFFSET-TIRE_HEIGHT),
                                                    TIRE_WIDTH, TIRE_HEIGHT);
                                g.fillRect (getXPos()+(CAR_WIDTH/2),
                                                    getYPos()-(CAR_HEIGHT/2-TIRE_OFFSET), TIRE_WIDTH, TIRE_HEIGHT);
    }

}

import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class CarGUI {

    private static JFrame window;
    private static JFrame window2;
    private static JPanel panel;

    private static final int WINDOW_DIM = 700;

    static Car c;
    static Graphics g;

    public static void main (String[] args) {
                JButton button;

                window = new JFrame ("Car GUI");
                window.setSize (WINDOW_DIM, WINDOW_DIM);
                window.setVisible(true);
                window.setLayout (new BoxLayout(window.getContentPane(), BoxLayout.Y_AXIS));
                window2 = new JFrame ("Car GUI");
                window2.setSize (WINDOW_DIM, WINDOW_DIM);
                window2.setVisible(true);

                /*panel = new JPanel();
                panel.setPreferredSize (new Dimension(300,300));
                panel.setBorder(BorderFactory.createTitledBorder("Car simulation"));
                window.add(panel);*/

                g = window2.getGraphics();
                //System.out.println (g);

                button = new JButton ("Exit");
        button.addActionListener (new ActionListener() {
                public void actionPerformed (ActionEvent e) {
                    System.exit(0);
                }
            });
                window.add(button);

                button = new JButton ("Show");
        button.addActionListener (new ActionListener() {
                public void actionPerformed (ActionEvent e) {
                                    c.paint(g);
                }
            });
                window.add(button);

                button = new JButton ("x++");
        button.addActionListener (new ActionListener() {
                public void actionPerformed (ActionEvent e) {
                                    g.clearRect(0,0,WINDOW_DIM,WINDOW_DIM);
                                    c.setXPos(c.getXPos()+1);
                                    c.paint(g);
                }
            });
                window.add(button);

                button = new JButton ("x--");
        button.addActionListener (new ActionListener() {
                public void actionPerformed (ActionEvent e) {
                                    g.clearRect(0,0,WINDOW_DIM,WINDOW_DIM);
                                    c.setXPos(c.getXPos()-1);
                                    c.paint(g);
                }
            });
                window.add(button);

                button = new JButton ("y++");
        button.addActionListener (new ActionListener() {
                public void actionPerformed (ActionEvent e) {
                                    g.clearRect(0,0,WINDOW_DIM,WINDOW_DIM);
                                    c.setYPos(c.getYPos()+1);
                                    c.paint(g);
                }
            });
                window.add(button);

                button = new JButton ("y--");
        button.addActionListener (new ActionListener() {
                public void actionPerformed (ActionEvent e) {
                                    g.clearRect(0,0,WINDOW_DIM,WINDOW_DIM);
                                    c.setYPos(c.getYPos()-1);
                                    c.paint(g);
                }
            });
                window.add(button);

                button = new JButton ("Run");
        button.addActionListener (new ActionListener() {
                public void actionPerformed (ActionEvent e) {
                                    Random random = new Random();
                                    int dir = 0;
                                    int dist = 0;
                                    for ( int i = 0; i < 500; i++ ) {
                                                try {
                                                    Thread.sleep(10);
                                                } catch (Exception ex) {
                                                }
                                                if ( dist == 0 ) {
                                                    dir = random.nextInt(4);
                                                    dist = random.nextInt(10)+10;
                                                }
                                                int x = c.getXPos();
                                                int y = c.getYPos();
                                                switch (dir) {
                                                case 0:
                                                    x++;
                                                    break;
                                                case 1:
                                                    x--;
                                                    break;
                                                case 2:
                                                    y++;
                                                    break;
                                                case 3:
                                                    y--;
                                                    break;
                                                }
                                                dist--;
                                                g.clearRect(0,0,WINDOW_DIM,WINDOW_DIM);
                                                c.setPos(x,y);
                                                c.paint(g);
                                    }
                }
            });
                window.add(button);

                c = new Car ();
                c.setPos (WINDOW_DIM/2,WINDOW_DIM/2);
                c.paint(g);

                window.pack();

    }

}

కామెంట్‌లు లేవు:

కామెంట్‌ను పోస్ట్ చేయండి