Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

java create component can not appear

$
0
0
I have this code here:
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JComponent;

/**
   This component draws two car shapes.
*/
public class CarComponent extends JComponent
{  
	private Car car1;
	private Car car2;
	public CarComponent()
	{
		int x2 = getWidth() - 60;
		car1 = new Car(20,50);
		car2 = new Car(getWidth() - 60, 80);
		
	}
	public void paintComponent(Graphics g)
	{  
		Graphics2D g2 = (Graphics2D) g;
		car1.draw(g2);
		car2.draw(g2);

	}
	public void moveBy(int dx, int dy)
	{
		car1.translate(dx,dy);
		repaint();
		car2.translate(-dx,dy);
		repaint();
		
	}
}


import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;

/**
   A car shape that can be positioned anywhere on the screen.
*/
public class Car
{
   private int xLeft;
   private int yTop;

   /**
      Constructs a car with a given top left corner.
      @param x the x coordinate of the top left corner
      @param y the y coordinate of the top left corner
   */
   public Car(int x, int y)
   {
      xLeft = x;
      yTop = y;
   }

   /**
      Draws the car.
      @param g2 the graphics context
   */
   public void draw(Graphics2D g2)
   {
      Rectangle body 
            = new Rectangle(xLeft, yTop + 10, 60, 10);      
      Ellipse2D.Double frontTire 
            = new Ellipse2D.Double(xLeft + 10, yTop + 20, 10, 10);
      Ellipse2D.Double rearTire
            = new Ellipse2D.Double(xLeft + 40, yTop + 20, 10, 10);

      // The bottom of the front windshield
      Point2D.Double r1 
            = new Point2D.Double(xLeft + 10, yTop + 10);
      // The front of the roof
      Point2D.Double r2 
            = new Point2D.Double(xLeft + 20, yTop);
      // The rear of the roof
      Point2D.Double r3 
            = new Point2D.Double(xLeft + 40, yTop);
      // The bottom of the rear windshield
      Point2D.Double r4 
            = new Point2D.Double(xLeft + 50, yTop + 10);

      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.draw(body);
      g2.draw(frontTire);
      g2.draw(rearTire);
      g2.draw(frontWindshield);      
      g2.draw(roofTop);      
      g2.draw(rearWindshield);      
   }
   public void setLocation(int x, int y)
   {
		this.xLeft = x;
		this.yTop = y;
   }
   public void translate(int dx, int dy)
   {
		xLeft += dx;
		yTop += dy;
   }
}

import javax.swing.JFrame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
import javax.swing.JFrame;

public class CarViewer
{
	public static void main(String[] args)
	{
		JFrame frame = new JFrame("Car Viewer");
		final CarComponent car = new CarComponent();
		frame.add(car);

		class CarMover implements ActionListener
		{
			public void actionPerformed(ActionEvent event)
			{
				car.moveBy(1,0);
			}
		}
		
		ActionListener listener = new CarMover();
		Timer t = new Timer(100,listener);
		t.start();
		frame.setSize(300,400);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
	}
}



but when I display it, it just show one car. Of course when I change "car2 = new Car(getWidth() - 60, 80);
" of getWidth() to the constant it will appear two cars. Can someone please help me trying to fix this problem?

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>