-1

我正在用java开发汽车模拟。我的编码如下。我想放一个面板,它是与 180 度的速度计图像一起旋转的线。我的问题是我该怎么做?

 public class House extends JPanel implements ActionListener
    {
    MyCanvas canvas;
    JButton mybutton,mybutton1,mybutton2,mybutton3,mybutton4,mybutton5;
    JTextField text1,text2,text3;
    JSlider sliderTransX, sliderTransY, sliderRotateTheta, sliderRotateX,
      sliderRotateY, sliderScaleX, sliderScaleY, sliderWidth;
    `enter code here`int k = 0;

  double transX = 0.0;
  double transY = 0.0;
  double rotateTheta = 0.0;
  double rotateX = 345.0;
  double rotateY = 250.0;
  double scaleX = 1.0;
  double scaleY = 1.0;
  float width = 1.0f;
  Cargame game = new Cargame();

  public House() 
  {
    super(new BorderLayout());
    JPanel controlPanel = new JPanel(new GridLayout(3, 3));
    add(controlPanel, BorderLayout.NORTH);
    sliderRotateTheta = setSlider(controlPanel, JSlider.HORIZONTAL, 0, 180,
        0, 90, 45);
    mybutton = new JButton("Accelerate");
    mybutton.addActionListener(this);
    mybutton1 = new JButton("ShiftUp");
    mybutton1.addActionListener(this);
    mybutton2 = new JButton("ShiftDown");
    mybutton2.addActionListener(this);
    mybutton3 = new JButton("Deaccelerate");
    mybutton3.addActionListener(this);
    mybutton4 = new JButton("Start Engine");
    mybutton4.addActionListener(this);
    mybutton5 = new JButton("Stop Engine");
    mybutton5.addActionListener(this);
    text1=new JTextField(5);
    text2=new JTextField(5);
    text3=new JTextField(5);

    sliderWidth = new JSlider(JSlider.HORIZONTAL, 0, 20, 1);
    sliderWidth.addChangeListener(new ChangeListener()
    {
      public void stateChanged(ChangeEvent e) {

        width = sliderWidth.getValue();
        canvas.repaint();
      }
    });

   JPanel widthPanel = new JPanel();
  widthPanel.setLayout(new GridLayout(3, 3));
    widthPanel.add(mybutton);
    widthPanel.add(mybutton1);
    widthPanel.add(mybutton2);
    widthPanel.add(mybutton3);
    widthPanel.add(mybutton4);
    widthPanel.add(mybutton5);
    widthPanel.add(text1);
    widthPanel.add(text2);
    widthPanel.add(text3);
    add(widthPanel, BorderLayout.SOUTH);

    canvas = new MyCanvas();
    add(canvas, "Center");
  }

  public void actionPerformed(ActionEvent e)
    {
      if (e.getSource() == mybutton)
      {
          game.Accelerate();
      }
      if (e.getSource() == mybutton1)
      {
          game.ShiftUp();
      }
       if (e.getSource() == mybutton2)
      {
          game.ShiftDown();
      }
        if (e.getSource() == mybutton3)
      {
          game.Deaccelerate();
      }

        JSlider tempSlider=new JSlider();
        rotateTheta= ((double)game.getLevel()) * Math.PI / 180;
        text1.setText(String.valueOf(game.getLevel()));
         text2.setText(String.valueOf(game.getGear()));
         text3.setText(String.valueOf(game.Speedometer()));
        canvas.repaint();
    }


  public JSlider setSlider(JPanel panel, int orientation, int minimumValue,
      int maximumValue, int initValue, int majorTickSpacing,
      int minorTickSpacing) {
    JSlider slider = new JSlider(orientation, minimumValue, maximumValue,
        initValue);

    slider.addChangeListener(new ChangeListener() {
      public void stateChanged(ChangeEvent e) {
        JSlider tempSlider = (JSlider) e.getSource();
sliderRotateTheta.setValue(k);
       if(tempSlider.equals(sliderRotateTheta))
       {
          rotateTheta = sliderRotateTheta.getValue() * Math.PI / 180;

          canvas.repaint();

          }}

    });
    slider.setVisible(false);
    panel.add(slider);
    return slider;
  }

  class MyCanvas extends Canvas {      
    public void paint(Graphics g) {
      Graphics2D g2D = (Graphics2D) g;
      g2D.rotate(rotateTheta, rotateX, rotateY);

      BasicStroke stroke = new BasicStroke(width);
      g2D.setStroke(stroke);

      drawHome(g2D);
    }

    public void drawHome(Graphics2D g2D) {
      Line2D line7 = new Line2D.Float(205f, 250f, 345f, 250f);
      g2D.draw(line7);
    }
  }

  public static void main(String[] a) {
    JFrame f = new JFrame();
    f.getContentPane().add(new House());
    f.setDefaultCloseOperation(1);
    f.setSize(700, 550);
    f.setVisible(true);
  }

}
4

1 回答 1

1

使用JFreeChart,您可以控制DialPlot使用 aJSliderJSpinner。此相关示例使用 aJSlider来控制折线图,​​更多示例可以在 JWS演示中找到。

附录:如此处所示,您可以使用圆方程的参数形式来计算指标线的自由端点。

于 2013-03-20T15:08:42.347 回答