平铺的图像摆动 [英] Tiled images in swing

查看:88
本文介绍了平铺的图像摆动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有任务准备两个带秋千的窗户。一个包含正方形网格,其中包含随机数。第二,我需要加载平铺图像,然后以正确的顺序显示它们,形成平铺图像。

I have task to prepare two windows with swing. One contains grid of squares, with random numbers in them. In second I need to load pieces of tiled image and then show them in the correct order, forming tiled image.

Windows应如下所示:

Windows should look like this :

alt text http://img535.imageshack.us /img535/3129/lab8a.jpg

那么如何咬这个?我只使用了几次摆动来绘制一些二维折线,所以基本上我理论上现在该做什么。

Okay so how to bite this? I've used swing only few times to draw some 2d polylines, so basically I just theoretically now what to do.

好的,所以窗口编号1:
我开始为窗口创建Jframe。然后我做循环,并在其中创建16个JLabel随机数字?如何在每个图块和整个窗口之间设置边距?

Ok, so window number 1: I start with creating Jframe for the window. Then I do for loop and in it create 16 JLabels with random numbers in them? How to set margins between each tile and the whole window?

窗口编号2:
所以我开始相同,但是不是加载数字而是添加图像?现在,我如何从文件加载图像然后将其设置为背景?

Window number 2: So I start the same, but instead of loading numbers I add images? Now, how can I load image from file and then set it as background?

推荐答案

以下代码使用以下代码列出JLabels GridLayout的 GridLayout 的参数如下:rows,cols,horizo​​ntal gap,vertical gap。在下面的示例中,我在垂直和水平标签之间有3个像素宽的间隙。

The following code lays out the JLabels using the GridLayout. The arguments to the GridLayout are the following: rows, cols, horizontal gap, vertical gap. In the example below I have 3 pixels wide gap between labels both vertically and horizontally.

要使用图像而不是数字,您可以传递 ImageIcon 到JLabel的构造函数而不是文本。

To use images instead of numbers, you could pass an ImageIcon to the constructor of the JLabel instead of the text.

然而,看起来你正在做一个用户应该能够点击的游戏在瓷砖上。这表明您可能应该使用按钮而不是标签,但这取决于您: - )

However, it looks like your doing a game where the user should be able to click on the tiles. This suggests that you perhaps should use buttons instead of labels, but it's up to you :-)

import java.awt.GridLayout;

import javax.swing.*;
import javax.swing.border.BevelBorder;


public class FrameTest {

    public static void main(String[] args) {
        final JFrame f = new JFrame("Frame Test");

        JPanel panel = new JPanel(new GridLayout(4, 4, 3, 3));

        for (int i = 0; i < 16; i++) {
            JLabel l = new JLabel("" + i, JLabel.CENTER);
            //JLabel l = new JLabel(new ImageIcon("image_file.png"), JLabel.CENTER);
            l.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
            l.setFont(l.getFont().deriveFont(20f));
            panel.add(l);
        }

        f.setContentPane(panel);
        f.setSize(200, 200);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
    }
}

这篇关于平铺的图像摆动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆