使用JSlider缩放绘图



第一个问题。我试图用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);
       }
}

每次调用更改侦听器时,它都会创建一个新的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);
    }
}

最新更新