简体   繁体   English

JFrame为什么不显示整个图像?

[英]Why isn't JFrame displaying the entire image?

Recently I have been experimenting with java graphics and decided to make a program that can print a Collage of images. 最近,我一直在尝试使用Java图形,并决定制作一个可以打印图像拼贴的程序。 The constructor for this program takes in an array of Images and the width that each image will be displayed at. 该程序的构造函数接受一个I​​mage数组,并显示每个图像的宽度。

import java.awt.*;
import java.awt.image.BufferedImage; 
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;
public class CollageTest extends JComponent
{
    private  Image [][] board; //Holds images within it.
    private int pixel;//width of image.


    public CollageTest(Image x[][], int pixel)
    {
        board = x;
        this.pixel = pixel;
    }
    public int getPixel()
    {
        return pixel;
    }
    public void paint(Graphics g)
    {
        for (int i = 0; i < board.length; i++)
        {
            for (int j = 0; j < board[0].length; j++)
            {
                g.drawImage(board[i][j], (i * pixel) , (j * pixel), pixel, pixel, null);
            }
        }
    }




    public static void main(String args[])
    {
        BufferedImage[][] images = new BufferedImage[20][20];
        BufferedImage img = null;
        try {
            img = ImageIO.read(new File("gorge3.jpg"));
        } catch (IOException e) {
        }

        for (int i = 0; i < images.length; i++)
        {
            for (int j = 0; j < images[0].length; j++)
            {
                images[i][j] = img;
            }
        }
        CollageTest test = new CollageTest(images, 20);
        JFrame frame = new JFrame("MyCollage");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(test);
        frame.setSize(test.getPixel() * images.length,test.getPixel() * images[0].length);
        frame.setVisible(true);
        test.repaint();

    }
}

To test the I initialized a CollageTest using an 20 x 20 array filled entirely with this Image. 为了进行测试,我使用了一个20 x 20的数组(完全填充了此Image)初始化了CollageTest。 我用这个图像填充了一个数组

When I ran the program originally I used The following to set the size of the JFrame's window: 最初运行该程序时,我使用以下代码来设置JFrame窗口的大小:

frame.setSize(test.getPixel() * images.length,test.getPixel() * images[0].length);

Unfortunately the JFrame did display part of the image. 不幸的是,JFrame确实显示了部分图像。 The JFrame window did not go low enough down to display all of the window. JFrame窗口的高度不够低,无法显示所有窗口。 To double double check that my math wasn't off, I tested using the following line in replacement: 要再次仔细检查我的数学是否还没有结束,我在替换中使用以下行进行了测试:

frame.setSize(400, 400);//Math 20 images * 20 pixels = 400 pixels

The same problem occured. 发生相同的问题。 It was not until I used the following line that the entire image was displayed: 直到我使用以下行显示了整个图像:

frame.setSize(400, 437);

So my question is, why is my JFrame misbehaving and is their a better way to fix it? 所以我的问题是,为什么我的JFrame行为异常,它们是一种更好的解决方法吗?

The problem lies in here: 问题出在这里:

frame.setSize(test.getPixel() * images.length,test.getPixel() * images[0].length);

You are manually setting the frame size according to the image size. 您正在根据图像尺寸手动设置帧尺寸。 However the frame size takes the title-bar and possibly borders into considerations. 但是,帧大小会考虑标题栏和可能的边框。 If the title-bar takes up 37 pixels, it leaves you 37 pixels lesser for the image. 如果标题栏占用了37个像素,则图像少了37个像素。

What I would do is: 我要做的是:

Add the image to a JPanel. 将图像添加到JPanel。 You may set the size of the panel according to the image. 您可以根据图像设置面板的尺寸。 pack() your JFrame after adding the panel containing your image so that it will determine the preferred size of the JFrame by itself. 在添加包含图像的面板之后,将JFrame pack() JFrame上,以便它自己确定JFrame的首选大小。

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

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