Java大规模多帧实例问题

时间:2014-11-13 09:26:53

标签: java swing class instance frame

叹气好人......这里会有一些辛苦的代码,但无论如何我都会这样做。
基本上,我有一个定制的(实际上它只是JFrame的一个HEAVILY定制版本),并且遇到了重大问题。

我有一个背景。 (很好,那没关系)那么我有一个终端框架弹出并吐出东西。此终端框架基于另一个名为CustomFrame的类。我还有另一个名为Notification的类,它也是一个基于自定义帧的终端ALSO的帧类。

一开始,后台加载很好。终端负载很好。调用方法以显示通知窗口。这就是问题出现的地方。通知窗口不会显示。

我试过了frame.setVisible(); frame.setSize(); frame.setLocation();我试过了,一切。

如果我根本没有显示终端,它似乎会把它的代码吐到Notification上,几乎就像只有一个CustomFrame实例在所有时间都打开。

我希望你理解我的问题......所以这是代码!

Game.java

public class Game implements KeyListener {

int BACK_WIDTH = java.awt.Toolkit.getDefaultToolkit().getScreenSize().width;
int BACK_HEIGHT = java.awt.Toolkit.getDefaultToolkit().getScreenSize().height;

JFrame back_frame = new JFrame();
JPanel window = new JPanel();
JLabel title = new JLabel(Variables.TITLE);

Terminal login = new Terminal();

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

public Game() {
    try {

        back_frame.setSize(BACK_WIDTH, BACK_HEIGHT);
        back_frame.setLocation(0, 0);
        back_frame.getContentPane().setBackground(Color.BLACK);
        back_frame.setUndecorated(true);
        back_frame.setVisible(true);
        back_frame.add(window);
        window.setBackground(Color.BLACK);
        window.setLayout(null);

        window.add(title);
        title.setBounds((BACK_WIDTH / 2) - (550 / 2), (BACK_HEIGHT / 2) - (50 / 2), 550, 50);
        title.setForeground(Color.WHITE);

        back_frame.addKeyListener(this);
        login.addKeyListener(this);
        login.setLocationRelativeTo(null);
        login.setVariables(Types.LOGINTERMINAL);

        waitForStart();

    } catch (FontFormatException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

int index;
public void waitForStart() {
    Timer timer = new Timer(2000, new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (index < 1 && index >= 0) {
                index++;
            } else {
                ((Timer)e.getSource()).stop();

                login.setVisible(true);
                login.slowPrint("Please login to continue...\n"
                          + "Type 'help' for more information.\n");
            }
       }
    });
    timer.start();
}

public void keyPressed(KeyEvent e) {
    int i = e.getKeyCode();

    if(i == KeyEvent.VK_ESCAPE) {
        System.exit(0);
    }
}

public void keyReleased(KeyEvent e) {}

public void keyTyped(KeyEvent e) {}

}

CustomFrame.java

public class CustomFrame implements MouseListener {

static JFrame frame = new JFrame();
public static Paint window = new Paint();

public void addKeyListener(KeyListener listener) {
    frame.addKeyListener(listener);
}

private Point initialClick;
private boolean inBounds = false;

public int getWidth() {
    return frame.getWidth();
}
public int getHeight() {
    return frame.getHeight();
}

public void add(JComponent component) {
    window.add(component);
}

public void setLocation(int x, int y) {
    frame.setLocation(x, y);
}

public void setLocationRelativeTo(Component c) {
    frame.setLocationRelativeTo(c);
}

private void setFrameType(Types type) {
    switch(type) {
        case TERMINAL:
            frame.setSize(600, 400);
            break;
        case LOGINTERMINAL:
            frame.setSize(600, 400);
            break;
        case NOTIFICATION:
            frame.setSize(300, 150);
            break;
        default:
            frame.setSize(600, 400);
            break;
    }
}

int index = 0;
public void slowPrint(final String text, final JTextArea field) {
    Timer timer = new Timer(40, new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (index < text.length() && index >= 0) {
                String newChar = Character.toString(text.charAt(index));
                field.append(newChar);
                index++;
            } else {
                field.append("\n");

                index = 0;
                ((Timer)e.getSource()).stop();
            }
       }
    });
    timer.start();
}

public void slowPrintAndClear(final String text, final JTextArea field, final boolean andQuit) {
    Timer timer = new Timer(40, new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (index < text.length() && index >= 0) {
                String newChar = Character.toString(text.charAt(index));
                field.append(newChar);
                index++;
            } else {
                field.append("\n");

                if(andQuit == false) {
                    field.setText(null);
                } else {
                    System.exit(0);
                }

                index = 0;
                ((Timer)e.getSource()).stop();
            }
       }
    });
    timer.start();
}

public CustomFrame(Types type) {

    frame.setAlwaysOnTop(true);
    frame.addMouseListener(this);
    frame.setResizable(false);
    frame.setUndecorated(true);
    setFrameType(type);
    frame.add(window);
    window.setLayout(null);

    frame.addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent e) {
            initialClick = e.getPoint();
            frame.getComponentAt(initialClick);
        }
    });

    frame.addMouseMotionListener(new MouseMotionAdapter() {
            public void mouseDragged(MouseEvent e) {
                if(e.getX() >= 0 && e.getX()<= frame.getWidth() &&
                   e.getY() >= 0 && e.getY() <= 20) {
                    inBounds = true;
                }
                if(inBounds == true) {
                    int thisX = frame.getLocation().x;
                    int thisY = frame.getLocation().y;
                    int xMoved = (thisX + e.getX()) - (thisX + initialClick.x);
                    int yMoved = (thisY + e.getY()) - (thisY + initialClick.y);
                    int x = thisX + xMoved;
                    int y = thisY + yMoved;
                    frame.setLocation(x, y);
                }
            }
        });
}

public JFrame setVisible(boolean bool) {
    frame.setVisible(bool);
    return null;
}

public void mouseClicked(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}

public void mousePressed(MouseEvent e) {
    int x = e.getX();
    int y = e.getY();

    if(x >= CustomFrame.frame.getWidth() - 20 && x <= CustomFrame.frame.getWidth() - 6 &&
       y >= 3 && y <= 14) {
        frame.dispose();
    }

}

public void mouseReleased(MouseEvent e) {
    inBounds = false;
}

}

class Paint extends JPanel {
private static final long serialVersionUID = 1L;

private void doDrawing(Graphics g) {

    Graphics2D g2d = (Graphics2D) g;

    g2d.setColor(Color.BLACK);
    g2d.fillRect(0, 0, CustomFrame.frame.getWidth(), CustomFrame.frame.getHeight());

    Color LIGHT_BLUE = new Color(36, 171, 255);

    //g2d.setColor(Color.BLUE);
    GradientPaint topFill = new GradientPaint(0, 0, LIGHT_BLUE, CustomFrame.frame.getWidth(), 20, Color.BLUE);
    g2d.setPaint(topFill);

    g2d.fillRect(0, 0, CustomFrame.frame.getWidth(), 20);

    g2d.setColor(Color.WHITE);
    g2d.drawRect(0, 0, CustomFrame.frame.getWidth() - 1, CustomFrame.frame.getHeight() - 1);
    g2d.drawLine(0, 20, CustomFrame.frame.getWidth(), 20);

    g2d.fillRect(CustomFrame.frame.getWidth() - 20, 3, 14, 14);

}

public void paintComponent(Graphics g) {

    super.paintComponent(g);
    doDrawing(g);
}
}

Terminal.java

public class Terminal implements KeyListener {

static CustomFrame frame = new CustomFrame(Types.TERMINAL);

JTextArea log = new JTextArea();
JTextField field = new JTextField();

public void setVisible(boolean bool) {
    frame.setVisible(bool);
}

public void addKeyListener(KeyListener listener) {
    frame.addKeyListener(listener);
}

public void setLogText(String str) {
    log.setText(log.getText() + str + "\n");
}

public void setLocation(int x, int y) {
    frame.setLocation(x, y);
}

public void setLocationRelativeTo(Component c) {
    frame.setLocationRelativeTo(c);
}

int index = 0;
public void slowPrint(final String text) {
    frame.slowPrint(text, log);
}

public void slowPrintAndClear(final String text, boolean andQuit) {
    frame.slowPrintAndClear(text, log, andQuit);
}

public Terminal() {
    try {

        JScrollPane pane = new JScrollPane();
        JScrollBar scrollBar = pane.getVerticalScrollBar();

        scrollBar.setUI(new ScrollBarUI());
        pane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        pane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        pane.setViewportView(log);

        frame.add(field);
        frame.add(pane);            

        log.setBackground(Color.BLACK);
        log.setForeground(Color.WHITE);
        log.setWrapStyleWord(true);
        log.setLineWrap(true);
        pane.setBounds(4, 20 + 4, frame.getWidth() - 8, frame.getHeight() - 50);
        pane.setBorder(null);
        log.setEditable(false);
        log.setCaretColor(Color.BLACK);

        field.setBackground(Color.BLACK);
        field.setForeground(Color.WHITE);
        field.setBounds(2, frame.getHeight() - 23, frame.getWidth() - 5, 20);
        field.setHighlighter(null);
        field.setCaretColor(Color.BLACK);
        field.addKeyListener(this);
        field.setText("  >  ");

    } catch (FontFormatException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

private void dumpToLog() {
    log.setText(log.getText() + field.getText() + "\n");
    field.setText("  >  ");
}

public void setVariables(Types type) {
    switch(type) {
        case TERMINAL:
            this.type = Types.TERMINAL;
            break;
        case LOGINTERMINAL:
            this.type = Types.LOGINTERMINAL;
            break;
        default:
            this.type = Types.TERMINAL;
            break;
    }
}

Types type;
public void keyPressed(KeyEvent e) {
    int i = e.getKeyCode();

    String text1 = "  >  ";
    String text2 = field.getText().replaceFirst(text1, "");
    String text2_1 = text2.trim();
    String text = text1 + text2_1;

    if (type == Types.TERMINAL) {

    } else if (type == Types.LOGINTERMINAL) {
        if(i == KeyEvent.VK_ENTER && field.isFocusOwner()) {
            if(text.startsWith("  >  register") || text.startsWith("  >  REGISTER")) {
                if(!(text.length() == 13)) {
                    dumpToLog();
                    slowPrint("Registry not available at this current given time.\n");
                    //TODO: Create registry system.
                    new Notification("test");
                } else {
                    dumpToLog();
                    slowPrint("\nInformation:\n"
                            + "Registers a new account.\n\n"
                            + "Usage:\n"
                            + "register <username>\n");
                }
            } else {
                System.out.println("start |" + text + "| end");
                dumpToLog();
                slowPrint("Unknown command.\n");
            }
        }
    } else {
        // SETUP CODE FOR NOTIFICATION ERROR AGAIN
    }

    if(field.isFocusOwner() && i == KeyEvent.VK_LEFT || i == KeyEvent.VK_RIGHT) {
        e.consume();
    }

    if(!field.getText().startsWith("  >  ")) {
        field.setText("  >  ");
    }
}

public void keyReleased(KeyEvent e) {}
public void keyTyped(KeyEvent e) {}

}

Notification.java

public class Notification {

static CustomFrame frame = new CustomFrame(Types.NOTIFICATION);
JTextArea display = new JTextArea();

public Notification(String notification) {
    try {

        frame.setLocationRelativeTo(null);
        frame.add(display);

        display.setBackground(Color.BLACK);
        display.setForeground(Color.WHITE);
        display.setWrapStyleWord(true);
        display.setLineWrap(true);
        display.setBounds(4, 20 + 4, frame.getWidth() - 8, frame.getHeight() - 50);
        display.setBorder(null);
        display.setEditable(false);
        display.setCaretColor(Color.BLACK);

        frame.slowPrint(notification, display);
        frame.setVisible(true);
    } catch (FontFormatException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

}

Types.java

public enum Types {
    TERMINAL, LOGINTERMINAL,
    NOTIFICATION;
}

ScrollBarUI.java

public class ScrollBarUI extends MetalScrollBarUI {

private Image thumb, track;

private JButton blankButton() {
    JButton b = new JButton();
    b.setPreferredSize(new Dimension(0, 0));
    b.setMaximumSize(new Dimension(0, 0));
    b.setMinimumSize(new Dimension(0, 0));
    return b;
}

public ScrollBarUI() {
    thumb = FauxImage.create(32, 32, true);
    track = FauxImage.create(32, 32, false);
}

protected void paintThumb(Graphics g, JComponent component, Rectangle rectangle) {
    Graphics2D g2d = (Graphics2D) g;
    g.setColor(Color.BLUE);
    g2d.drawImage(thumb, rectangle.x, rectangle.y, rectangle.width, rectangle.height, null);
    g2d.setPaint(Color.WHITE);
    g2d.drawRect(rectangle.x, rectangle.y, rectangle.width - 1, rectangle.height-1);
}

protected void paintTrack(Graphics g, JComponent component, Rectangle rectangle) {
    ((Graphics2D) g).drawImage(track, rectangle.x, rectangle.y, rectangle.width, rectangle.height, null);
}

protected JButton createIncreaseButton(int orientation) {
    return blankButton();
}

protected JButton createDecreaseButton(int orientation) {
    return blankButton();
}

private static class FauxImage {
    static public Image create(int width, int height, boolean thumb) {
        BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d = bi.createGraphics();

        if (thumb == true) {
            Color LIGHT_BLUE = new Color(0, 140, 255);
            //g2d.setPaint(Color.BLUE);

            GradientPaint topFill = new GradientPaint(5, 25, Color.BLUE, 2, 2, LIGHT_BLUE);
            g2d.setPaint(topFill);
            g2d.fillRect(0, 0, width, height);

            g2d.dispose();
        } else {
            g2d.setPaint(Color.BLACK);
            g2d.fillRect(0, 0, width, height);

            g2d.dispose();
        }

        return bi;
    }
}

}

但是,如果有人能够帮助我完成如此庞大的职位,那么我将非常感激。

干杯和谢谢... ALOT。

修改

是否有时间修复字体。非常抱歉,现在已经完成了。

修改

这是调用通知框架的地方,但最终没有显示:

if(!(text.length() == 13)) {
    dumpToLog();
    slowPrint("Registry not available at this current given time.\n");
    //TODO: Create registry system.
    new Notification("test");
}

1 个答案:

答案 0 :(得分:1)

enter image description here

正如@Andrew Thompson所说,@ trashgod指出,使用多个帧是不好的做法。

如果你仍然需要解决问题,请点击:

问题在于您的游戏应用程序的static CustomFrame实例,然后使用setUndecorated(...)等方法修改该框架实例。

Terminal课程中,您有

static CustomFrame frame = new CustomFrame(Types.TERMINAL);

在您的Notification课程中,您有

static CustomFrame frame = new CustomFrame(Types.NOTIFICATION);

但您获得了frame

的相同实例
static JFrame frame = new JFrame(); (in your CustomFrame class)

这意味着什么:

加载游戏应用程序时,终端可见。当您注册用户时,您正在使用修改后的框架尺寸显示Notification,然后调用setVisible()的{​​{1}}方法。

导致问题的原因。为同一个静态实例调用CustomFramesetUndecorated()您无法修改可见的框架。意思是,您只能在可见之前修改框架。在这里,setVisible()已经可见(对于终端),当显示frame时,您正试图更改大小和显示。哪个错了。

正如你所说我想要不同的JFrame,就像在我的代码中一样,我使用Types.java作为枚举来选择我不同类型的帧。由于使用了不同的组件和大小调整,每次帧都完全不同,为实现这一点,每种类型的帧需要多个实例。

更改/修复代码:

<强> Game1.java

Notification

<强> CustomFrame1.java

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;

public class Game1 implements KeyListener
{

    int BACK_WIDTH = java.awt.Toolkit.getDefaultToolkit().getScreenSize().width;
    int BACK_HEIGHT = java.awt.Toolkit.getDefaultToolkit().getScreenSize().height;

    JFrame back_frame = new JFrame();
    JPanel window = new JPanel();
    JLabel title = new JLabel("Title");

    Terminal1 login = new Terminal1();

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

    public Game1()
    {
        try
        {

            back_frame.setSize(BACK_WIDTH, BACK_HEIGHT);
            back_frame.setLocation(0, 0);
            back_frame.getContentPane().setBackground(Color.BLACK);
            back_frame.setUndecorated(true);
            back_frame.setVisible(true);
            back_frame.add(window);
            window.setBackground(Color.BLACK);
            window.setLayout(null);

            window.add(title);
            title.setBounds((BACK_WIDTH / 2) - (550 / 2), (BACK_HEIGHT / 2) - (50 / 2), 550, 50);
            title.setForeground(Color.WHITE);

            back_frame.addKeyListener(this);
            login.addKeyListener(this);
            login.setLocationRelativeTo(null);
            login.setVariables(Types.LOGINTERMINAL);

            waitForStart();

        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    int index;

    public void waitForStart()
    {
        Timer timer = new Timer(2000, new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                if (index < 1 && index >= 0)
                {
                    index++;
                }
                else
                {
                    ((Timer) e.getSource()).stop();

                    login.setVisible(true);
                    login.slowPrint("Please login to continue...\n" + "Type 'help' for more information.\n");
                }
            }
        });
        timer.start();
    }

    public void keyPressed(KeyEvent e)
    {
        int i = e.getKeyCode();

        if (i == KeyEvent.VK_ESCAPE)
        {
            System.exit(0);
        }
    }

    public void keyReleased(KeyEvent e)
    {
    }

    public void keyTyped(KeyEvent e)
    {
    }

}

<强> Terminal1.java

import java.awt.Color;
import java.awt.Component;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionAdapter;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.Timer;

public class CustomFrame1 implements MouseListener
{

    JFrame frame = new JFrame();
    public static Paint window = null;

    public void addKeyListener(KeyListener listener)
    {
        frame.addKeyListener(listener);
    }

    private Point initialClick;
    private boolean inBounds = false;

    public int getWidth()
    {
        return frame.getWidth();
    }

    public int getHeight()
    {
        return frame.getHeight();
    }

    public void add(JComponent component)
    {
        window.add(component);
    }

    public void setLocation(int x, int y)
    {
        frame.setLocation(x, y);
    }

    public void setLocationRelativeTo(Component c)
    {
        frame.setLocationRelativeTo(c);
    }

    private void setFrameType(Types type)
    {
        switch (type)
        {
        case TERMINAL:
            frame.setSize(600, 400);
            break;
        case LOGINTERMINAL:
            frame.setSize(600, 400);
            break;
        case NOTIFICATION:
            frame.setSize(300, 150);
            break;
        default:
            frame.setSize(600, 400);
            break;
        }
    }

    int index = 0;

    public void slowPrint(final String text, final JTextArea field)
    {
        Timer timer = new Timer(40, new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                if (index < text.length() && index >= 0)
                {
                    String newChar = Character.toString(text.charAt(index));
                    field.append(newChar);
                    index++;
                }
                else
                {
                    field.append("\n");

                    index = 0;
                    ((Timer) e.getSource()).stop();
                }
            }
        });
        timer.start();
    }

    public void slowPrintAndClear(final String text, final JTextArea field, final boolean andQuit)
    {
        Timer timer = new Timer(40, new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                if (index < text.length() && index >= 0)
                {
                    String newChar = Character.toString(text.charAt(index));
                    field.append(newChar);
                    index++;
                }
                else
                {
                    field.append("\n");

                    if (andQuit == false)
                    {
                        field.setText(null);
                    }
                    else
                    {
                        System.exit(0);
                    }

                    index = 0;
                    ((Timer) e.getSource()).stop();
                }
            }
        });
        timer.start();
    }

    public CustomFrame1(Types type)
    {

        window = new Paint(frame);
        frame.setAlwaysOnTop(true);
        frame.addMouseListener(this);
        frame.setResizable(false);
        frame.setUndecorated(true);
        setFrameType(type);
        frame.add(window);
        window.setLayout(null);

        frame.addMouseListener(new MouseAdapter()
        {
            public void mousePressed(MouseEvent e)
            {
                initialClick = e.getPoint();
                frame.getComponentAt(initialClick);
            }
        });

        frame.addMouseMotionListener(new MouseMotionAdapter()
        {
            public void mouseDragged(MouseEvent e)
            {
                if (e.getX() >= 0 && e.getX() <= frame.getWidth() && e.getY() >= 0 && e.getY() <= 20)
                {
                    inBounds = true;
                }
                if (inBounds == true)
                {
                    int thisX = frame.getLocation().x;
                    int thisY = frame.getLocation().y;
                    int xMoved = (thisX + e.getX()) - (thisX + initialClick.x);
                    int yMoved = (thisY + e.getY()) - (thisY + initialClick.y);
                    int x = thisX + xMoved;
                    int y = thisY + yMoved;
                    frame.setLocation(x, y);
                }
            }
        });
    }

    public void dispose()
    {
        frame.dispose();
    }

    public JFrame setVisible(boolean bool)
    {
        frame.setVisible(bool);
        return null;
    }

    public void mouseClicked(MouseEvent e)
    {
    }

    public void mouseEntered(MouseEvent e)
    {
    }

    public void mouseExited(MouseEvent e)
    {
    }

    public void mousePressed(MouseEvent e)
    {
        int x = e.getX();
        int y = e.getY();

        if (x >= frame.getWidth() - 20 && x <= frame.getWidth() - 6 && y >= 3 && y <= 14)
        {
            frame.dispose();
        }

    }

    public void mouseReleased(MouseEvent e)
    {
        inBounds = false;
    }

}

class Paint extends JPanel
{
    private static final long serialVersionUID = 1L;

    private JFrame frame;

    public Paint(JFrame frame)
    {
        this.frame = frame;
    }

    private void doDrawing(Graphics g)
    {

        Graphics2D g2d = (Graphics2D) g;

        g2d.setColor(Color.BLACK);
        g2d.fillRect(0, 0, frame.getWidth(), frame.getHeight());

        Color LIGHT_BLUE = new Color(36, 171, 255);

        // g2d.setColor(Color.BLUE);
        GradientPaint topFill = new GradientPaint(0, 0, LIGHT_BLUE, frame.getWidth(), 20, Color.BLUE);
        g2d.setPaint(topFill);

        g2d.fillRect(0, 0, frame.getWidth(), 20);

        g2d.setColor(Color.WHITE);
        g2d.drawRect(0, 0, frame.getWidth() - 1, frame.getHeight() - 1);
        g2d.drawLine(0, 20, frame.getWidth(), 20);

        g2d.fillRect(frame.getWidth() - 20, 3, 14, 14);

    }

    public void paintComponent(Graphics g)
    {

        super.paintComponent(g);
        doDrawing(g);
    }
}

<强> Notification1.java

import java.awt.Color;
import java.awt.Component;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;

public class Terminal1 implements KeyListener
{

    static CustomFrame1 frame = new CustomFrame1(Types.TERMINAL);

    JTextArea log = new JTextArea();
    JTextField field = new JTextField();

    public void setVisible(boolean bool)
    {
        frame.setVisible(bool);
    }

    public void addKeyListener(KeyListener listener)
    {
        frame.addKeyListener(listener);
    }

    public void setLogText(String str)
    {
        log.setText(log.getText() + str + "\n");
    }

    public void setLocation(int x, int y)
    {
        frame.setLocation(x, y);
    }

    public void setLocationRelativeTo(Component c)
    {
        frame.setLocationRelativeTo(c);
    }

    int index = 0;

    public void slowPrint(final String text)
    {
        frame.slowPrint(text, log);
    }

    public void slowPrintAndClear(final String text, boolean andQuit)
    {
        frame.slowPrintAndClear(text, log, andQuit);
    }

    public Terminal1()
    {
        try
        {

            JScrollPane pane = new JScrollPane();
            JScrollBar scrollBar = pane.getVerticalScrollBar();

            scrollBar.setUI(new ScrollBarUI());
            pane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
            pane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
            pane.setViewportView(log);

            frame.add(field);
            frame.add(pane);

            log.setBackground(Color.BLACK);
            log.setForeground(Color.WHITE);
            log.setWrapStyleWord(true);
            log.setLineWrap(true);
            pane.setBounds(4, 20 + 4, frame.getWidth() - 8, frame.getHeight() - 50);
            pane.setBorder(null);
            log.setEditable(false);
            log.setCaretColor(Color.BLACK);

            field.setBackground(Color.BLACK);
            field.setForeground(Color.WHITE);
            field.setBounds(2, frame.getHeight() - 23, frame.getWidth() - 5, 20);
            field.setHighlighter(null);
            field.setCaretColor(Color.BLACK);
            field.addKeyListener(this);
            field.setText("  >  ");

        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    private void dumpToLog()
    {
        log.setText(log.getText() + field.getText() + "\n");
        field.setText("  >  ");
    }

    public void setVariables(Types type)
    {
        switch (type)
        {
        case TERMINAL:
            this.type = Types.TERMINAL;
            break;
        case LOGINTERMINAL:
            this.type = Types.LOGINTERMINAL;
            break;
        default:
            this.type = Types.TERMINAL;
            break;
        }
    }

    Types type;

    public void keyPressed(KeyEvent e)
    {
        int i = e.getKeyCode();

        String text1 = "  >  ";
        String text2 = field.getText().replaceFirst(text1, "");
        String text2_1 = text2.trim();
        String text = text1 + text2_1;

        if (type == Types.TERMINAL)
        {

        }
        else if (type == Types.LOGINTERMINAL)
        {
            if (i == KeyEvent.VK_ENTER && field.isFocusOwner())
            {
                if (text.startsWith("  >  register") || text.startsWith("  >  REGISTER"))
                {
                    if (!(text.length() == 13))
                    {
                        dumpToLog();
                        slowPrint("Registry not available at this current given time.\n");
                        // TODO: Create registry system.
                        new Notification1("test");
                    }
                    else
                    {
                        dumpToLog();
                        slowPrint("\nInformation:\n" + "Registers a new account.\n\n" + "Usage:\n" + "register <username>\n");
                    }
                }
                else
                {
                    System.out.println("start |" + text + "| end");
                    dumpToLog();
                    slowPrint("Unknown command.\n");
                }
            }
        }
        else
        {
            // SETUP CODE FOR NOTIFICATION ERROR AGAIN
        }

        if (field.isFocusOwner() && i == KeyEvent.VK_LEFT || i == KeyEvent.VK_RIGHT)
        {
            e.consume();
        }

        if (!field.getText().startsWith("  >  "))
        {
            field.setText("  >  ");
        }
    }

    public void keyReleased(KeyEvent e)
    {
    }

    public void keyTyped(KeyEvent e)
    {
    }

}