Object Orient Programming

Object Orient Programming

I have this program that needs to display multiple taxis. I have the code but there is an error. Could someone tell me where i am going wrong??

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

public class TaxiCanvas extends JComponent
{
    private Taxi[] taxis;

    public TaxiCanvas(int numTaxis)
    {
    super();
    makeTaxis(numTaxis);
    }

    private void makeTaxis(int numTaxis)
    {
    taxis = new Taxi[numTaxis];
    for (int i=0; i<taxis.length; i++)
    {
        taxis[i] = new Taxi(i+1);
    }
    }

    @Override
    public void paint(Graphics g)
    {
        for (int i=0; i<taxis.length;i++)
        {
        int w = taxis[i].getBodyWidth();
        int h = taxis[i].getBodyHeight();
        int x = randomInt(w,getWidth() - w );
        int y = randomInt(h, getHeight() - h);
        taxis[i].display(g, x, y);
        }
        g.drawString(taxis.length + " taxis", getWidth()/2-30,20);
    }
       private int randomInt(int min, int max)
    {
    return (int)(Math.random()*(max - min +1)+min);
    }
}


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

public class TaxiFrame extends JFrame
{
    public TaxiFrame()
    {
    super(" Taxi Project");
    setSize(300,200);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setLocation(100,100);
    getContentPane().setBackground(Color.CYAN);
    getContentPane().add("Center", new TaxiCanvas(5));
    setVisible(true);
    }
    public static void main(String[] args)
    {
    new TaxiFrame();
    }

}

import java.awt.*;

public class Taxi
{
    private TaxiCanvas canvas;
    public int number;

    private int BodyWidth;
    private int BodyHeight;

    public Taxi(TaxiCanvas canvas)
    {
    this.canvas = canvas;
    }

    public Taxi(int num)
    {
    number = num;
    BodyWidth = 44;
    BodyHeight = 20;
    }
     public int getBodyWidth()
     {
     return BodyWidth;
     }
    public int getBodyHeight()
    {
    return BodyHeight;
    }
    private int randomInt(int min, int max)
    {
    return (int)(Math.random()*(max - min +1)+min);
    }

      public void display(Graphics g , int x, int y)
      {
      int xSq = canvas.getWidth() / 120;
      int ySq = canvas.getHeight() / 100;

// int xSq = 2;
// int ySq = 2;
// car body
      g.setColor(Color.YELLOW);
      int[] xbody ={x, x, x + (xSq*12),x +(xSq*18),x + (xSq*29),x +(xSq*37),x + (xSq*42),x + (xSq*44),x + (xSq*44)};
      int[] ybody ={y-(ySq*6),y-(ySq*11),y-(ySq*14),y-(ySq*20),y-(ySq*20),y-(ySq*14),y-(ySq*14),y-(ySq*11),y-(ySq*6)};
      g.fillPolygon(xbody, ybody, 9);
      g.setColor(Color.BLACK);
      g.drawLine(x,y-(ySq*6),x + (xSq*44),y-(ySq*6));

      //left wheel
      g.setColor(Color.BLACK);
      g.fillOval(x +(xSq*3),y-(ySq*10),xSq*8,ySq*8);

      //right wheel
      g.setColor(Color.BLACK);
      g.fillOval(x+(xSq*32), y-(ySq*10),xSq*8,ySq*8);

      //left door
      g.setColor(Color.LIGHT_GRAY);
      g.drawRect(x+(xSq*13),y-(ySq*14),xSq*9,ySq*7);
      g.setColor(Color.BLACK);
      g.drawString(""+number,x+(xSq*16),y-(ySq*8));

      g.setColor(Color.LIGHT_GRAY);
      int[]xleftWin={x+(xSq*13),x+(xSq*18),x+(xSq*22),x+(xSq*22)};
      int[]yleftWin={y-(ySq*14),y-(ySq*19),y-(ySq*19),y-(ySq*14)};
      g.fillPolygon(xleftWin, yleftWin, 4);

      //right door
      int[]xleftDoor={x+(xSq*24),x+(xSq*24),x+(xSq*35),x+(xSq*35),x+(xSq*30)};
      int[]yleftDoor={y-(ySq*7),y-(ySq*14),y-(ySq*14),y-(ySq*11),y-(ySq*7)};
      g.drawPolygon(xleftDoor, yleftDoor, 5);


      int[]xrightWin={x+(xSq*24),x+(xSq*24),x+(xSq*28),x+(xSq*35)};
      int[]yrightWin={y-(ySq*14),y-(ySq*19),y-(ySq*19),y-(ySq*14)};
      g.fillPolygon(xrightWin, yrightWin, 4);
    }

}
View Answers

Ads









Related Tutorials/Questions & Answers:
Object Orient Programming
Object Orient Programming   I have this program that needs to display multiple taxis. I have the code but there is an error. Could someone tell me where i am going wrong?? import java.awt.*; import javax.swing.*; public
object oriented programming protocol
object oriented programming protocol  What is object oriented programming protocol? Is it different from objective c protocol programming?? Thanks
Advertisements
Object Oriented Programming in PHP
Object Oriented Programming in PHP  Hi, Which version of PHP supports OOPS. How can I learn Object Oriented Programming in PHP? Thanks
object oriented programming data science
object oriented programming data science  Hi, I am beginner in Data...: object oriented programming data science Try to provide me good examples or tutorials links so that I can learn the topic "object oriented programming
Object Oriented Programming in Java
OOPS acronym for Object Oriented Programming is a model or concept that works around objects and data. Programming language like Java that follow the OOP... of a class. For a language to be an Object Oriented Programming Language, it must
ModuleNotFoundError: No module named 'orient'
ModuleNotFoundError: No module named 'orient'  Hi, My Python... 'orient' How to remove the ModuleNotFoundError: No module named 'orient'... to install padas library. You can install orient python with following command
Java using Netbeans Object Oriented Programming
Java using Netbeans Object Oriented Programming   I am trying to make a program that uses a main class and a separate class with one public method and one private method. The main method in the main class asks the user for 2
An Overview of Java      Java is a programming language      Java is Object Oriented Programming
; Java as a programming language Java is an Object oriented application programming language developed by Sun Microsystems. Java is a very powerful general-purpose programming language.  
Java Object
Java Object         Object is the basic entity of object oriented programming language. Object... properties of the class or its group. Java object is an instance of the class. It takes
ModuleNotFoundError: No module named 'fast-orient'
ModuleNotFoundError: No module named 'fast-orient'  Hi, My Python... 'fast-orient' How to remove the ModuleNotFoundError: No module named 'fast-orient' error? Thanks   Hi, In your python environment
Introduction to POJO (Plain Old Java Object) Programming Model
Introduction to POJO (Plain Old Java Object) Programming Model... Object). It is a Java object that doesn't extend or implement some specialized... the message driven POJO on the client side, client looks up a stub object
programming
Java Constructor programming for single and double constructor  ... constructor,double parameter constor,and the now when we create a object of in this constructor he we tell the how much object we created in the no argument.single
Exp.4 Write any C++ programs to demonstrate multiple inheritance concept of an object oriented programming.
Exp.4 Write any C++ programs to demonstrate multiple inheritance concept of an object oriented programming.  (Aim:- The main aim of this experiment is reusability of code and getting idea about classifying objects , identifying
object
object  is it possible to create object in the same class..?.   Yes, you can. class CreateObject { CreateObject(){ System.out.println("I have an object."); } public static void main(String[] args
programming
create a object of constructor Java  write a program which have... constor,and the now when we create a object of in this constructor he we tell the how much object we created in the no argument.single or double parameter
programming
constructor object in java application programming  write a program...,double parameter constor,and the now when we create a object of in this constructor he we tell the how much object we created in the no argument.single or double
programming
Construct a constructor of object in Java programming  write...,double parameter constor,and the now when we create a object of in this constructor he we tell the how much object we created in the no argument.single or double
Programming
Programming  Given a number n, write a programming to determine its square root if it is possible, in the contraly case print an appropriate massege on the screen
programming
parameter constor,and the now when we create a object of in this constructor he we tell the how much object we created in the no argument.single or double parameter
programming
constor,and the now when we create a object of in this constructor he we tell the how much object we created in the no argument.single or double parameter
programming
,and the now when we create a object of in this constructor he we tell the how much object we created in the no argument.single or double parameter constructor
programming
,and the now when we create a object of in this constructor he we tell the how much object we created in the no argument.single or double parameter constructor
programming
constor,and the now when we create a object of in this constructor he we tell the how much object we created in the no argument.single or double parameter
programming
constor,and the now when we create a object of in this constructor he we tell the how much object we created in the no argument.single or double parameter constructor
programming
parameter constor,and the now when we create a object of in this constructor he we tell the how much object we created in the no argument.single or double
programming
parameter constor,and the now when we create a object of in this constructor he we tell the how much object we created in the no argument.single or double
programming
parameter constor,and the now when we create a object of in this constructor he we tell the how much object we created in the no argument.single or double
Learning Java Programming for Beginners
Java is an Object Oriented Programming Language that was developed by Sun Microsystems. It is based on C, C++ programming language but a tad better... are eyeing the vast field of programming and software development
About Java Programming Language
About Java Programming Language       Java is an Object oriented application programming language developed by Sun Microsystems. Java is a very powerful general-purpose programming
Introduction to POJO (Plain Old Java Object) Programming Model
Introduction to POJO (Plain Old Java Object) Programming Model... features of added by Sun Microsystems in EJB 3.0 is POJO (Plain Old Java Object). It is a Java object that doesn't extend or implement some specialized
Object
are the basic units of the object-oriented programming. Objects are the part of our day... in terms of object-oriented programming. Objects are key to understanding object... Object       An object

Ads