1

So I'm trying to make an easter egg mode for a program. What I want it to do is if you click a button, it sets boolean eastermode to true. I tried making it final, public, private and a lot of different things, but I eventually settled on using getters and setters from another class. However, this didn't work. Here's my JConsole class (slightly changed to delete irrelevant parts to this question):

package main.java;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.PrintStream;
import java.util.List;
import java.util.Scanner;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextPane;
import javax.swing.SwingWorker;


public class JConsole {
    private KeyManager keyManager;
    private boolean running = false;
    private static EggManager eggmanager;

    public static void main(String[] args) throws IOException {
        JFrame frame = new JFrame("Identity Visual");
        JPanel mainPanel = new JPanel(new BorderLayout());
        JPanel topPnl = new JPanel();
        JPanel btnPnl = new JPanel(new FlowLayout(FlowLayout.CENTER));
        JPanel morePnl = new JPanel(new FlowLayout(FlowLayout.CENTER));
        JTextPane jta = new JTextPane();

        JButton values = new JButton("I Value..");
        JButton ideas = new JButton("I Believe..");
        JButton inside = new JButton("Me On The Inside");
        JButton outside = new JButton("Me On The Outside");
        JButton other = new JButton("other About Me");
        JButton influences = new JButton("How have the characteristics listed above influenced who you are as a person?");
        JButton easter = new JButton(“Easter eggs“);

        btnPnl.add(values);
        btnPnl.add(ideas);
        btnPnl.add(inside);
        btnPnl.add(outside);
        btnPnl.add(other);
        morePnl.add(influences);
        morePnl.add(easter);

        btnPnl.setBorder(BorderFactory.createLineBorder(Color.BLACK));

        mainPanel.add(topPnl, BorderLayout.NORTH);
        mainPanel.add(btnPnl, BorderLayout.NORTH);
        mainPanel.add(morePnl, BorderLayout.SOUTH);
        mainPanel.add(jta,BorderLayout.CENTER);
        frame.setLayout(new BorderLayout());
        values.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                new SwingWorker<Void, Object>(){
                    @Override
                    protected Void doInBackground() throws Exception {
                        outputTest(“values”);
                        return null;
                    }}.execute();
            }});
        ideas.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                new SwingWorker<Void, Object>(){
                    @Override
                    protected Void doInBackground() throws Exception {
                        outputTest(“ideas”);
                        return null;
                    }}.execute();
            }});
        inside.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                new SwingWorker<Void, Object>(){
                    @Override
                    protected Void doInBackground() throws Exception {
                        outputTest(“inside");
                        return null;
                    }}.execute();
            }});
        outside.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                new SwingWorker<Void, Object>(){
                    @Override
                    protected Void doInBackground() throws Exception {
                        outputTest("outside.");
                        return null;
                    }}.execute();
            }});
        other.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                new SwingWorker<Void, Object>(){
                    @Override
                    protected Void doInBackground() throws Exception {
                        outputTest("1. I like to create things");
                        Thread.sleep(1000);
                        outputTest("2. I really like computers");
                        Thread.sleep(1300);
                        outputTest("3. I love dogs");
                        Thread.sleep(1250);
                        outputTest("4. I like reading");
                        Thread.sleep(1250);
                        outputTest("5. I really like oranges.");
                        Thread.sleep(1340);
                        outputTest("other.");
                        return null;
                    }}.execute();
            }});
        influences.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                new SwingWorker<Void, Object>(){
                    @Override
                    protected Void doInBackground() throws Exception {
                        outputTest("INFULENCES#TEMP");
                        if (eggmanager.eastermode = true){
                            outputTest("INFULENCES#EASTER");
//Right here is where I want it to actually show INFULENCES#EASTER when clicked and eastermode = true.
                        }
                        return null;
                    }}.execute();
            }});
        easter.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                new SwingWorker<Void, Object>(){
                    @Override
                    protected Void doInBackground() throws Exception {
                        outputTest("Easter egg mode activated");
                        eggmanager.setEastermode(true);
                        return null;
                    }}.execute();
            }}

        );
        frame.add(mainPanel);
        frame.setSize(1000, 500);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        console(jta);
    }


    public static void outputTest(String msg){
       System.out.println(msg);
    }

    public static void console(final JTextPane area) throws IOException {
        area.setContentType("text/html");
        final PipedInputStream outPipe = new PipedInputStream();
        System.setOut(new PrintStream(new PipedOutputStream(outPipe), true));       
        new SwingWorker<Void, String>() {
            @Override
            protected Void doInBackground() throws Exception {
                @SuppressWarnings("resource")
                Scanner s = new Scanner(outPipe);
                while (s.hasNextLine()){
                    String line = s.nextLine();
                    publish(line + "\n");
                }
                return null;
            }

            @Override
            protected void process(List<String> chunks) {
                for (String line : chunks){
                    area.setText("<font size=\"10\" color=\"red\">"+line+"</font>");
                }
            }
        }.execute();
    }

    private void tick() {
        keyManager.tick();
    }
    public void run() {


        int fps = 60;
        double timePerTick = 1000000000 / fps;
        double delta = 0;
        long now;
        long lastTime = System.nanoTime();
        long timer = 0;
        int ticks = 0;
        while(running) {
            now = System.nanoTime();
            delta +=(now - lastTime) / timePerTick;
            timer += now - lastTime;
            lastTime = now;

            if(delta >= 1) {
            tick();

            ticks++;
            delta--;
            }
            if (timer >= 1000000000){
                System.out.println("fps: " + ticks);
                ticks = 0;
                timer = 0;
            }

        }


    }

}

And here's my EggManager (ignore key manager, not important to question): package main.java;

public class EggManager {
    public boolean eastermode = false;

    public boolean isEastermode() {
        return eastermode;
    }

    public void setEastermode(boolean eastermode) {
        this.eastermode = eastermode;
    }
}

If someone could help, that would be so great. Thanks.

minindo
  • 13
  • 4
  • 1
    This code should throw a NullPointerException. Please clarify if this is so -- as you never initialize your eggManager variable. – Hovercraft Full Of Eels Sep 25 '16 at 00:39
  • 1
    Also, for better help, consider creating and posting a [mcve] or [SSCCE](http://sscce.org), a small but complete program that only has necessary code to demonstrate your problem, that we can copy, paste, compile and run without modification. Please check the links and ask if anything in them confuses you. – Hovercraft Full Of Eels Sep 25 '16 at 00:41
  • Oops. sorry, i was kind of in a hurry. Wait, like this: `protected Void doInBackground() throws NullPointerException {`... – minindo Sep 25 '16 at 00:49
  • Also why are you over-complicating things by using SwingWorkers where none are called for. – Hovercraft Full Of Eels Sep 25 '16 at 00:51
  • Get rid of the SwingWorkers, don't check for NullPointerExceptions, and instead initialize your object that you're using. That simple. – Hovercraft Full Of Eels Sep 25 '16 at 00:51
  • Oh, thanks. Just noticed it was marked as a duplicate. I'll check the NullPointerException question. – minindo Sep 25 '16 at 00:51
  • OK. Will do. Sorry, i'm kind of a newbie at java – minindo Sep 25 '16 at 00:52

0 Answers0