简体   繁体   English

在 Java (JFrame) 中重新缩放绘图

[英]Rescale a drawing in Java (JFrame)

I need the simplest way to rescale a drawing in java (for example a rectangle...).我需要最简单的方法来重新缩放 Java 中的绘图(例如矩形......)。 I found a way to "stretch" them:我找到了一种“拉伸”它们的方法:

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import java.awt.Dimension;

public class Stretch extends JFrame {
    int originalHeight = 600;
    int originalWidth = 600;
    public Stretch() {
        super("Stretch");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(originalWidth, originalHeight);
    }
    public static void main(String[] args) {
        Stretch s = new Stretch();
        s.setVisible(true);
    }
    public void paint(Graphics g) {
        Dimension size = this.getBounds().getSize();
        int rectWidth = 100;
        int rectHeight = 130;

        g.setColor(Color.white);
        g.fillRect(0, 0, size.width, size.height);

        g.setColor(Color.black);
        g.drawRect(100, 100, rectWidth + size.width - originalWidth, rectHeight + size.height - originalHeight);

    }
}

As you can see the formula is like that:正如你所看到的,公式是这样的:

g.drawRect(100, 100, rectWidth + size.width - originalWidth, rectHeight + size.height - originalHeight);

Now how can I rescale the drawing?现在如何重新缩放绘图? Width and height have to maintain the same proportion.宽度和高度必须保持相同的比例。 Thank you.谢谢你。

There are bascially two different approaches for this:对此,基本上有两种不同的方法:

  • You can "scale the whole Graphics "您可以“缩放整个Graphics
  • You can scale the actual shapes您可以缩放实际形状

The difference may be subtle in some cases, but can be important: When you scale the whole Graphics , then everything will be scaled.在某些情况下,差异可能很小,但可能很重要:当您缩放整个Graphics所有内容都将被缩放。 Particularly, when you scale it about 2.0 , and then draw a line with a width of 1.0 , the line will be drawn 2 pixels wide.特别是,当您将其缩放约2.0 ,然后绘制一条宽度为1.0的线时,该线将被绘制为 2 个像素宽。 Whether or not this is desired depends on the application case.这是否需要取决于应用案例。

In order to scale the actual shapes, you can not use the drawRect method.为了缩放实际形状,您不能使用drawRect方法。 Instead you will have to create Shape instances that represent the geometric shapes.相反,您必须创建表示几何形状的Shape实例。 You can then create scaled versions of these shapes with AffineTransform#createTransformedShape .然后,您可以使用AffineTransform#createTransformedShape创建这些形状的缩放版本。

Here is an example that compares both approaches (and corrects some of the other issues that have been in your code).这是一个比较两种方法的示例(并纠正了您的代码中存在的一些其他问题)。 In both cases, the same rectangle with an original size of (10,13) is painted, scaled by a factor of 5.0.在这两种情况下,都绘制了原始大小为 (10,13) 的相同矩形,缩放比例为 5.0。

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class ScalingDrawnObjects
{
    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                createAndShowGUI();
            }
        });
    }

    private static void createAndShowGUI()
    {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        ScalingDrawnObjectsPanel p = new ScalingDrawnObjectsPanel();
        f.getContentPane().add(p);
        f.setSize(600,400);
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }
}

class ScalingDrawnObjectsPanel extends JPanel
{
    @Override
    protected void paintComponent(Graphics gr)
    {
        super.paintComponent(gr);
        Graphics2D g = (Graphics2D)gr;

        Shape rectangle = new Rectangle2D.Double(2, 2, 10, 13);

        g.setColor(Color.RED);
        drawWithScaledGraphics(g, rectangle);

        g.translate(100, 0);

        drawScaledObject(g, rectangle);
    }

    private static void drawWithScaledGraphics(Graphics2D g, Shape shape)
    {
        AffineTransform oldAt = g.getTransform();
        g.scale(5.0, 5.0);
        g.draw(shape);
        g.setTransform(oldAt);
    }

    private static void drawScaledObject(Graphics2D g, Shape shape)
    {
        AffineTransform at = AffineTransform.getScaleInstance(5.0, 5.0);
        g.draw(at.createTransformedShape(shape));
    }

}

EDIT In response to the comment编辑回应评论

The code that I posted is not "complicated".我发布的代码并不“复杂”。 It is as compilcated as it has to be, but not more.它是复杂的,因为它必须,但不是更多。 You should not extend JFrame and you should not override paint .您不应扩展JFrame并且不应覆盖paint You should create the GUI on the EDT.您应该在 EDT 上创建 GUI。 You should ...你应该 ...

However, you should not use code like the following, but maybe this is what you're looking for但是,您不应该使用如下代码,但也许这就是您要寻找的

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import java.awt.Dimension;

public class Stretch extends JFrame {
    int originalHeight = 600;
    int originalWidth = 600;
    public Stretch() {
        super("Stretch");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(originalWidth, originalHeight);
    }
    public static void main(String[] args) {
        Stretch s = new Stretch();
        s.setVisible(true);
    }
    public void paint(Graphics g) {
        Dimension size = this.getBounds().getSize();
        int rectWidth = 100;
        int rectHeight = 130;

        g.setColor(Color.white);
        g.fillRect(0, 0, size.width, size.height);

        g.setColor(Color.black);

        int w = rectWidth + size.width - originalWidth;
        int h = rectHeight + size.height - originalHeight;
        double sx = (double)w / rectWidth;
        double sy = (double)h / rectHeight;
        double s = Math.min(sx, sy);
        int fw = (int)(s * rectWidth);
        int fh = (int)(s * rectHeight);
        g.drawRect(100, 100, fw, fh);

    }
}

How about multiplying the width and the height by the scale you want?将宽度和高度乘以您想要的比例怎么样?

So if you want to scale it by 2:因此,如果您想将其缩放 2:

g.drawRect(100, 100, 2 * (rectWidth + size.width - originalWidth), 2 * (rectHeight + size.height - originalHeight));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM