I'm somewhat new to Java, but I'm not new to programming as I know C, C++, and some Objective-C(the syntax is a bit too weird when you deal with classes).
As my first program done in Java, I decided to program a GUI Calculator that can first do the simple stuff such as your arithmetics to complex functions such as your trig functions, logarithms, and squares to functions that can cover a concept(this one I'm still thinking about).
Anyway, I have two problems that seem very weird.
Problem #1: While this part isn't very critical, I decided to allow the user to customize the text color of the buttons. I stored a Color variable that colored the Foreground of the Buttons. However, after selecting a color, I tried to change the color of a button and it gives me a NullPointerException error. The error occurs at the line
. This section means that it goes to the Control class(which I didn't post), then from the Control class to the Interface class(the main window and also another one that I didn't post), finds the JButton Digit1, and then changes the color of the text. The weird part is that if I create a constructor that goes to the Interface class, it gives me a NullPointerException error. Its even weirder that even trying to change a boolean value from true to false from the Preferences_Control to the Control class also gives me a NullPointerException error. The only time I can change a variable value without getting a NullPointerException is if I'm referring to the ColorSliders class.
My code from the Preferences_Control class:
Problem #2: When I change the variable color to something else and when I re-enter the ColorSliders window, the variable tends to always revert back to the default settings that is declared from the start of the program, even though I ran tests from the Preferences_Control and it shows that the color did change.
The Code from ColorSliders class(I didn't code this section, but I did add to it):
NOTE: The class Interface(which is not posted) contains the main() argument.
As my first program done in Java, I decided to program a GUI Calculator that can first do the simple stuff such as your arithmetics to complex functions such as your trig functions, logarithms, and squares to functions that can cover a concept(this one I'm still thinking about).
Anyway, I have two problems that seem very weird.
Problem #1: While this part isn't very critical, I decided to allow the user to customize the text color of the buttons. I stored a Color variable that colored the Foreground of the Buttons. However, after selecting a color, I tried to change the color of a button and it gives me a NullPointerException error. The error occurs at the line
Code:
control.gui.Digit1.setForeground(defaultColor)
My code from the Preferences_Control class:
Code:
import java.awt.Color;
public class Preferences_Control implements ActionListener {
Interface_Preferences gui;
public Control control;
ColorSliders color;
//The variables for this window
Color defaultColor = new Color(0,0,0); //For default
Color arithColor = new Color(255,0,0); //For arithmetic
Color functColor = new Color(0,0,255); //For Functions
//Declares the function
public Preferences_Control(Interface_Preferences in)
{
gui = in;
}
public Preferences_Control(Control in2)
{
control = in2;
}
public Preferences_Control(ColorSliders in3)
{
color=in3;
}
public void actionPerformed(ActionEvent input)
{
Object current = input.getSource();
if(current==gui.defaultColoring)
{
System.out.println(defaultColor);
ColorSliders color = new ColorSliders();
color.colorType=0;
}
if(current==gui.arithColoring)
{
System.out.println(arithColor);
ColorSliders color = new ColorSliders();
color.colorType=1;
}
}
//This only works with the default color and can only be called from ColorSliders class
public void changeDefaultColor()
{
Color newColor = defaultColor;
//NOTE: control.gui.(...)-sends to the Interface control while gui.(..) goes to Interface Preferences
System.out.println("Changing the default color to " + newColor);
control.gui.Digit1.setForeground(newColor);
System.out.println("Done!");
}
public void changeArithmeticColor(Color newColor)
{
System.out.println(arithColor);
arithColor=newColor;
gui.paint();
System.out.println(arithColor);
}
}
Problem #2: When I change the variable color to something else and when I re-enter the ColorSliders window, the variable tends to always revert back to the default settings that is declared from the start of the program, even though I ran tests from the Preferences_Control and it shows that the color did change.
The Code from ColorSliders class(I didn't code this section, but I did add to it):
Code:
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class ColorSliders extends JFrame implements ChangeListener, ActionListener {
ColorPanel canvas = new ColorPanel();
Preferences_Control control = new Preferences_Control(this);
JSlider red = new JSlider(0,255,255);
JSlider green = new JSlider(0,255,0);
JSlider blue = new JSlider(0,255,0);
JButton done = new JButton("Done");
Color newColor = new Color(0,0,0);
int colorType=0;
public ColorSliders()
{
super("Color Slide");
setSize(270,300);
setUndecorated(true);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setVisible(true);
red.setMajorTickSpacing(50);
red.setMinorTickSpacing(10);
red.setPaintTicks(true);
red.setPaintLabels(true);
red.addChangeListener(this);
green.setMajorTickSpacing(50);
green.setMinorTickSpacing(10);
green.setPaintTicks(true);
green.setPaintLabels(true);
green.addChangeListener(this);
blue.setMajorTickSpacing(50);
blue.setMinorTickSpacing(10);
blue.setPaintTicks(true);
blue.setPaintLabels(true);
blue.addChangeListener(this);
JLabel redLabel = new JLabel("Red: ");
JLabel greenLabel = new JLabel("Green: ");
JLabel blueLabel = new JLabel("Blue: ");
GridLayout grid = new GridLayout(5,1);
FlowLayout right = new FlowLayout(FlowLayout.RIGHT);
setLayout(grid);
JPanel redPanel = new JPanel();
redPanel.setLayout(right);
redPanel.add(redLabel);
redPanel.add(red);
add(redPanel);
JPanel greenPanel = new JPanel();
greenPanel.setLayout(right);
greenPanel.add(greenLabel);
greenPanel.add(green);
add(greenPanel);
JPanel bluePanel = new JPanel();
bluePanel.setLayout(right);
bluePanel.add(blueLabel);
bluePanel.add(blue);
add(bluePanel);
add(canvas);
JPanel donePanel = new JPanel();
donePanel.setLayout(right);
donePanel.add(done);
add(donePanel);
done.addActionListener(this);
setVisible(true);
}
public void stateChanged(ChangeEvent event)
{
JSlider source = (JSlider) event.getSource();
if(source.getValueIsAdjusting() != true)
{
Color current = new Color(red.getValue(),green.getValue(),blue.getValue());
canvas.changeColor(current);
System.out.println(colorType);
//For default color
switch(colorType)
{
case 0:
{
System.out.println("The old color was " + control.defaultColor);
control.defaultColor = current;
System.out.println("The new color is " + control.defaultColor);
break;
}
case 1:
{
System.out.println("The old color was " + control.arithColor);
newColor = current;
System.out.println("The new color is " + newColor);
break;
}
}
canvas.repaint();
}
}
public Insets getInsets()
{
Insets border = new Insets(45,10,10,10);
return border;
}
public void actionPerformed(ActionEvent event)
{
String command = event.getActionCommand();
if(command.equals("Done"))
{
System.out.println("Invoked!");
if(colorType==0)
{
control.changeDefaultColor(); //Now goes to Preferences_Control and Preferences_Control will deal with the new color
}
if(colorType==1)
{
control.changeArithmeticColor(newColor);
System.out.println("Arithmetic color sucessful");
}
setVisible(false);
}
}
}
class ColorPanel extends JPanel
{
Color background;
ColorPanel()
{
background = Color.red;
}
public void paintComponent(Graphics comp)
{
Graphics2D comp2D = (Graphics2D) comp;
comp2D.setColor(background);
comp2D.fillRect(0,0,getSize().width,getSize().height);
}
void changeColor(Color newBackground)
{
background = newBackground;
}
}
NOTE: The class Interface(which is not posted) contains the main() argument.