Java Swing如何缩放三角形

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


我有一个单独的类MyTriangle扩展MyShape. 我也有两个变量:scaleX和scaleY. 每当我尝试将三角形x和y值相乘并更改窗口大小时,三角形就会变得疯狂并突然消失。当我尝试写作时


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

    }


我应该怎么做才能让它工作?像这样的缩放适用于矩形和椭圆形。


长风秋雁
浏览 147回答 2
2回答

慕尼黑5688855

每次绘制时都会缩放它。因此,如果在您进行缩放后比例因子没有设置为 1,那么每次绘制它时它都会变大或变小。在paintComponent 中创建新数组以传递缩放值但保留原始值。*编辑&nbsp;@Overrideprotected void paintComponent(Graphics graphics) {&nbsp; &nbsp; int[] xS = new int[3];&nbsp; &nbsp; int[] yS = new int[3];&nbsp;&nbsp; &nbsp; super.paintComponent(graphics);&nbsp; &nbsp; for(int i = 0; i<3; i++){&nbsp; &nbsp; &nbsp; &nbsp; xS[i]=(int)(x[i]*scX);&nbsp; &nbsp; &nbsp; &nbsp; yS[i]=(int)(y[i]*scY);&nbsp; &nbsp; }&nbsp; &nbsp; graphics.fillPolygon(xS,yS,3);}

慕神8447489

由于您正在处理多边形,因此最简单的解决方案是利用 API 的现有功能,通过使用 aAffineTransform来更改输出的比例https://i.stack.imgur.com/e2vLR.gif 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 {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; new Main();&nbsp; &nbsp; }&nbsp; &nbsp; public Main() {&nbsp; &nbsp; &nbsp; &nbsp; EventQueue.invokeLater(new Runnable() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void run() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JFrame frame = new JFrame();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MyTriangle shape = new MyTriangle(50, 100, 0, 0, 100, 100, 0, 0, 0);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSlider slider = new JSlider(0, 400);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; slider.setValue(100);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; frame.add(shape);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; frame.add(slider, BorderLayout.SOUTH);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; slider.addChangeListener(new ChangeListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void stateChanged(ChangeEvent arg0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; double value = slider.getValue() / 100d;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; shape.setScale(value);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; frame.pack();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; frame.setLocationRelativeTo(null);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; frame.setVisible(true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }&nbsp; &nbsp; public class MyTriangle extends JPanel {&nbsp; &nbsp; &nbsp; &nbsp; int[] x;&nbsp; &nbsp; &nbsp; &nbsp; int[] y;&nbsp; &nbsp; &nbsp; &nbsp; double scX;&nbsp; &nbsp; &nbsp; &nbsp; double scY;&nbsp; &nbsp; &nbsp; &nbsp; public MyTriangle(int x1, int x2, int x3, int y1, int y2, int y3, int red, int green, int blue) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.x = new int[]{x1, x2, x3};&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.y = new int[]{y1, y2, y3};&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; scX = 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; scY = 1;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public Dimension getPreferredSize() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return new Dimension(200, 200);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public void setScale(double scale) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; scX = scale;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; scY = scale;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; repaint();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; protected void paintComponent(Graphics graphics) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; super.paintComponent(graphics);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Graphics2D g2d = (Graphics2D) graphics.create();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; AffineTransform at = AffineTransform.getScaleInstance(scX, scY);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g2d.setTransform(at);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g2d.fillPolygon(x, y, 3);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g2d.dispose();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}我强烈建议您查看2D 图形教程以获取更多详细信息
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java