Skip to content

Commit

Permalink
Add the option to change the GUI colours
Browse files Browse the repository at this point in the history
  • Loading branch information
RoanH committed Feb 10, 2017
1 parent ac262ea commit 41d9355
Show file tree
Hide file tree
Showing 8 changed files with 236 additions and 80 deletions.
Binary file modified KeysPerSecond/src/Thumbs.db
Binary file not shown.
Binary file modified KeysPerSecond/src/graphleft.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified KeysPerSecond/src/graphright.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 3 additions & 4 deletions KeysPerSecond/src/me/roan/kps/BasePanel.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package me.roan.kps;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
Expand Down Expand Up @@ -32,10 +31,10 @@ public abstract class BasePanel extends JPanel {
@Override
public void paintComponent(Graphics g1) {
Graphics2D g = (Graphics2D) g1;
g.setColor(Color.BLACK);
g.setColor(ColorManager.background);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
g.drawImage(Main.unpressed, 2, 2, null);
g.setColor(Color.CYAN);
g.drawImage(ColorManager.unpressed, 2, 2, null);
g.setColor(ColorManager.foreground);
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g.setFont(font1);
g.drawString(getTitle(), (this.getWidth() - g.getFontMetrics().stringWidth(getTitle())) / 2, 30);
Expand Down
119 changes: 119 additions & 0 deletions KeysPerSecond/src/me/roan/kps/ColorManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package me.roan.kps;

import java.awt.Color;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;

/**
* Central class for managing
* colours and resources
* @author Roan
*/
public class ColorManager {
/**
* Image for a pressed key
* Image taken from osu!lazer
* https://cloud.githubusercontent.com/assets/191335/16511435/17acd2f2-3f8b-11e6-8b50-5fccba819ce5.png
*/
protected static Image pressed;
/**
* Image for an unpressed key<br>
* Image taken from osu!lazer
* https://cloud.githubusercontent.com/assets/191335/16511432/17ac5232-3f8b-11e6-95b7-33f9a4df0b7c.png
*/
protected static Image unpressed;
/**
* Left side of the graph border
*/
protected static Image gleft = null;
/**
* Right side of the graph border
*/
protected static Image gright = null;
/**
* Middle section of the graph border
*/
protected static Image gmid = null;
/**
* Foreground color
*/
protected static Color foreground;
/**
* Background color
*/
protected static Color background;
/**
* Color used to fill the area underneath the graph
*/
protected static Color alphaAqua;

/**
* Prepares the colours and images
* used by the program.
* @param front The foreground colour of the GUI
* @param back The background colour of the GUI
* @param graph Whether or not to load resources for the graph
* @param custom Whether or not the supplied colours are custom colours
* @throws IOException When an IOException occurs
*/
public static final void prepareImages(Color front, Color back, boolean graph, boolean custom) throws IOException{
if(custom){
foreground = front;
background = back;
alphaAqua = new Color(front.getRed(), front.getGreen(), front.getBlue(), 51);
pressed = dye(ImageIO.read(ClassLoader.getSystemResource("hit.png")));
unpressed = dye(ImageIO.read(ClassLoader.getSystemResource("key.png")));
if(graph){
gright = dye(ImageIO.read(ClassLoader.getSystemResource("graphright.png")));
gleft = dye(ImageIO.read(ClassLoader.getSystemResource("graphleft.png")));
gmid = dye(ImageIO.read(ClassLoader.getSystemResource("graphmiddle.png")));
}
}else{
foreground = Color.CYAN;
background = Color.BLACK;
alphaAqua = new Color(0.0F, 1.0F, 1.0F, 0.2F);
pressed = ImageIO.read(ClassLoader.getSystemResource("hit.png"));
unpressed = ImageIO.read(ClassLoader.getSystemResource("key.png"));
if(graph){
gright = ImageIO.read(ClassLoader.getSystemResource("graphright.png"));
gleft = ImageIO.read(ClassLoader.getSystemResource("graphleft.png"));
gmid = ImageIO.read(ClassLoader.getSystemResource("graphmiddle.png"));
}
}
}

/**
* Parses the given image in
* order to change its colours
* to the set {@link #foreground}
* colour
* @param img The image to parse
* @return The recoloured image
*/
private static final Image dye(BufferedImage img){
for(int x = 0; x < img.getWidth(); x++){
for(int y = 0; y < img.getHeight(); y++){
img.setRGB(x, y, repaintPixel(img.getRGB(x, y), foreground));
}
}
return img;
}

/**
* Mixes the two given colours
* where the new color is made
* twice as prominent as the
* original color while the
* alpha value is copied from
* the original color
* @param original The original color
* @param newcolor The new color
* @return The two mixed colours
*/
private static final int repaintPixel(int original, Color newcolor){
return (((original >> 24) & 0x000000FF) << 24) | (((((original >> 16) & 0x000000FF) + 2 * newcolor.getRed()) / 3) << 16) | (((((original >> 8) & 0x000000FF) + 2 * newcolor.getGreen()) / 3) << 8) | (((original & 0x000000FF) + 2 * newcolor.getBlue()) / 3);
}
}
38 changes: 10 additions & 28 deletions KeysPerSecond/src/me/roan/kps/GraphPanel.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package me.roan.kps;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Polygon;
import java.awt.RenderingHints;
import java.awt.Stroke;
Expand Down Expand Up @@ -38,10 +36,6 @@ public class GraphPanel extends JPanel{
* Draw the horizontal average line
*/
protected static boolean showAverage = true;
/**
* Color used to fill the area underneath the graph
*/
private static final Color alphaAqua = new Color(0.0F, 1.0F, 1.0F, 0.2F);
/**
* Stroke used to draw the graph
*/
Expand All @@ -54,18 +48,6 @@ public class GraphPanel extends JPanel{
* Whether or not the graph is active and plotting
*/
protected boolean enabled = true;
/**
* Left side of the graph border
*/
protected static Image gleft = null;
/**
* Right side of the graph border
*/
protected static Image gright = null;
/**
* Middle section of the graph border
*/
protected static Image gmid = null;
/**
* Number of frames, this is used to determine
* the width of the graph and it equal
Expand All @@ -80,12 +62,12 @@ public void paintComponent(Graphics g1){
if(enabled){
Graphics2D g = (Graphics2D)g1;
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(Color.BLACK);
g.setColor(ColorManager.background);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
Polygon poly = new Polygon();
poly.addPoint(this.getWidth() - 5, this.getHeight() - 5);
poly.addPoint(this.getWidth() - 6, this.getHeight() - 5);
for(int i = 1; i <= values.size(); i++){
int px = (int) (5 + ((double)(this.getWidth() - 10) / (double)(MAX - 1)) * (MAX - i));
int px = (int) (6 + ((double)(this.getWidth() - 12) / (double)(MAX - 1)) * (MAX - i));
int py = (int) (this.getHeight() - 5 - ((float)(this.getHeight() - 11) * ((float)values.get(i - 1) / (float)maxval)));
poly.addPoint(px, py);
if(i == values.size()){
Expand All @@ -94,18 +76,18 @@ public void paintComponent(Graphics g1){
}
if(showAverage){
int y = (int) (this.getHeight() - 5 - ((float)(this.getHeight() - 11) * (Main.avg / (float)maxval)));
g.setColor(Color.CYAN.darker());
g.setColor(ColorManager.foreground.darker());
g.setStroke(avgstroke);
g.drawLine(3, y, this.getWidth() - 5, y);
g.drawLine(5, y, this.getWidth() - 5, y);
}
g.setStroke(line);
g.setColor(alphaAqua);
g.setColor(ColorManager.alphaAqua);
g.fillPolygon(poly);
g.setColor(Color.CYAN);
g.setColor(ColorManager.foreground);
g.drawPolygon(poly);
g.drawImage(gleft, 3, 2, null);
g.drawImage(gmid, 44 + 1, 2, (44 + 2) * (frames - 2), 64, null);
g.drawImage(gright, 44 + 1 + (44 + 2) * (frames - 2), 2, null);
g.drawImage(ColorManager.gleft, 3, 2, null);
g.drawImage(ColorManager.gmid, 44 + 1, 2, (44 + 2) * (frames - 2), 64, null);
g.drawImage(ColorManager.gright, 44 + 1 + (44 + 2) * (frames - 2), 2, null);
}
}

Expand Down
9 changes: 4 additions & 5 deletions KeysPerSecond/src/me/roan/kps/KeyPanel.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package me.roan.kps;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
Expand Down Expand Up @@ -55,13 +54,13 @@ protected KeyPanel(Key key) {
@Override
public void paintComponent(Graphics g1) {
Graphics2D g = (Graphics2D) g1;
g.setColor(Color.BLACK);
g.setColor(ColorManager.background);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
g.drawImage(Main.unpressed, 2, 2, null);
g.drawImage(ColorManager.unpressed, 2, 2, null);
if (key.down) {
g.drawImage(Main.pressed, 2, 2, null);
g.drawImage(ColorManager.pressed, 2, 2, null);
}else{
g.setColor(Color.CYAN);
g.setColor(ColorManager.foreground);
}
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
if(key.name.length() == 1){
Expand Down
Loading

0 comments on commit 41d9355

Please sign in to comment.