MainGUI.java Source code

Java tutorial

Introduction

Here is the source code for MainGUI.java

Source

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import org.json.simple.parser.ParseException;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author He
 */
public class MainGUI extends javax.swing.JFrame {

    private static final long serialVersionUID = 8064285163713571914L;

    private static final int MAX_NUMBER_QUESTIONS = 20;

    private boolean modifiable = true;
    private int questionHandling = 0;
    private int difficulty = 0;
    private OptionsGUI opt = new OptionsGUI(this);
    private String username;
    UserHistoryGUI stats = null;

    /**
     * Creates new form MainGUI
     */
    public MainGUI(String username) {
        initComponents();
        this.username = username;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        bkgPanel = bkgPanel = new BKGPanel();

        jButton1.setText("jButton1");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        bkgPanel.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseMoved(java.awt.event.MouseEvent evt) {
                bkgPanelMouseMoved(evt);
            }
        });
        bkgPanel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                bkgPanelMouseClicked(evt);
            }

            public void mousePressed(java.awt.event.MouseEvent evt) {
                bkgPanelMousePressed(evt);
            }
        });

        javax.swing.GroupLayout bkgPanelLayout = new javax.swing.GroupLayout(bkgPanel);
        bkgPanel.setLayout(bkgPanelLayout);
        bkgPanelLayout.setHorizontalGroup(bkgPanelLayout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 656, Short.MAX_VALUE));
        bkgPanelLayout.setVerticalGroup(bkgPanelLayout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 454, Short.MAX_VALUE));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                        .addComponent(bkgPanel, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(
                bkgPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
                Short.MAX_VALUE));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void bkgPanelMouseMoved(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_bkgPanelMouseMoved
        // TODO add your handling code here:
    }//GEN-LAST:event_bkgPanelMouseMoved

    private void bkgPanelMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_bkgPanelMousePressed
        // TODO add your handling code here:
    }//GEN-LAST:event_bkgPanelMousePressed

    private void bkgPanelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_bkgPanelMouseClicked
        for (CButton c : ((BKGPanel) bkgPanel).getButtons()) {
            if (c.isContained(evt.getX(), evt.getY())) {
                if (c.text.equals("Play")) {
                    final MainGUI main = this;
                    SwingUtilities.invokeLater(new Runnable() {
                        @Override
                        public void run() {
                            QuestionGUI gui = new QuestionGUI(main,
                                    Question.generateQuestionSet(main.getDifficulty(), 20), username);
                            gui.setVisible(true);
                        }
                    });
                } else if (c.text.equals("Check Stats")) {
                    try {
                        if (stats != null && stats.isValid()) {
                            return;
                        }
                        stats = new UserHistoryGUI(username);
                        stats.setVisible(true);
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                } else if (c.text.equals("Options")) {
                    opt.setVisible(true);
                } else if (c.text.equals("Facebook Connect")) {

                }
            }
        }
    }//GEN-LAST:event_bkgPanelMouseClicked

    public boolean isModifiable() {
        return modifiable;
    }

    public void setDifficulty(int d) {
        difficulty = d;
    }

    public void setQuestionHandling(int i) {
        questionHandling = i;
    }

    public int getDifficulty() {
        return difficulty;
    }

    public int getQuestionHandling() {
        return questionHandling;
    }

    public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                MainGUI gui = new MainGUI("");
                gui.setVisible(true);
            }
        });
    }

    private static class CButton {

        private final int HORIZONTAL_MARGIN = 4;
        private final int VERTICAL_MARGIN = 4;

        private int x, y, w, h;
        String text, icon;
        Color color = Color.ORANGE;
        BufferedImage image = null;
        JPanel jp;
        Font font;

        public CButton(JPanel jp, int x, int y, int w, int h, String text, String icon) {
            this.jp = jp;
            this.x = x;
            this.y = y;
            this.w = w;
            this.h = h;
            this.text = text;
            this.icon = icon;
            if (icon != null) {
                try {
                    image = ImageIO.read(ResourceLoader.load(icon));
                } catch (IOException io) {

                }
            }
        }

        public boolean isContained(int px, int py) {
            //image.getWidth() + 2 * HORIZONTAL_MARGIN

            return px >= x && px <= x + w + (image != null ? image.getWidth() + 2 * HORIZONTAL_MARGIN : 0)
                    && py >= y && py <= y + h;
        }

        public CButton(JPanel jp, int x, int y, int w, int h, String text) {
            this(jp, x, y, w, h, text, null);
        }

        public CButton(JPanel jp, int x, int y, int w, int h) {
            this(jp, x, y, w, h, "    ", null);
        }

        public CButton(JPanel jp, int x, int y, String icon) {
            this(jp, x, y, 0, 0, "", icon);
        }

        public void resizeImage(int nw, int nh) {
            if (image != null) {
                image = toBufferedImage(image.getScaledInstance(nw, nh, Image.SCALE_SMOOTH));
            }
        }

        private final float MIX_FACTOR = 0.5f;

        private static BufferedImage toBufferedImage(Image img) {
            if (img instanceof BufferedImage) {
                return (BufferedImage) img;
            }

            // Create a buffered image with transparency
            BufferedImage bimage = new BufferedImage(img.getWidth(null), img.getHeight(null),
                    BufferedImage.TYPE_INT_ARGB);

            // Draw the image on to the buffered image
            Graphics2D bGr = bimage.createGraphics();
            bGr.drawImage(img, 0, 0, null);
            bGr.dispose();

            // Return the buffered image
            return bimage;
        }

        private Color taintColor(Color base, Color influence) {
            return new Color(base.getRed() + ((int) ((influence.getRed() - base.getRed()) * MIX_FACTOR)),
                    base.getBlue() + ((int) ((influence.getGreen() - base.getGreen()) * MIX_FACTOR)),
                    base.getBlue() + ((int) ((influence.getBlue() - base.getBlue()) * MIX_FACTOR)));
        }

        public void drawButton(Graphics2D g, boolean mouseInBox) {

            int textw = 0;
            int texth = 0;
            int atextw = 0;
            if (font == null) {
                g.setFont(Font.getFont("Calibri"));
                font = g.getFont().deriveFont(22);
            }
            g.setFont(font);
            FontMetrics fontMetric = g.getFontMetrics();
            for (char c : text.toCharArray()) {
                textw += fontMetric.charWidth(c);
            }
            atextw = textw;
            texth = fontMetric.getHeight();
            textw = Math.max(textw + 2 * HORIZONTAL_MARGIN, w);
            texth = Math.max(texth + 2 * VERTICAL_MARGIN, h);
            if (image != null) {
                textw += image.getWidth() + 2 * HORIZONTAL_MARGIN;
                texth = Math.max(image.getHeight() + 2 * VERTICAL_MARGIN, texth);
            }
            Color fill = new Color(0, 0, 0, 130);
            if (mouseInBox) {
                fill = taintColor(fill, new Color(0, 255, 0, 50));
            }
            g.setColor(fill);
            g.fillRect(x, y, textw, texth);
            g.setColor(color);
            g.drawRect(x, y, textw, texth);
            h = texth;
            g.drawString(text, x + HORIZONTAL_MARGIN, y + VERTICAL_MARGIN + 18);
            if (image != null) {
                g.drawImage(image, x + textw - image.getWidth() - HORIZONTAL_MARGIN, y + VERTICAL_MARGIN - 1, jp);
            }
        }
    }

    private static class BKGPanel extends JPanel {

        public BKGPanel() {
            super();
            play = new CButton(this, 30, 30, 120, 0, "Play", "play.png");
            checkStats = new CButton(this, 30, 75, 120, 0, "Check Stats", "leaderboard.png");
            options = new CButton(this, 30, 120, 120, 0, "Options", "option.png");
            fbConnect = new CButton(this, 30, 165, 120, 0, "Facebook Connect", "fblogo.jpg");
            play.resizeImage(30, 30);
            checkStats.resizeImage(30, 30);
            options.resizeImage(30, 30);
            fbConnect.resizeImage(30, 30);

            Thread update = new Thread(new Runnable() {
                @Override
                public synchronized void run() {
                    while (true) {
                        try {
                            repaint();
                            wait(25);
                        } catch (InterruptedException e) {
                            break;
                        }
                    }
                }
            });
            update.setDaemon(true);
            update.start();

            try {
                logo = ImageIO.read(ResourceLoader.load("logo.png"));
            } catch (IOException e) {

            }
        }

        public CButton[] getButtons() {
            return new CButton[] { play, checkStats, options, fbConnect };
        }

        BufferedImage logo = null;

        private final int WAIT_THRESHOLD = 50;
        private final int[] orangeM = { 0, 0, 100, 100 };
        private Paint orangePaint = null;
        private long startMS = -1;

        private final float MIX_FACTOR = 0.5f;

        CButton play, checkStats, options, fbConnect;

        private void drawColorfulBox(Graphics2D g, int x, int y, int w, int h) {
            Color previousColor = g.getColor();
            Paint previousPaint = g.getPaint();
            g.setPaint(orangePaint);
            g.fillRect(x, y, w, h);
            g.setColor(Color.ORANGE);
            g.drawRect(x, y, w, h);
            g.setPaint(previousPaint);
            g.setColor(previousColor);
        }

        private Color taintColor(Color base, Color influence) {
            return new Color(base.getRed() + ((int) ((influence.getRed() - base.getRed()) * MIX_FACTOR)),
                    base.getBlue() + ((int) ((influence.getGreen() - base.getGreen()) * MIX_FACTOR)),
                    base.getBlue() + ((int) ((influence.getBlue() - base.getBlue()) * MIX_FACTOR)));
        }

        @Override
        public void paintComponent(Graphics graphics) {
            super.paintComponent(graphics);
            Graphics2D g = (Graphics2D) graphics;

            if (startMS == -1) {
                startMS = System.currentTimeMillis();
            } else if (System.currentTimeMillis() - startMS >= WAIT_THRESHOLD) {
                for (int i = 0; i < orangeM.length; i++) {
                    orangeM[i] += 2;
                    orangeM[i] %= 200;
                }
                startMS = System.currentTimeMillis();
            }
            orangePaint = new GradientPaint(orangeM[0], orangeM[1], Color.RED, orangeM[2], orangeM[3],
                    new Color(120, 0, 0), true);

            drawColorfulBox(g, 0, 0, 656 * 3 / 2, 454 * 3 / 2);
            if (logo != null) {
                g.drawImage(logo, 10, 216, this);
            }
            Point pm = getMousePosition();
            play.drawButton(g, pm == null ? false : play.isContained(pm.x, pm.y));
            checkStats.drawButton(g, pm == null ? false : checkStats.isContained(pm.x, pm.y));
            options.drawButton(g, pm == null ? false : options.isContained(pm.x, pm.y));
            fbConnect.drawButton(g, pm == null ? false : fbConnect.isContained(pm.x, pm.y));
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel bkgPanel;
    private javax.swing.JButton jButton1;
    // End of variables declaration//GEN-END:variables
}