继承画一辆卡车

时间:2016-04-08 22:24:24

标签: java swing

我最近制作了一辆汽车的图纸,并试图将.fillPolygon更改为.fillRect,但它并没有改变汽车看起来像一辆卡车。我如何更改下面的代码以将汽车的绘图变成卡车的图纸?我在Eclipse上做了这个。

Car.java

import java.awt.*;

public class Car {
    //Coordinates if car is drawn at position 0,0
    private int [] x = {0, 0, 20, 25, 70, 80, 105, 110};
    private int[] y = {35, 15, 15, 0, 0, 15, 15, 35};

    private int [] xCurrent = new int [x.length];
    private  int [] yCurrent = new int [y.length];

    private int xOffset = 0, yOffset =0;
    private Color carColor;

      //-----------------------------------------------------------------
    //  Sets up the graphical car with the specified offsets.
    //-----------------------------------------------------------------
    public Car(int xOff, int yOff, Color color)
    {
       xOffset = xOff;
       yOffset = yOff;
       carColor = color;

       for (int i=0; i < x.length; i++)
       {
          xCurrent[i] = x[i] + xOffset;
          yCurrent[i] = y[i] + yOffset;
       }
    }

    //
    public int getXOffset() {return xOffset;}
    public int getYOffset() {return yOffset;}

    //-----------------------------------------------------------------
    //  Draws the car at a particular x and y offset.
    //-----------------------------------------------------------------
    public void draw(Graphics page)
    {
       page.setColor(carColor);
       page.fillPolygon(xCurrent, yCurrent, x.length);

       page.setColor(Color.black);
       page.fillOval(13+xOffset, 28+yOffset, 14, 14);  // rear wheel
       page.fillOval(83+xOffset, 28+yOffset, 14, 14);  // front wheel
       page.drawLine(15+xOffset, 18+yOffset, 15+xOffset, 3+yOffset);
    }
 }

CarPanel.java

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

public class CarPanel extends JPanel {
       private Car car1, car2, car3;
       private final int DELAY =20;
       private int x, y;
       private final int SPEED =2;
       public CarPanel ()
       {
           car1 =new Car(200, 150, Color.BLUE);
           car2 = new Car(50, 50, Color.RED);
           car3 = new Car(0, 220, Color.GREEN);
           x =0;
           y= 220;
           setPreferredSize(new Dimension(450,300));

           ActionListener taskPerformer = new ActionListener()
           {
               public void actionPerformed(ActionEvent evt) 
                {
                    x = car3.getXOffset() + SPEED;
                    y = car3.getYOffset();
                    if (x > getWidth()) x = 0;
                    car3 = new Car(x, y, Color.GREEN);
                    x = car2.getXOffset() + SPEED + 5;
                    if (x > getWidth()) x = 0;
                    y = car2.getYOffset();
                    car2 = new Car(x, y, Color.RED);
                    repaint();
                }
          };

          new Timer(DELAY, taskPerformer).start();

       }

       //-----------------------------------------------------------------
       //  Draws the car.
       //-----------------------------------------------------------------
       public void paint(Graphics page)
       {
          super.paint(page);
          car1.draw(page);
          car2.draw(page);
          car3.draw(page);
       }
}

GUITester.java

import javax.swing.JFrame;
public class GUITester {

    //-----------------------------------------------------------------
       //  Sets up a frame containing a tabbed pane. The panel on each
       //  tab demonstrates a different layout manager.
       //-----------------------------------------------------------------
       public static void main(String[] args)
       {
          JFrame frame = new JFrame("GUI Tester");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          // add any panel into here
          //ArrayReviews panel = new ArrayReviews();
          //ArrayBetter panel = new ArrayBetter();
          CarPanel panel = new CarPanel();
          //NumericKeypadPanel panel = new NumericKeypadPanel();
          //StarPanel panel = new StarPanel();
          //SnowPanel panel = new SnowPanel();
          //Draw1StarPanel panel = new Draw1StarPanel();
          //Rain panel = new Rain();
          //Cards panel = new Cards();
          //SelectionSortPanel panel = new SelectionSortPanel();
          //PersonPanel panel = new PersonPanel();
          //SinPanel panel = new SinPanel();
          //KochSnowFlake panel = new KochSnowFlake();
          //CalculatorPanelSimple panel = new CalculatorPanelSimple();
          frame.getContentPane().add(panel);
          frame.pack();
          frame.setVisible(true);
       }
}

1 个答案:

答案 0 :(得分:0)

育!您宣布x[] y[]数组,然后构建xOffset[] yOffset[]数组,将您的汽车/卡车转换为不同的位置。您可以让GPU为您完成工作:

void draw(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;  // Every Graphics is actually a Graphics2D.
    translate(xOffset, yOffset);      // Move the graphic origin
    g2d.fillPolygon(x, y, x.length);  // Draw with original coordinates
    translate(-xOffset, yOffset);     // Restore graphic origin, for other drawing
}

它不会把你的车变成卡车,但是你的代码没有显示你的尝试,所以很难说你出错了。

但是,如果这简化了您的代码,也许这会有所帮助。

@MadProgrammer所述,Shape API也可能有用。

static Shape CAR_SHAPE;

static {
    // Initialize CAR_SHAPE
}

void draw(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    translate(xOffset, yOffset); 
    g2d.draw(CAR_SHAPE); 
    translate(-xOffset, yOffset); 
}

请参阅Trail: 2D Graphics