在GUI中创建不同图像的多个实例

时间:2015-02-05 20:52:32

标签: java image user-interface simulator

我需要创建一个带有GUI的模拟器,我需要使用自定义图像来做到这一点。我花了2个多小时试图弄清楚如何使用多个不同图像的多个实例并将它们全部添加到显示器中,但我还没有设法让它工作。下面我发布了现在已经修改过的类,但是我在这个过程中尝试了很多不同的实现。我知道我的代码可能在此过程中搞砸了,但即使重新开始,我也无法找到解决方案。

概念很简单:我将有一个关于漆厂的模拟器,我需要模拟一个特定的配置,在下图中可视化:(它不是最终的可视化)

liquer plant

到目前为止,我只是尝试添加筒仓(大事:P)。下面是我的LiqPlantSim类,它最终将成为模拟器GUI处理程序。最后,将有另一个按钮所在的类,作为模拟器的控制面板。

import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.*;

public class LiqPlantSim extends JFrame{

    public static Silo silo,silo2,silo3,silo4;

    public LiqPlantSim(){
        super("Liq Plant Simulator");
        this.setFont(new Font("Helvetica", Font.PLAIN, 14));
        this.setBackground(Color.red);

        silo = new Silo(160,0);
        this.add(silo);
        silo2 = new Silo(440,0);
        this.add(silo2);
        silo3 = new Silo(160,310);
        this.add(silo3);
        silo4 = new Silo(440,310);
        this.add(silo4);

        this.setSize(800,600);    
        this.setLocation(100,100);
        this.setVisible(true);
        this.toFront();            
        this.setResizable(false);  
        this.addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent e){
                System.exit(0);
            }
        });
    }


}

以下是我的Silo课程。

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;

import javax.imageio.*;
import javax.swing.*;

public class Silo extends Component{

    BufferedImage img;
    int x,y;
    public Silo(int x,int y) {
        this.x=x;
        this.y=y;
        try {
            img = ImageIO.read(new File("img/EmptySilo.png"));
        } 
        catch (IOException e) {
            System.out.println("ERROR");
        }
    }

    public void paint(Graphics g) {
        g.drawImage(img, x, y, null);
    }

    public Dimension getPreferredSize() {
        if (img == null) {
           return new Dimension(100,100);
        } 
        else {
           return new Dimension(img.getWidth(null), img.getHeight(null));
        } 
    }

}

仅供参考,这是我的主要功能,我尝试创建一个LiqPlantSim的实例,它最终将成为模拟器;将有另一个窗口,其中包含用于处理模拟器的按钮。

public class Simulator {

    public static void main(String [] args){

        LiqPlantSim sim = new LiqPlantSim();
    }
}

我正在为任何需要的人添加Silo文件。

Silo

1 个答案:

答案 0 :(得分:2)

基本上,你正在与布局管理员作斗争。组件绘制是在组件的上下文中执行的,即位置0x0是组件的左上角。

首先关注Silo

的个别要求
public class Silo extends JComponent {

    BufferedImage img;

    public Silo() throws IOException {
        img = ImageIO.read(new File("img/EmptySilo.png"));
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(img, 0, 0, this);
    }

    @Override
    public Dimension getPreferredSize() {
        if (img == null) {
            return new Dimension(100, 100);
        } else {
            return new Dimension(img.getWidth(), img.getHeight());
        }
    }

    @Override
    public Dimension getMinimumSize() {
        return getPreferredSize();
    }

}

这还包括你可能要呈现的任何其他内容以及如何更改状态,但我会留给你...

接下来,您需要确定如何最好地布局每个组件。我的直觉是你需要使用GridBagLayout或者甚至想出一个最符合你要求的自定义布局管理器

在本次演示中,我只使用了GridLayout,但我认为您需要更复杂,更灵活的东西来满足您的整体需求

public class LiqPlantSim extends JFrame{

    public Silo silo,silo2,silo3,silo4;

    public LiqPlantSim(){
        super("Liq Plant Simulator");
        this.setFont(new Font("Helvetica", Font.PLAIN, 14));
        this.setBackground(Color.red);

        setLayout(new GridLayout(2, 2));

        silo = new Silo();
        this.add(silo);
        silo2 = new Silo();
        this.add(silo2);
        silo3 = new Silo();
        this.add(silo3);
        silo4 = new Silo();
        this.add(silo4);

        setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setResizable(false);  
        this.pack();
        this.setLocation(100,100);
        this.setVisible(true);
        this.toFront();            
    }


}

<强>更新

您可以通过多种方式布局最终视图,所有这些方法都将归结为通过您的问题无法提供的实现特定详细信息。

我通常会尽可能地坚持使用预定义的布局,例如GridBagLayout,但这种方式大多简单,但有时,您可能需要构建自己的布局管理器以获得您想要的内容。< / p>

在一天结束时,您希望尽一切可能分离和隔离责任区域,例如,不要试图在一个容器中尝试布局一切,它会让您疯狂

以下使用通常称为复合布局的技术,利用两个不同的布局管理器来实现最终目标。

Layout

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new Brewery());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class Brewery extends JPanel {

        public Brewery() {
            setBackground(Color.WHITE);
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            try {

                gbc.gridx = 0;
                gbc.gridy = 0;
                gbc.anchor = GridBagConstraints.SOUTH;
                JPanel top = new JPanel(new GridLayout(1, 2));
                top.setOpaque(false);
                top.add(new Silo());
                top.add(new Silo());
                add(top, gbc);

                gbc.gridy++;
                gbc.gridwidth = GridBagConstraints.REMAINDER;
                add(new Pipe(), gbc);

                gbc.anchor = GridBagConstraints.NORTH;
                gbc.gridy++;
                gbc.gridwidth = 1;
                JPanel bottom = new JPanel(new GridLayout(1, 2));
                bottom.setOpaque(false);
                bottom.add(new Silo());
                bottom.add(new Silo());
                add(bottom, gbc);
            } catch (IOException exp) {
                exp.printStackTrace();
            }
        }

    }

    public class Silo extends JComponent {

        BufferedImage img;

        public Silo() throws IOException {
            setBorder(new EmptyBorder(0, 50, 0, 50));
            img = ImageIO.read(new File("img/EmptySilo.png"));
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            int x = (getWidth() - img.getWidth()) / 2;
            int y = (getHeight() - img.getHeight()) / 2;
            g.drawImage(img, x, y, this);
        }

        @Override
        public Dimension getPreferredSize() {
            Dimension dim = new Dimension(100, 100);
            if (img != null) {
                dim = new Dimension(img.getWidth(), img.getHeight());
            }
            Insets insets = getInsets();
            dim.width += insets.left + insets.right;
            dim.height += insets.top + insets.bottom;
            return dim;
        }

        @Override
        public Dimension getMinimumSize() {
            return getPreferredSize();
        }

    }


    public class Pipe extends JComponent {

        BufferedImage img;

        public Pipe() throws IOException {
            img = ImageIO.read(new File("img/Pipe.png"));
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            int x = (getWidth() - img.getWidth()) / 2;
            int y = (getHeight() - img.getHeight()) / 2;
            g.drawImage(img, x, y, this);
        }

        @Override
        public Dimension getPreferredSize() {
            if (img == null) {
                return new Dimension(100, 100);
            } else {
                return new Dimension(img.getWidth(), img.getHeight());
            }
        }

        @Override
        public Dimension getMinimumSize() {
            return getPreferredSize();
        }

    }
}

有关详细信息,请参阅Laying Out Components Within a Container