使用JSlider缩放绘图

时间:2015-10-25 19:35:33

标签: java swing jframe actionlistener

这里有第一个问题。我正在尝试使用JSlider扩展自定义绘图。然而,它没有做任何事情,我不能为我的生活找出原因。我的代码抓取自定义形状并在最初绘制它,但它不会缩放。

class DrawFrame extends JFrame {

    private int CarWidth = 50;
    private CarShape shape = new CarShape(150, 150, CarWidth);

    public DrawFrame()
    {

        setTitle("Draw a Car");
        setSize(400, 400);


        JSlider slider = new JSlider(JSlider.VERTICAL, 1, 100, 50);
        slider.setMajorTickSpacing(5);
        slider.setPaintTicks(true);


        slider.addChangeListener((new ChangeListener() {
            public void stateChanged(ChangeEvent e) {
                JSlider source = (JSlider) e.getSource();
                int x = (int)source.getValue();
                CarWidth = x;
                repaint();
            }

        }));
        add(slider, BorderLayout.WEST);
        add(shape);

    }
}


public class CarShape extends JPanel {
       private int x;
       private int y;
       private int width;


       public CarShape(int x, int y, int width)
       {
          this.x = x;
          this.y = y;
          this.width = width;
       }

       public void update(int x){
        x = width;
    }

       public void paintComponent(Graphics g)
       {
           super.paintComponent(g);
           Graphics2D g2 = (Graphics2D) g; 
          Rectangle2D.Double body
                = new Rectangle2D.Double(x, y + width / 6, 
                      width - 1, width / 6);
          Ellipse2D.Double frontTire
                = new Ellipse2D.Double(x + width / 6, y + width / 3, 
                      width / 6, width / 6);
          Ellipse2D.Double rearTire
                = new Ellipse2D.Double(x + width * 2 / 3, y + width / 3,
                      width / 6, width / 6);

          // The bottom of the front windshield
          Point2D.Double r1
                = new Point2D.Double(x + width / 6, y + width / 6);
          // The front of the roof
          Point2D.Double r2
                = new Point2D.Double(x + width / 3, y);
          // The rear of the roof
          Point2D.Double r3
                = new Point2D.Double(x + width * 2 / 3, y);
          // The bottom of the rear windshield
          Point2D.Double r4
                = new Point2D.Double(x + width * 5 / 6, y + width / 6);
          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 class SliderTester {

    public static void main(String[] args)
       {
         DrawFrame frame = new DrawFrame();
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         frame.setVisible(true);
       }
}

1 个答案:

答案 0 :(得分:1)

每次调用更改侦听器时,它都会创建一个新的CarShape对象,但这对显示的CarShape对象没有影响。也许更好的是调整可视化对象的大小......好吧,你刚刚更改了我的代码还是我想象的东西?

现在您正在更改CarWidth(应该重命名为carWidth),但这不会改变可视化CarShape对象的状态。而是为您的CarShape类提供一个setCarWidth(int width)方法,一个更改其状态的方法,然后在stateChange方法中调用该方法。

如,

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

@SuppressWarnings("serial")
class DrawFrame extends JFrame {

    private int carWidth = 50;
    private CarShape shape = new CarShape(150, 150, carWidth);

    public DrawFrame() {

        setTitle("Draw a Car");
        setSize(400, 400);

        JSlider slider = new JSlider(JSlider.VERTICAL, 1, 100, 50);
        slider.setMajorTickSpacing(5);
        slider.setPaintTicks(true);

        slider.addChangeListener((new ChangeListener() {
            public void stateChanged(ChangeEvent e) {
                JSlider source = (JSlider) e.getSource();
                carWidth = (int) source.getValue();
                shape.setCarWidth(carWidth);
                repaint();
            }

        }));
        add(slider, BorderLayout.WEST);
        add(shape);

    }
}

@SuppressWarnings("serial")
class CarShape extends JPanel {
    private int x;
    private int y;
    private int width;

    public CarShape(int x, int y, int width) {
        this.x = x;
        this.y = y;
        this.width = width;
    }

    public void setCarWidth(int w) {
        this.width = w;
    }

    // this method is just messed up -- you're setting the parameter!
    public void update(int x) {
        x = width;  // no!!!
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        Rectangle2D.Double body = new Rectangle2D.Double(x, y + width / 6,
                width - 1, width / 6);
        Ellipse2D.Double frontTire = new Ellipse2D.Double(x + width / 6, y
                + width / 3, width / 6, width / 6);
        Ellipse2D.Double rearTire = new Ellipse2D.Double(x + width * 2 / 3, y
                + width / 3, width / 6, width / 6);

        // The bottom of the front windshield
        Point2D.Double r1 = new Point2D.Double(x + width / 6, y + width / 6);
        // The front of the roof
        Point2D.Double r2 = new Point2D.Double(x + width / 3, y);
        // The rear of the roof
        Point2D.Double r3 = new Point2D.Double(x + width * 2 / 3, y);
        // The bottom of the rear windshield
        Point2D.Double r4 = new Point2D.Double(x + width * 5 / 6, y + width / 6);
        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 class SliderTester {

    public static void main(String[] args) {
        DrawFrame frame = new DrawFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}