Java Swing如何缩放三角形

时间:2019-04-29 21:17:43

标签: java swing graphics awt

当我更改窗口大小时,我想绘制一些随机三角形并缩放它们。

我有一个单独的类MyTriangle,它扩展了MyShape。我也有两个变量:scaleXscaleY。每当我尝试将三角形xy的值相乘并更改窗口大小时,三角形就会发疯并突然消失。 当我尝试写

graphics.fillPolygon(new int[]{(int)(x1*scX),(int)(x2*scX)(int)(x3*scX)}, new int[]{(int)(y1*scY),(int)(y2*scY),(int)(y3*scY), 3);

由于某种原因,我的窗口没有任何反应。

//from MyShape class:
//scX = window.getWidth()/(double)window.defaultWidth;
//scY = window.getHeight()/ (double)window.defaultHeight;

public class MyTriangle extends MyShape {
    int[] x;
    int[] y;
    public MyTriangle(int x1, int x2, int x3, int y1, int y2, int y3, int red, int green, int blue) {
        super(x1, x2, x3, y1, y2, y3, red, green, blue);
        this.x=new int[]{x1,x2,x3};
        this.y=new int[]{y1,y2,y3};
    }

    @Override
    protected void paintComponent(Graphics graphics) {
        super.paintComponent(graphics);
        for(int i = 0; i<3; i++){
            x[i]=(int)(x[i]*scX);
            y[i]=(int)(y[i]*scY);
        }
        graphics.fillPolygon(x,y,3);
    }

我应该如何使其起作用?像这样缩放,可以在矩形和椭圆形下正常工作。

2 个答案:

答案 0 :(得分:2)

每次绘制时都可以缩放比例。因此,如果缩放后​​缩放比例未设置为1,则每次绘制时缩放比例都会变大或变小。在paintComponent中创建新数组以传递缩放值,但保留原始值。
*编辑

 @Override
protected void paintComponent(Graphics graphics) {
    int[] xS = new int[3];
    int[] yS = new int[3]; 
    super.paintComponent(graphics);
    for(int i = 0; i<3; i++){
        xS[i]=(int)(x[i]*scX);
        yS[i]=(int)(y[i]*scY);
    }
    graphics.fillPolygon(xS,yS,3);
}

答案 1 :(得分:1)

由于您要处理多边形,所以最简单的解决方案是通过使用AffineTransform来更改输出比例来利用API的现有功能

Simple Scale

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class Main {

    public static void main(String[] args) {
        new Main();
    }

    public Main() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                MyTriangle shape = new MyTriangle(50, 100, 0, 0, 100, 100, 0, 0, 0);
                JSlider slider = new JSlider(0, 400);
                slider.setValue(100);
                frame.add(shape);
                frame.add(slider, BorderLayout.SOUTH);

                slider.addChangeListener(new ChangeListener() {
                    @Override
                    public void stateChanged(ChangeEvent arg0) {
                        double value = slider.getValue() / 100d;
                        shape.setScale(value);
                    }
                });

                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class MyTriangle extends JPanel {

        int[] x;
        int[] y;

        double scX;
        double scY;

        public MyTriangle(int x1, int x2, int x3, int y1, int y2, int y3, int red, int green, int blue) {
            this.x = new int[]{x1, x2, x3};
            this.y = new int[]{y1, y2, y3};
            scX = 1;
            scY = 1;
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        public void setScale(double scale) {
            scX = scale;
            scY = scale;
            repaint();
        }

        @Override
        protected void paintComponent(Graphics graphics) {
            super.paintComponent(graphics);
            Graphics2D g2d = (Graphics2D) graphics.create();
            AffineTransform at = AffineTransform.getScaleInstance(scX, scY);
            g2d.setTransform(at);
            g2d.fillPolygon(x, y, 3);
            g2d.dispose();
        }
    }
}

我强烈建议您查看2D Graphics Tutorial以获得更多详细信息