简体   繁体   English

多个画布彼此相邻

[英]Multiple Canvas' next to each other

I am making an emulator for a seven segment display calculator with up to 10 digits. 我正在为最多7位显示计算器的10位数字制作模拟器。 I have the logic down where I have an array of size 10 of objects. 我有逻辑,我有一个大小为10的对象数组。 These objects take in a number and "turn on" the segments necessary to display the correct number on the canvas. 这些对象采用数字并“打开”在画布上显示正确数字所必需的段。 I am trying to have 10 separate canvas' next to each other and have a draw function that draws the correct number in each digit based on what segments are "turned on". 我试图将10个独立的画布彼此相邻,并具有一个draw函数,该函数基于“打开”了哪些段在每个数字中绘制正确的数字。 Anyways, I can figure that part out. 无论如何,我可以弄清楚那部分。 The part I can't seem to figure out is how to get 10 separate canvas' next to each other on a single JFrame. 我似乎无法弄清楚的部分是如何在单个JFrame上将10个单独的画布彼此相邻。 I have tried using 10 panels, but the formatting is not working and I'm having issues. 我尝试使用10个面板,但是格式不起作用,并且出现问题。 Any suggestions on how to make this? 关于如何做的任何建议?

This is a really basic example of 7 segment display set up using nothing more then custom JPanel s... 这是7段显示的一个非常基本的示例,它仅使用自定义JPanel来设置...

分割

Each element in the display is just a JPanel with some additional "properties", which are then laid out on another JPanel which acts as the primary display. 显示中的每个元素只是一个带有一些其他“属性”的JPanel ,然后将其放置在充当主显示的另一个JPanel上。 You can effect which segements are turned on and off through the use of a boolean array, where position; 您可以通过使用boolean数组(位置在哪里)来影响打开和关闭哪些拼写。

  • 0 is the top 0是顶部
  • 1 & 2 are the left and right, top half 1&2是左,右,上半部分
  • 3 is the middle 3是中间
  • 4 * 5 are the left and right, bottom half 4 * 5是左,右,下半部分
  • 6 is the bottom 6是底部

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.LineBorder;

public class TestSegmentDisplay {

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

    private SegmentDisplay segmentDisplay;
    private int count = 0;

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

                segmentDisplay = new SegmentDisplay();

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(segmentDisplay);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);

                final Timer timer = new Timer(1000, new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        setSegmentValue(segmentDisplay, count);
                        count++;
                        if (count > 9) {
                            count = 0;
                        }
                    }
                });
                timer.start();

            }
        });
    }

    public static void setSegmentValue(SegmentDisplay display, int value) {

        boolean[] states = new boolean[]{
            false,
            false, false,
            false,
            false, false,
            false
        };
        switch (value) {
            case 0:
                states = new boolean[]{
                    true,
                    true, true,
                    false,
                    true, true,
                    true};
                break;
            case 1:
                states = new boolean[]{
                    false,
                    false, true,
                    false,
                    false, true,
                    false};
                break;
            case 2:
                states = new boolean[]{
                    true,
                    false, true,
                    true,
                    true, false,
                    true};
                break;
            case 3:
                states = new boolean[]{
                    true,
                    false, true,
                    true,
                    false, true,
                    true};
                break;
            case 4:
                states = new boolean[]{
                    false,
                    true, true,
                    true,
                    false, true,
                    false};
                break;
            case 5:
                states = new boolean[]{
                    true,
                    true, false,
                    true,
                    false, true,
                    true};
                break;
            case 6:
                states = new boolean[]{
                    true,
                    true, false,
                    true,
                    true, true,
                    true};
                break;
            case 7:
                states = new boolean[]{
                    true,
                    false, true,
                    false,
                    false, true,
                    false};
                break;
            case 8:
                states = new boolean[]{
                    true,
                    true, true,
                    true,
                    true, true,
                    true};
                break;
            case 9:
                states = new boolean[]{
                    true,
                    true, true,
                    true,
                    false, true,
                    true};
                break;
        }

        display.setSegments(states);

    }

    public static class SegmentDisplay extends JPanel {

        private Segement[] segemnts;

        public SegmentDisplay() {
            segemnts = new Segement[7];
            segemnts[0] = new Segement(Segement.Direction.HORIZONTAL);
            segemnts[1] = new Segement(Segement.Direction.VERTICAL);
            segemnts[2] = new Segement(Segement.Direction.VERTICAL);
            segemnts[3] = new Segement(Segement.Direction.HORIZONTAL);
            segemnts[4] = new Segement(Segement.Direction.VERTICAL);
            segemnts[5] = new Segement(Segement.Direction.VERTICAL);
            segemnts[6] = new Segement(Segement.Direction.HORIZONTAL);

            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 1;
            gbc.gridy = 0;
            add(segemnts[0], gbc);

            gbc.gridx = 0;
            gbc.gridy = 1;
            add(segemnts[1], gbc);
            gbc.gridx = 2;
            add(segemnts[2], gbc);

            gbc.gridx = 1;
            gbc.gridy++;
            add(segemnts[3], gbc);

            gbc.gridx = 0;
            gbc.gridy++;
            add(segemnts[4], gbc);
            gbc.gridx = 2;
            add(segemnts[5], gbc);

            gbc.gridx = 1;
            gbc.gridy++;
            add(segemnts[6], gbc);
        }

        public void setSegments(boolean[] states) {

            if (states != null && states.length > 0 && states.length == segemnts.length) {

                for (int index = 0; index < segemnts.length; index++) {
                    segemnts[index].setOn(states[index]);
                }

            }

        }

    }

    public static class Segement extends JPanel {

        public enum Direction {

            VERTICAL,
            HORIZONTAL;
        }

        public static final int SMALL_SIZE = 10;
        public static final int LARGE_SIZE = SMALL_SIZE * 4;

        protected static final Color OUT_LINE = new Color(128, 0, 0);
        protected static final Color ON_COLOR = Color.RED;
        protected static final Color OFF_COLOR = new Color(64, 0, 0);

        private final Direction direction;
        private boolean on;

        public Segement(Direction direction) {
            this.direction = direction;
            setBorder(new LineBorder(OUT_LINE));
            setOn(true);
            setOn(false);
        }

        public void setOn(boolean value) {
            if (on != value) {
                on = value;
                if (on) {
                    setBackground(ON_COLOR);
                } else {
                    setBackground(OFF_COLOR);
                }
            }
        }

        public boolean isOn() {
            return on;
        }

        public Direction getDirection() {
            return direction;
        }

        @Override
        public Dimension getPreferredSize() {
            return getDirection() == Direction.VERTICAL ? new Dimension(SMALL_SIZE, LARGE_SIZE) : new Dimension(LARGE_SIZE, SMALL_SIZE);
        }

    }

}

There's probably a really clever way to set up the switch statement, but I can't really be bothered right now... 设置switch语句可能是一种非常聪明的方法,但是现在我真的不能打扰...

What I would then do, is add each SegmentDisplay to a JPanel using something like GridBagLayout or FlowLayout ... 然后,我要做的是使用诸如GridBagLayoutFlowLayout类的方法将每个SegmentDisplay添加到JPanel

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

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