1

sigh OK guys... There is going to be a painstaking amount of code here, but i'm going to do it anyway.
So basically, I have a custom made (well it's actually just a HEAVILY customized version of a JFrame) and am having major issues.

I have a background. (Fair enough, that's fine) THEN I have a Terminal frame that pops up and spits stuff out. This Terminal frame is based off another class named CustomFrame. I also have ANOTHER class called Notification, which is ALSO a frame class like Terminal ALSO based off Custom Frame.

In the beginning, background loads fine. Terminal loads fine. Calls method to show Notification window. And thats where the problem rises. The notification window won't show.

I have tried frame.setVisible(); frame.setSize(); frame.setLocation(); I have tried, EVERYTHING.

And if I don't show Terminal at all, it seems to spit it's code onto Notification instead, almost like there can only be ONE instance of the CustomFrame open AT ALL TIMES.

I hope you understand my problems... So here is the code!

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;
    }
}

}

On a serious note though, if anyone is able to help me with such a sizeable post, I will SERIOUSLY be eternally grateful.

Cheers and thankyou... ALOT.

Edit:

Did have the time to fix up fonts. Extremely sorry, now it has been done.

Edit:

Here is where the Notification frame is called and doesn't end up showing:

if(!(text.length() == 13)) {
    dumpToLog();
    slowPrint("Registry not available at this current given time.\n");
    //TODO: Create registry system.
    new Notification("test");
}
  • by using KeyBindings shouldn't be reason for this question (half or more), override BasicScrollBarUI instead of MetalScrollBarUI (KeyBindigs are part of XxxXxxUI), whats wrong with linked code by @trashgod – mKorbel Nov 13 '14 at 09:32
  • 2
    *"I have put custom fonts in there. Just change them to default ones and your good to go."* If *you* could not be bothered doing it, why should *we?* For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example). – Andrew Thompson Nov 13 '14 at 09:39
  • 1
    *"..almost like there can only be ONE instance of the CustomFrame open AT ALL TIMES."* There **should** never be more than one frame. See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) – Andrew Thompson Nov 13 '14 at 09:40
  • 1
    override getpreferredSize instead of referencing to something static (nor to get Size from JFrame), remove all static declarations, should be private – mKorbel Nov 13 '14 at 09:46
  • I have updated the fonts section as I didn't have the time earlier. (Had very important duties to attend to. xD) On a more serious note, its done. –  Nov 13 '14 at 10:10
  • mKorbel, KeyBindings are only vital due to the fact that they create the frame. And the problem is, is that by USING the KeyListener, it should create a JFrame. It did not. –  Nov 13 '14 at 10:11
  • Andrew Thompson, was busy, sorry about the fonts. All done. And with the multiple JFrames, I have read that topic 5+ times already. Basically, I want DIFFERENT JFrames, as in my code, I use Types.java as an enum to pick my different TYPES of frames. The frame is completely different each time due to the use of different components and sizing. I understand exactly what your saying though. –  Nov 13 '14 at 10:13
  • mKorbel, again. I don't understand what you meen by 'override getPreferredSize' Can you please make it a little clearer? Also, just a side-note, what difference will it make? –  Nov 13 '14 at 10:14
  • Wow. I am really tired, and really sloppy at the moment. I added another edit to show where I call Notification so you can locate the error... I also cleaned up the code alot more. –  Nov 13 '14 at 10:22
  • 2
    @user3636058: Instead of multiple frames, a fundamentally [flawed](http://stackoverflow.com/q/6309407/230513) approach, consider multiple panels in a `CardLayout`, for [example](http://stackoverflow.com/a/5655843/230513), and/or modeless dialogs, for [example](http://stackoverflow.com/a/11832979/230513). Also, sleep. – trashgod Nov 13 '14 at 10:59

1 Answers1

1

enter image description here

As @Andrew Thompson, @trashgod pointed, it is a bad practice to use multiple frames.

If you still need to fix your problem, here goes:

The issue is with your static instance of the CustomFrame for your Game application and then modifying that frame instance using methods like setUndecorated(...).

In your Terminal class, you have

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

and in your Notification class, you have

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

but you are getting the same instance of the frame

static JFrame frame = new JFrame(); (in your CustomFrame class)

So what this means :

When the Game application loads, the Terminal is visible. And when you register a user, you are displying a Notification, with modified frame size and then by calling the setVisible() method of the CustomFrame.

Which is causing the issue. The setUndecorated() and setVisible() is invoked for the same static instance. YOU CANNOT MODIFY A FRAME WHICH IS VISIBLE. Meaning, YOU CAN ONLY MODIFY A FRAME BEFORE IT IS VISIBLE. Here your frame is already visible (for Terminal) and when displaying the Notification you are trying to change the size and display. WHICH IS WRONG.

As you said I want DIFFERENT JFrames, as in my code, I use Types.java as an enum to pick my different TYPES of frames. The frame is completely different each time due to the use of different components and sizing, to achieve this, you need multiple instances for each type of frame.

Changes/Fixes to your code :

Game1.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)
    {
    }

}

CustomFrame1.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);
    }
}

Terminal1.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)
    {
    }

}

Notification1.java

import java.awt.Color;

import javax.swing.JTextArea;

public class Notification1
{

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

    public Notification1(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 (Exception e)
        {
            e.printStackTrace();
        }
    }

}
always_a_rookie
  • 4,515
  • 1
  • 25
  • 46
  • Thankyou VERY much. I understand the bad practises of multiple Frames but what I'm trying to achieve with this 'notification' is almost my own version of JOptionPane. I use that Terminal as the main one and the Notification for... well... notifications. Nothing else. THANKYOU again. –  Nov 13 '14 at 21:23
  • 1
    If you are trying to use the `Notification` as a popup window, It is better to use it as a `JDialog`. Because as per the documentation, **All dialogs are modal. Each showXxxDialog method blocks the caller until the user's interaction is complete.** – always_a_rookie Nov 13 '14 at 21:36
  • True. But then I have the slowPrint implemented and other features that I may add soon. xD Thankyou again! –  Nov 14 '14 at 05:10