Skip to content

Commit

Permalink
Issue #2: Replace java.awt.Color
Browse files Browse the repository at this point in the history
  • Loading branch information
SilverTiger committed Oct 22, 2015
1 parent 9abbab7 commit 6439734
Show file tree
Hide file tree
Showing 7 changed files with 250 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/silvertiger/tutorial/lwjgl/game/Ball.java
Expand Up @@ -23,7 +23,7 @@
*/
package silvertiger.tutorial.lwjgl.game;

import java.awt.Color;
import silvertiger.tutorial.lwjgl.graphic.Color;
import silvertiger.tutorial.lwjgl.graphic.Texture;
import silvertiger.tutorial.lwjgl.math.Vector2f;

Expand Down
2 changes: 1 addition & 1 deletion src/silvertiger/tutorial/lwjgl/game/Entity.java
Expand Up @@ -23,7 +23,7 @@
*/
package silvertiger.tutorial.lwjgl.game;

import java.awt.Color;
import silvertiger.tutorial.lwjgl.graphic.Color;
import silvertiger.tutorial.lwjgl.graphic.Renderer;
import silvertiger.tutorial.lwjgl.graphic.Texture;
import silvertiger.tutorial.lwjgl.math.Vector2f;
Expand Down
2 changes: 1 addition & 1 deletion src/silvertiger/tutorial/lwjgl/game/Paddle.java
Expand Up @@ -23,8 +23,8 @@
*/
package silvertiger.tutorial.lwjgl.game;

import java.awt.Color;
import org.lwjgl.glfw.GLFW;
import silvertiger.tutorial.lwjgl.graphic.Color;
import silvertiger.tutorial.lwjgl.graphic.Texture;
import silvertiger.tutorial.lwjgl.math.Vector2f;

Expand Down
240 changes: 240 additions & 0 deletions src/silvertiger/tutorial/lwjgl/graphic/Color.java
@@ -0,0 +1,240 @@
/*
* The MIT License
*
* Copyright © 2015, Heiko Brumme
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package silvertiger.tutorial.lwjgl.graphic;

/**
* This class represents a RGBA color.
*
* @author Heiko Brumme
*/
public final class Color {

public static final Color WHITE = new Color(1f, 1f, 1f);
public static final Color BLACK = new Color(0f, 0f, 0f);
public static final Color RED = new Color(1f, 0f, 0f);
public static final Color GREEN = new Color(0f, 1f, 0f);
public static final Color BLUE = new Color(0f, 0f, 1f);

/** This value specifies the red component. */
private float red;

/** This value specifies the green component. */
private float green;

/** This value specifies the blue component. */
private float blue;

/** This value specifies the transparency. */
private float alpha;

/** The default color is black. */
public Color() {
this(0f, 0f, 0f);
}

/**
* Creates a RGB-Color with an alpha value of 1.
*
* @param red The red component. Range from 0f to 1f.
* @param green The green component. Range from 0f to 1f.
* @param blue The blue component. Range from 0f to 1f.
*/
public Color(float red, float green, float blue) {
this(red, green, blue, 1f);
}

/**
* Creates a RGBA-Color.
*
* @param red The red component. Range from 0f to 1f.
* @param green The green component. Range from 0f to 1f.
* @param blue The blue component. Range from 0f to 1f.
* @param alpha The transparency. Range from 0f to 1f.
*/
public Color(float red, float green, float blue, float alpha) {
setRed(red);
setGreen(green);
setBlue(blue);
setAlpha(alpha);
}

/**
* Creates a RGB-Color with an alpha value of 1.
*
* @param red The red component. Range from 0 to 255.
* @param green The green component. Range from 0 to 255.
* @param blue The blue component. Range from 0 to 255.
*/
public Color(int red, int green, int blue) {
this(red, green, blue, 1f);
}

/**
* Creates a RGBA-Color.
*
* @param red The red component. Range from 0 to 255.
* @param green The green component. Range from 0 to 255.
* @param blue The blue component. Range from 0 to 255.
* @param alpha The transparency. Range from 0 to 255.
*/
public Color(int red, int green, int blue, int alpha) {
setRed(red);
setGreen(green);
setBlue(blue);
setAlpha(alpha);
}

/**
* Returns the red component.
*
* @return The red component.
*/
public float getRed() {
return red;
}

/**
* Sets the red component.
*
* @param red The red component. Range from 0f to 1f.
*/
public void setRed(float red) {
if (red < 0f) {
red = 0f;
}
if (red > 1f) {
red = 1f;
}
this.red = red;
}

/**
* Sets the red component.
*
* @param red The red component. Range from 0 to 255.
*/
public void setRed(int red) {
setRed(red / 255f);
}

/**
* Returns the green component.
*
* @return The green component.
*/
public float getGreen() {
return green;
}

/**
* Sets the green component.
*
* @param green The green component. Range from 0f to 1f.
*/
public void setGreen(float green) {
if (green < 0f) {
green = 0f;
}
if (green > 1f) {
green = 1f;
}
this.green = green;
}

/**
* Sets the green component.
*
* @param green The green component. Range from 0 to 255.
*/
public void setGreen(int green) {
setGreen(green / 255f);
}

/**
* Returns the blue component.
*
* @return The blue component.
*/
public float getBlue() {
return blue;
}

/**
* Sets the blue component.
*
* @param blue The blue component. Range from 0f to 1f.
*/
public void setBlue(float blue) {
if (blue < 0f) {
blue = 0f;
}
if (blue > 1f) {
blue = 1f;
}
this.blue = blue;
}

/**
* Sets the blue component.
*
* @param blue The blue component. Range from 0 to 255.
*/
public void setBlue(int blue) {
setBlue(blue / 255f);
}

/**
* Returns the transparency.
*
* @return The transparency.
*/
public float getAlpha() {
return alpha;
}

/**
* Sets the transparency.
*
* @param alpha The transparency. Range from 0f to 1f.
*/
public void setAlpha(float alpha) {
if (alpha < 0f) {
alpha = 0f;
}
if (alpha > 1f) {
alpha = 1f;
}
this.alpha = alpha;
}

/**
* Sets the transparency.
*
* @param alpha The transparency. Range from 0 to 255.
*/
public void setAlpha(int alpha) {
setAlpha(alpha / 255f);
}
}
7 changes: 3 additions & 4 deletions src/silvertiger/tutorial/lwjgl/graphic/Renderer.java
Expand Up @@ -23,7 +23,6 @@
*/
package silvertiger.tutorial.lwjgl.graphic;

import java.awt.Color;
import java.awt.FontFormatException;
import java.io.FileInputStream;
import java.io.IOException;
Expand Down Expand Up @@ -427,9 +426,9 @@ public void drawTextureRegion(float x1, float y1, float x2, float y2, float s1,
flush();
}

float r = c.getRed() / 255f;
float g = c.getGreen() / 255f;
float b = c.getBlue() / 255f;
float r = c.getRed();
float g = c.getGreen();
float b = c.getBlue();

vertices.put(x1).put(y1).put(r).put(g).put(b).put(s1).put(t1);
vertices.put(x1).put(y2).put(r).put(g).put(b).put(s1).put(t2);
Expand Down
2 changes: 1 addition & 1 deletion src/silvertiger/tutorial/lwjgl/state/GameState.java
Expand Up @@ -23,12 +23,12 @@
*/
package silvertiger.tutorial.lwjgl.state;

import java.awt.Color;
import java.nio.IntBuffer;
import org.lwjgl.BufferUtils;
import org.lwjgl.glfw.GLFW;
import silvertiger.tutorial.lwjgl.game.Ball;
import silvertiger.tutorial.lwjgl.game.Paddle;
import silvertiger.tutorial.lwjgl.graphic.Color;
import silvertiger.tutorial.lwjgl.graphic.Renderer;
import silvertiger.tutorial.lwjgl.graphic.Texture;

Expand Down
6 changes: 3 additions & 3 deletions src/silvertiger/tutorial/lwjgl/text/Font.java
Expand Up @@ -23,7 +23,6 @@
*/
package silvertiger.tutorial.lwjgl.text;

import java.awt.Color;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
Expand All @@ -37,6 +36,7 @@
import java.util.HashMap;
import java.util.Map;
import org.lwjgl.BufferUtils;
import silvertiger.tutorial.lwjgl.graphic.Color;
import silvertiger.tutorial.lwjgl.graphic.Texture;
import silvertiger.tutorial.lwjgl.graphic.Renderer;

Expand Down Expand Up @@ -213,7 +213,7 @@ private Texture createFontTexture(java.awt.Font font, boolean antiAlias) {
AffineTransform transform = AffineTransform.getScaleInstance(1f, -1f);
transform.translate(0, -image.getHeight());
AffineTransformOp operation = new AffineTransformOp(transform,
AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
image = operation.filter(image, null);

/* Get charWidth and charHeight of image */
Expand Down Expand Up @@ -281,7 +281,7 @@ private BufferedImage createCharImage(java.awt.Font font, char c, boolean antiAl
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
}
g.setFont(font);
g.setPaint(Color.WHITE);
g.setPaint(java.awt.Color.WHITE);
g.drawString(String.valueOf(c), 0, metrics.getAscent());
g.dispose();
return image;
Expand Down

0 comments on commit 6439734

Please sign in to comment.