java动态调整图像大小以适应网格布局中的网格

发布于 2024-11-16 16:59:10 字数 133 浏览 3 评论 0原文

我想使用 JButton 的自定义子类制作国际象棋类型的棋盘。我的问题是我的棋子图像有点太小了。有没有办法让图像精确缩放到网格布局中每个网格的大小?如果我调整 Jframe 的大小,网格的大小也会改变。有没有办法让图像在调整整个框架大小时动态调整大小?

I want to make a chess type board using a custom subclass of JButton. My problem is that my images of the chess pieces are a bit too small. Is there a way I can get the image to scale to exactly the size of each grid in my gridlayout? If I resize the Jframe, the grids will change size as well. Is there a way to get the image to resize dynamically upon resizing of the whole frame?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

薆情海 2024-11-23 16:59:10

为此,您有 3 个选项

1) 使用 Gimp、Photoshop 等调整图像本身的大小。

2) 动态创建图标

Image i = icon.getImage();
if(i != null){
   int width = (int)(size * fraction);
   int height =(int)(size*icon.getIconHeight()/icon.getIconWidth()*fraction);
   miniature = new ImageIcon(i.getScaledInstance(width, height, Image.SCALE_SMOOTH));
}

3) 在框架的油漆上,您可以使用比例

private void scaledDrawing(Graphics g, float scale){
   Graphics2D g2 = (Graphics2D) g;
   AffineTransform at = new AffineTransform();
   AffineTransform save = g2.getTransform();
   at.setToIdentity();
   at.scale(goa.getScale().x, goa.getScale().y);
   g2.transform(at);
   image.paintIcon(c, g2);
   g2.setTransform(save);
}

You have 3 option for this

1) Resize the images themselves using Gimp, Photoshop, etc.

2) Create an icon dynamically

Image i = icon.getImage();
if(i != null){
   int width = (int)(size * fraction);
   int height =(int)(size*icon.getIconHeight()/icon.getIconWidth()*fraction);
   miniature = new ImageIcon(i.getScaledInstance(width, height, Image.SCALE_SMOOTH));
}

3) on the paint of your frame you can use scale

private void scaledDrawing(Graphics g, float scale){
   Graphics2D g2 = (Graphics2D) g;
   AffineTransform at = new AffineTransform();
   AffineTransform save = g2.getTransform();
   at.setToIdentity();
   at.scale(goa.getScale().x, goa.getScale().y);
   g2.transform(at);
   image.paintIcon(c, g2);
   g2.setTransform(save);
}
尐籹人 2024-11-23 16:59:10

您可以对图像进行一些转换,但它可能看起来有点难看。如果图像足够小,也许您可​​以强制按钮的最小尺寸,这样如果框架尺寸非常小,就会出现滚动条。另一种选择可能是拥有两组或三组不同的图像,其尺寸经过良好缩放,然后将它们交换为不同的板尺寸。

You could apply some transformation to the images but it might look a little ugly. If the images are small enough, maybe you can just force a minimum size of the button so that a scrollbar will appear if the frame is sized really small. Another option might be to have two or three different sets of the images at nicely scaled sizes, and swap them out for different board sizes.

我做我的改变 2024-11-23 16:59:10

另一种选择是覆盖绘图函数以填充所有可用位置:

@Override
public final void paint(final Graphics g) {
    super.paint(g);
    g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
}

这是一个示例:

import java.awt.Component;
import java.awt.Graphics;
import java.awt.Image;


public final class Tileset extends Component {

    private Image image;

    public Tileset(final Image image) {
        this.image = image;
    }

    @Override
    public final void paint(final Graphics g) {
        super.paint(g);
        g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
    }

    public final Image getImage() {
        return (image);
    }

    public final void setImage(final Image image) {
        this.image = image;
    }
}

With:

import javax.swing.JPanel;
import java.awt.GridLayout;


public final class Map extends JPanel {

    public Map(final GridLayout layout) {
        setLayout(layout);
    }

    public Map(final Integer width, final Integer height) {
        this(new GridLayout(width, height));
    }
}

And:

final Map map = new Map(13, 17);

final Image grass = new ImageIcon("src/main/res/tilesets/grass1.png").getImage();
final Image wood = new ImageIcon("src/main/res/tilesets/wood1.png").getImage();
final Image rock = new ImageIcon("src/main/res/tilesets/rock1.png").getImage();

for (int i = 0; i != 13; ++i) {
    for (int j = 0; j != 17; ++j) {
        if (i % 2 == 0) {
            if (j % 2 == 0)
                map.add(new Tileset(grass), i, j);
            else
                map.add(new Tileset(rock), i, j);
        }
        else
            map.add(new Tileset(wood), i, j);
    }
}

这将为您提供:

在此处输入图像描述

Another alternative will be to override the paint function to fill all the available place:

@Override
public final void paint(final Graphics g) {
    super.paint(g);
    g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
}

Here is an example:

import java.awt.Component;
import java.awt.Graphics;
import java.awt.Image;


public final class Tileset extends Component {

    private Image image;

    public Tileset(final Image image) {
        this.image = image;
    }

    @Override
    public final void paint(final Graphics g) {
        super.paint(g);
        g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
    }

    public final Image getImage() {
        return (image);
    }

    public final void setImage(final Image image) {
        this.image = image;
    }
}

With:

import javax.swing.JPanel;
import java.awt.GridLayout;


public final class Map extends JPanel {

    public Map(final GridLayout layout) {
        setLayout(layout);
    }

    public Map(final Integer width, final Integer height) {
        this(new GridLayout(width, height));
    }
}

And:

final Map map = new Map(13, 17);

final Image grass = new ImageIcon("src/main/res/tilesets/grass1.png").getImage();
final Image wood = new ImageIcon("src/main/res/tilesets/wood1.png").getImage();
final Image rock = new ImageIcon("src/main/res/tilesets/rock1.png").getImage();

for (int i = 0; i != 13; ++i) {
    for (int j = 0; j != 17; ++j) {
        if (i % 2 == 0) {
            if (j % 2 == 0)
                map.add(new Tileset(grass), i, j);
            else
                map.add(new Tileset(rock), i, j);
        }
        else
            map.add(new Tileset(wood), i, j);
    }
}

That will give you:

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文