Java Swing How to scale triangle

丶灬走出姿态 提交于 2019-12-11 16:59:44

问题


I would like to draw some random triangles and scale them, when I'm changing the size of my window.

I have a separate class MyTriangle that extends MyShape. I also have two variables: scaleX and scaleY. Whenever I try to multiply triangle x and y values and change the window size, triangles go crazy and suddenly disappear. When I try to write

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);

Nothing happens in my window for some reason.

//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);
    }

How should I do it to make it work? Scaling like this works fine with rectangles and ovals.


回答1:


you scale it every time you paint it. so if the scale factor doesn't get set to 1 after you do a zoom then every time it paints it it'll either get bigger or smaller. in paintComponent make new arrays to pass the scaled values but keep the originals.
*EDIT

 @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);
}



回答2:


Since you're dealing with a polygon, the simplest solution would be to take advantage of the already existing functionality of the API, by using a AffineTransform to change the scale of the output

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();
        }
    }
}

I would highly recommend having a look at the 2D Graphics Tutorial for more details



来源:https://stackoverflow.com/questions/55910927/java-swing-how-to-scale-triangle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!