Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Contrast Button #106

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/ant.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 37 additions & 0 deletions Simple-Java-Calculator.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module-library">
<library name="JUnit4">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/junit/junit/4.13.1/junit-4.13.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
<orderEntry type="module-library">
<library name="JUnit5.8.1">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter/5.8.1/junit-jupiter-5.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-api/5.8.1/junit-jupiter-api-5.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/opentest4j/opentest4j/1.2.0/opentest4j-1.2.0.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-commons/1.8.1/junit-platform-commons-1.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/apiguardian/apiguardian-api/1.1.2/apiguardian-api-1.1.2.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-params/5.8.1/junit-jupiter-params-5.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-engine/5.8.1/junit-jupiter-engine-5.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-engine/1.8.1/junit-platform-engine-1.8.1.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
</component>
</module>
208 changes: 189 additions & 19 deletions src/simplejavacalculator/UI.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,18 @@

package simplejavacalculator;

import java.awt.FlowLayout;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import java.awt.Font;
import javax.swing.Box;
import javax.swing.BoxLayout;

import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.ImageIcon;
import java.io.*;

public class UI implements ActionListener {
Expand All @@ -49,10 +47,11 @@ public class UI implements ActionListener {
private final JPanel panelSub8;

private final JTextArea text;
private boolean colourCounter;

private final JButton but[], butAdd, butMinus, butMultiply, butDivide,
butEqual, butCancel, butSquareRoot, butSquare, butOneDividedBy,
butCos, butSin, butTan, butxpowerofy, butlog, butrate, butabs, butBinary, butln;
butEqual, butCancel,butSquareRoot, butSquare, butOneDividedBy,
butCos, butSin, butTan, butxpowerofy, butlog, butrate, butabs, butBinary, butln,butContrast;
private final Calculator calc;

private final String[] buttonValue = {"0", "1", "2", "3", "4", "5", "6",
Expand All @@ -66,7 +65,7 @@ public class UI implements ActionListener {

public UI() throws IOException {
frame = new JFrame("Calculator PH");

imageReturn = new BufferedImageCustom();
image = new ImageIcon(imageReturn.imageReturn());

Expand All @@ -86,9 +85,9 @@ public UI() throws IOException {
text = new JTextArea(1, 30);

textFont = new Font("Consolas",Font.BOLD, 24);

but = new JButton[10];
for (int i = 0; i < 10; i++) {
//colourCounter = false;
but = new JButton[11];
for (int i = 0; i < 11; i++) {
but[i] = new JButton(String.valueOf(i));
}
butAdd = new JButton("+");
Expand All @@ -106,10 +105,10 @@ public UI() throws IOException {
butxpowerofy = new JButton("x^y");
butlog = new JButton("log10(x)");
butrate = new JButton("x%");
butabs = new JButton("abs(x)");
butCancel = new JButton("C");
butBinary = new JButton("Bin");

butabs = new JButton("abs(x)");
butCancel = new JButton("C");
butBinary = new JButton("Bin");
butContrast = new JButton("Contrast");
calc = new Calculator();

}
Expand All @@ -124,7 +123,7 @@ public void init() {
text.setFont(textFont);
text.setEditable(false);

for (int i = 0; i < 10; i++) {
for (int i = 0; i < 11; i++) {
but[i].setFont(font);
}
butAdd.setFont(font);
Expand All @@ -144,7 +143,8 @@ public void init() {
butrate.setFont(font);
butabs.setFont(font);
butCancel.setFont(font);
butBinary.setFont(font);
butBinary.setFont(font);
butContrast.setFont(font);

panel.add(Box.createHorizontalStrut(100));
panelSub1.add(text);
Expand Down Expand Up @@ -176,7 +176,8 @@ public void init() {

panelSub5.add(Box.createHorizontalStrut(92));
panelSub5.add(but[0]);
panelSub5.add(butln);
panelSub5.add(butln);
panelSub5.add(butContrast);
panelSub5.add(Box.createHorizontalStrut(210));
panel.add(panelSub5);

Expand All @@ -196,8 +197,9 @@ public void init() {
panelSub8.add(butabs);
panelSub8.add(butBinary);
panel.add(panelSub8);
panel.setOpaque(true);

for (int i = 0; i < 10; i++) {
for (int i = 0; i < 11; i++) {
but[i].addActionListener(this);
}
butAdd.addActionListener(this);
Expand All @@ -219,6 +221,7 @@ public void init() {

butEqual.addActionListener(this);
butCancel.addActionListener(this);
butContrast.addActionListener(this);

frame.add(panel);
frame.setVisible(true);
Expand All @@ -229,7 +232,7 @@ public void actionPerformed(ActionEvent e) {
final Object source = e.getSource();
Double checkNum = null;

for (int i = 0; i < 10; i++) {
for (int i = 0; i < 11; i++) {
if (source == but[i]) {
text.replaceSelection(buttonValue[i]);
return;
Expand Down Expand Up @@ -307,7 +310,14 @@ public void actionPerformed(ActionEvent e) {

if (source == butBinary)
parsetoBinary();

}
/*
*We want the contrast button to be outside the IF condition,otherwise it will only work
* when the field is NOT empty.
*/
if(source == butContrast)
contrastButton();

text.selectAll();
}
Expand Down Expand Up @@ -336,4 +346,164 @@ public void writer(final Double num) {
text.setText(Double.toString(num));
}
}

/**
* @Author Lampros Giotis
* This method implements the contrast function.It switches the colours of the buttons as well as
* the panels to make it more contrast.When pressed again it switches back to normal
*/
public void contrastButton(){
if(colourCounter == false){//The button has not been pressed.Therfor the starting value remains the same as the initiliazed
panel.setBackground(Color.black);//////
panelSub1.setBackground(Color.black);//
panelSub2.setBackground(Color.black);//
panelSub3.setBackground(Color.black);//////Adjusting the black colour to the panels first
panelSub4.setBackground(Color.black);//
panelSub5.setBackground(Color.black);//
panelSub6.setBackground(Color.black);//
panelSub7.setBackground(Color.black);//
panelSub8.setBackground(Color.black);//

//We also change the contrast of the buttons.
for (int i = 0; i < 11; i++) {
but[i].setBackground(Color.white);
but[i].setForeground(Color.black);
}
//==========================================================================
butabs.setBackground(Color.white);//We change the colours on the butabs button
butabs.setForeground(Color.black);
//======================================================================
butContrast.setBackground(Color.white);//We change the colours on the contrast button
butContrast.setForeground(Color.black);
//======================================================================
butCancel.setBackground(Color.white);//We change the colours on the cancel button
butCancel.setForeground(Color.black);
//======================================================================
butAdd.setBackground(Color.white);//We change the colours on the add button
butAdd.setForeground(Color.black);
//======================================================================
butTan.setBackground(Color.white);//We change the colours on the tan button
butTan.setForeground(Color.black);
//======================================================================
butDivide.setBackground(Color.white);//We change the colours on the divide button
butDivide.setForeground(Color.black);
//======================================================================
butSin.setBackground(Color.white);//We change the colours on the sin button
butSin.setForeground(Color.black);
//======================================================================
butSquare.setBackground(Color.white);//We change the colours on the square button
butSquare.setForeground(Color.black);
//======================================================================
butSquareRoot.setBackground(Color.white);//We change the colours on the squareroot button
butSquareRoot.setForeground(Color.black);
//======================================================================
butEqual.setBackground(Color.white);//We change the colours on the equal button
butEqual.setForeground(Color.black);
//=======================================================================
butMinus.setBackground(Color.white);//We change the colours on the minus button
butMinus.setForeground(Color.black);
//=======================================================================
butMultiply.setBackground(Color.white);//We change the colours on the multiply button
butMultiply.setForeground(Color.black);
//=======================================================================
butCos.setBackground(Color.white);//We change the colours on the cos button
butCos.setForeground(Color.black);
//=======================================================================
butxpowerofy.setBackground(Color.white);//We change the colours on the powerofy button
butxpowerofy.setForeground(Color.black);
//=======================================================================
butlog.setBackground(Color.white);//We change the colours on the log button
butlog.setForeground(Color.black);
//=======================================================================
butrate.setBackground(Color.white);//We change the colours on the rate button
butrate.setForeground(Color.black);
//=======================================================================
butln.setBackground(Color.white);//We change the colours on the ln button
butln.setForeground(Color.black);
//=======================================================================
butBinary.setBackground(Color.white);//We change the colours on the binary button
butBinary.setForeground(Color.black);
//Finally...
colourCounter = true;//We set the button to true as the action listener has been enabled and the colour is changed to black
}else if(colourCounter == true){//The button is pressed
panel.setBackground(Color.lightGray);
panelSub1.setBackground(Color.lightGray);
panelSub2.setBackground(Color.lightGray);
panelSub3.setBackground(Color.lightGray);
panelSub4.setBackground(Color.lightGray);
panelSub5.setBackground(Color.lightGray);
panelSub6.setBackground(Color.lightGray);
panelSub7.setBackground(Color.lightGray);
panelSub8.setBackground(Color.lightGray);
//We also change the contrast of the buttons.
for (int i = 0; i < 11; i++) {
but[i].setBackground(Color.white);
but[i].setForeground(Color.black);
}
//=====================================================================
butabs.setBackground(Color.white);//We change the colours on the butabs button
butabs.setForeground(Color.black);
//======================================================================
butContrast.setBackground(Color.white);//We change the colours on the contrasts button
butContrast.setForeground(Color.black);
//======================================================================
butCancel.setBackground(Color.white);//we change the colour on the cancel button
butCancel.setForeground(Color.black);
//======================================================================
butAdd.setBackground(Color.white);//we change the colour on add button
butAdd.setForeground(Color.black);
//======================================================================
butTan.setBackground(Color.white);//we change the colour of the tan button
butTan.setForeground(Color.black);
//======================================================================
butDivide.setBackground(Color.white);//we change the clour of the division button
butDivide.setForeground(Color.black);
//======================================================================
butSin.setBackground(Color.white);//we change the colour of the sin button
butSin.setForeground(Color.black);
//======================================================================
butSquare.setBackground(Color.white);//we change the colour of the square button
butSquare.setForeground(Color.black);
//======================================================================
butSquareRoot.setBackground(Color.white);//we change the colour the squareroot button
butSquareRoot.setForeground(Color.black);
//======================================================================
butOneDividedBy.setBackground(Color.white);//we change the colour of the deivideby button
butOneDividedBy.setForeground(Color.black);
//======================================================================
butEqual.setBackground(Color.white);//we change the colour of the equal button
butEqual.setForeground(Color.black);
//=======================================================================
butMinus.setBackground(Color.white);//we change the colour of the minus button
butMinus.setForeground(Color.black);
//=======================================================================
butMultiply.setBackground(Color.white);//we change the colour of the multiply button
butMultiply.setForeground(Color.black);
//=======================================================================
butCos.setBackground(Color.white);//we change the colour of the cos button
butCos.setForeground(Color.black);
//=======================================================================
butxpowerofy.setBackground(Color.white);//we change the colour of the powerofy button
butxpowerofy.setForeground(Color.black);
//=======================================================================
butlog.setBackground(Color.white);//we change the colour of the log button
butlog.setForeground(Color.black);
//=======================================================================
butrate.setBackground(Color.white);//we change the colour of the rate button
butrate.setForeground(Color.black);
//=======================================================================
butln.setBackground(Color.white);//we change the colour of the ln button
butln.setForeground(Color.black);
//=======================================================================
butCancel.setBackground(Color.white);//we change the colour of the cancel button
butCancel.setForeground(Color.black);
//=======================================================================
butBinary.setBackground(Color.white);//we change the colour of the binary button
butBinary.setForeground(Color.black);
colourCounter = false;//We change the value back to false in order to suppose
// in some way that the next time the button is pressed,it should change back to black colour.

}
}

}
Loading