Question

1. Design and implement an application that displays an animation of a car (side view) moving across the screen from left to right. Create a Car class that represents the car. (Hint: Use timer class as in the Rebound class and here are the list of Graphics methods you can use to draw different shapes: 2. Design and implement an application that draws a traffic light and uses a push button to change the state of the light. Derive the drawing surface from the JPanel class and use another panel to organize the drawing surface and the button.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.awt.*;

public class Car {

private int x, y;

private java.awt.Color color;

private boolean isFilled;

/* Creates a new instance of Car /

public Car () {

x = 0;

y = 50;

color = Color.RED;

isFilled = true;

}

public Car (int x, int y, Color color, boolean isFilled ){

this.x = x > 0 ? x: 0;

this.y = y > 40 ? y : 40;

this.color = color;

this.isFilled = isFilled;

}

public void draw (Graphics g){

g.setColor (color);

int xs[] = {x*2, x*2+15, x*2+65, x*2+100};

int ys[] = {y, y-20, y-20, y};

g.fillPolygon (xs, ys, 4);

g.fillRoundRect (x*2, y, 105, 20, 5,5);

g.setColor (Color.black);

g.fillOval (10 + x*2, y+20, 18, 18);

g.fillOval (70 + x*2, y+20, 18, 18);

}

public void move (Graphics g){

x++;

draw (g);

}

}

// Test.java

import java.awt.Color;

import java.awt.Graphics;

import javax.swing.*;

public class Test extends JApplet {

private Car c, c2;

public Test (){

c = new Car ();

c2 = new Car (0,180,Color.ORANGE,true);

}

public void run () {

while(true){

try{

repaint ();

Thread.sleep (20);

} catch (InterruptedException e) {

}

}

}

public void paint (Graphics g) {

super.paint (g);

c.move (g);

c2.move (g);

}

public static void main (String[] args) {

JFrame frame = new JFrame ();

Test t = new Test ();

frame.setTitle ("Moving Car");

frame.getContentPane ().add (t);

frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

frame.setSize (1024, 400);

frame.setVisible (true);

t.run ();

}

}

Add a comment
Know the answer?
Add Answer to:
1. Design and implement an application that displays an animation of a car (side view) moving...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Modify the NervousShapes program so that it displays equilateral triangles as well as circles and rectangles....

    Modify the NervousShapes program so that it displays equilateral triangles as well as circles and rectangles. You will need to define a Triangle class containing a single instance variable, representing the length of one of the triangle’s sides. Have the program create circles, rectangles, and triangles with equal probability. Circle and Rectangle is done, please comment on your methods so i can understand */ package NervousShapes; import java.awt.*; import java.awt.event.*; import java.awt.image.*; import javax.imageio.*; import javax.swing.*; import javax.swing.event.*; public class...

  • Java Painter Class This is the class that will contain main. Main will create a new...

    Java Painter Class This is the class that will contain main. Main will create a new Painter object - and this is the only thing it will do. Most of the work is done in Painter’s constructor. The Painter class should extend JFrame in its constructor.  Recall that you will want to set its size and the default close operation. You will also want to create an overall holder JPanel to add the various components to. It is this JPanel that...

  • WRITING METHODS 1. Implement a method named surface that accepts 3 integer parameters named width, length,...

    WRITING METHODS 1. Implement a method named surface that accepts 3 integer parameters named width, length, and depth. It will return the total surface area (6 sides) of the rectangular box it represents. 2. Implement a method named rightTriangle that accepts 2 double arameters named sideA and hypotenuseB. The method will return the length of the third side. NOTE To test, you should put all of these methods into a ‘MyMethods’ class and then write an application that will instantiate...

  • ***This is a JAVA question*** ------------------------------------------------------------------------------------------------ import java.awt.*; import java.awt.event.*; import java.awt.image.*; import javax.imageio.*; import javax.swing.*;...

    ***This is a JAVA question*** ------------------------------------------------------------------------------------------------ import java.awt.*; import java.awt.event.*; import java.awt.image.*; import javax.imageio.*; import javax.swing.*; import javax.swing.event.*; public class DrawingPanel implements ActionListener { public static final int DELAY = 50; // delay between repaints in millis private static final String DUMP_IMAGE_PROPERTY_NAME = "drawingpanel.save"; private static String TARGET_IMAGE_FILE_NAME = null; private static final boolean PRETTY = true; // true to anti-alias private static boolean DUMP_IMAGE = true; // true to write DrawingPanel to file private int width, height; // dimensions...

  • Lab Assignment : In this lab, you are going to implement QueueADT interface that defines a...

    Lab Assignment : In this lab, you are going to implement QueueADT interface that defines a queue. Then you are going to use the queue to store animals. 1. Write a LinkedQueue class that implements the QueueADT.java interface using links. 2. Textbook implementation uses a count variable to keep track of the elements in the queue. Don't use variable count in your implementation (points will be deducted if you use instance variable count). You may use a local integer variable...

  • One example of computer-aided design (CAD) is building geometric structures inter- actively. In Chapter 4, we...

    One example of computer-aided design (CAD) is building geometric structures inter- actively. In Chapter 4, we will look at ways in which we can model geometric objects comprised of polygons. Here, we want to examine the interactive part. Let’s start by writing an application that will let the user specify a series of axis- aligned rectangles interactively. Each rectangle can be defined by two mouse positions at diagonally opposite corners. Consider the event listener canvas.addEventListener("mousedown", function() { gl.bindBuffer(gl.ARRAY_BUFFER, vBuffer); if...

  • could you please help me with this problem, also I need a little text so I...

    could you please help me with this problem, also I need a little text so I can understand how you solved the problem? import java.io.File; import java.util.Scanner; /** * This program lists the files in a directory specified by * the user. The user is asked to type in a directory name. * If the name entered by the user is not a directory, a * message is printed and the program ends. */ public class DirectoryList { public static...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT