Created
November 18, 2011 02:34
-
-
Save ReticentIris/1375398 to your computer and use it in GitHub Desktop.
Project Boxx - An open source Java graphic framework.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package boxx.component; | |
import java.awt.Color; | |
import java.awt.Dimension; | |
import java.awt.Font; | |
import java.awt.FontMetrics; | |
import java.awt.Graphics; | |
import java.awt.Graphics2D; | |
import java.awt.Image; | |
import java.awt.Point; | |
import java.awt.RenderingHints; | |
import java.awt.Shape; | |
import boxx.DimensionException; | |
/** | |
* | |
* @author Andrew Lee | |
* | |
*/ | |
public class BoxxComponent { | |
/* | |
* Instance variables. | |
*/ | |
private String label; | |
private Point point; | |
private Point labelPoint; | |
private Graphics g; | |
private Color backgroundColor; | |
private Image backgroundImage; | |
private Color borderColor; | |
private int borderWidth = 0; | |
private Color foregroundColor; | |
private Font font; | |
private Dimension size; | |
private Shape shape; | |
private boolean antiAlias; | |
/* | |
* Constructors. | |
*/ | |
/** | |
* Creates a new BoxxComponent with the specified properties. | |
* @param point The pointdinates of the component. | |
* @param g The graphics object that the component is to be used with. | |
*/ | |
public BoxxComponent(Point point, Graphics g){ | |
this.point = point; | |
this.g = g; | |
} | |
/** | |
* Creates a new BoxxComponent with the specified properties. | |
* @param point The pointdinates of the component. | |
* @param size The size of the component. | |
* @param g The graphics object that the component is to be used with. | |
*/ | |
public BoxxComponent(Point point, Dimension size, Graphics g){ | |
this.point = point; | |
this.size = size; | |
this.g = g; | |
} | |
/** | |
* Creates a new BoxxComponent with the specified properties. | |
* @param point The pointdinates of the component. | |
* @param size The size of the component. | |
* @param label The label of the component. | |
* @param g The graphics object that the component is to be used with. | |
*/ | |
public BoxxComponent(Point point, Dimension size, String label, Point labelPoint, Graphics g){ | |
this.point = point; | |
this.size = size; | |
this.label = label; | |
this.labelPoint = labelPoint; | |
this.g = g; | |
} | |
/* | |
* Paint methods. | |
*/ | |
/** | |
* Paints the component. | |
* @param g The graphics object used to paint the component. | |
*/ | |
public void paint(Graphics g){ | |
//Create a copy of g in order to reset g later. | |
Graphics gg = g.create(); | |
//Use Graphics2D and create variables x and y. | |
Graphics2D g2d = (Graphics2D) g; | |
int x, y; | |
//If anti alias is enabled. | |
if(antiAlias){ | |
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); | |
} | |
//Set the font. | |
if(font != null){ | |
g2d.setFont(getFont()); | |
} | |
//Width and height of the component. | |
int width = (int) getSize().getWidth(); | |
int height = (int) getSize().getHeight(); | |
//Paint the background first. | |
x = (int) getPoint().getX(); | |
y = (int) getPoint().getY(); | |
if(getBackgroundColor() != null){ | |
g2d.setColor(getBackgroundColor()); | |
if(shape != null){ | |
g2d.fill(shape); | |
} | |
else{ | |
g2d.fillRect(x, y, width, height); | |
//Paint the border. | |
g2d.setColor(getBorderColor()); | |
for(int n = 0; n < getBorderWidth(); n++){ | |
//If there is no border stop. | |
if(getBorderWidth() == 0){ | |
continue; | |
} | |
g2d.drawRect(x - n, y - n, width - 2 * n, height - 2 * n); | |
} | |
} | |
} | |
//Background image. | |
if(getBackgroundImage() != null){ | |
g2d.drawImage(backgroundImage, x, y, null); | |
} | |
//Label | |
if(getLabel() != null){ | |
//If the coordinates for the label are specified. | |
if(getLabelPoint() != null){ | |
x = (int) getLabelPoint().getX(); | |
y = (int) getLabelPoint().getY(); | |
} | |
else{ | |
//Attempt to center the string based on the size of the component and string. | |
FontMetrics metrics = g2d.getFontMetrics(font); | |
int labelHeight = metrics.getHeight(); | |
int labelWidth = metrics.stringWidth(getLabel()); | |
x = (int) getPoint().getX(); | |
y = (int) getPoint().getY(); | |
x = x + (int) (width / 2); | |
y = y + (int) (height / 2); | |
x -= labelWidth / 2; | |
y += labelHeight / 4; | |
} | |
//Set the foreground color and paint the label. | |
g2d.setColor(getForegroundColor()); | |
g2d.drawString(getLabel(), x, y); | |
} | |
g = gg; | |
} | |
/** | |
* Repaints the component. | |
* @throws DimensionException | |
*/ | |
public void repaint(){ | |
paint(g); | |
} | |
/* | |
* Setters. | |
*/ | |
/** | |
* Sets the label of the component to the one specified. | |
* @param label The new label for the component. | |
*/ | |
public void setLabel(String label){ | |
this.label = label; | |
} | |
/** | |
* Sets the point of the component to the one specified. | |
* @param point The new point for the component. | |
*/ | |
public void setPoint(Point point){ | |
this.point = point; | |
} | |
/** | |
* Sets the point of the component's label to the one specified. | |
* @param point The new point for the component's label. | |
*/ | |
public void setLabelPoint(Point point){ | |
this.labelPoint = point; | |
} | |
/** | |
* Sets the background of the component to the one specified. | |
* @param background The new background color for the component. | |
*/ | |
public void setBackgroundColor(Color backgroundColor){ | |
this.backgroundColor = backgroundColor; | |
} | |
/** | |
* Sets the background of the component to the one specified. | |
* @param backgroundImage The new background image for the component. | |
*/ | |
public void setBackgroundImage(Image backgroundImage){ | |
this.backgroundImage = backgroundImage; | |
} | |
/** | |
* Sets the border color of the component to the one specified. | |
* @param borderColor The new border color for the component. | |
*/ | |
public void setBorderColor(Color borderColor){ | |
this.borderColor = borderColor; | |
} | |
/** | |
* Sets the border width of the component to the one specified. | |
* @param borderWidth The new border width for the component. | |
*/ | |
public void setBorderWidth(int borderWidth){ | |
this.borderWidth = borderWidth; | |
} | |
/** | |
* Sets the foreground of the component to the one specified. | |
* @param foreground The new foreground for the component. | |
*/ | |
public void setForegroundColor(Color foreground){ | |
this.foregroundColor = foreground; | |
} | |
/** | |
* Sets the font of the component to the one specified. | |
* @param font The new font for the component. | |
*/ | |
public void setFont(Font font){ | |
this.font = font; | |
} | |
/** | |
* Sets the size of the component to the one specified. | |
* @param size The new size for the component. | |
*/ | |
public void setSize(Dimension size){ | |
this.size = size; | |
} | |
/** | |
* Sets the shape of the component to the one specified. | |
* @param shape The new shape for the component. | |
*/ | |
public void setShape(Shape shape){ | |
this.shape = shape; | |
} | |
/** | |
* Set whether to antialias the component. | |
* @param antialias Whether to antialias the component. | |
*/ | |
public void setAntiAlias(boolean antiAlias){ | |
this.antiAlias = antiAlias; | |
} | |
/* | |
* Getters. | |
*/ | |
/** | |
* Returns the label of the component. | |
* @return The label of the component. | |
*/ | |
public String getLabel(){ | |
return label; | |
} | |
/** | |
* Returns the coordinates of the component as a point. | |
* @return The coordinates of the componenet as a point. | |
*/ | |
public Point getPoint(){ | |
return point; | |
} | |
/** | |
* Returns the coordinates of the component's label. | |
* @return The coordinates of the component's label. | |
*/ | |
public Point getLabelPoint(){ | |
return labelPoint; | |
} | |
/** | |
* Returns the background color of the component. | |
* @return The background color of the component. | |
*/ | |
public Color getBackgroundColor(){ | |
return backgroundColor; | |
} | |
/** | |
* Returns the background image of the component. | |
* @return The background image of the component. | |
*/ | |
public Image getBackgroundImage(){ | |
return backgroundImage; | |
} | |
/** | |
* Returns the border color of the componenet. | |
* @return The border color of the componenet. | |
*/ | |
public Color getBorderColor(){ | |
return borderColor; | |
} | |
/** | |
* Returns the border width of the componenet. | |
* @return The border width of the componenet. | |
*/ | |
public int getBorderWidth(){ | |
return borderWidth; | |
} | |
/** | |
* Returns the foreground color of the component. | |
* @return The foreground color of the component. | |
*/ | |
public Color getForegroundColor(){ | |
return foregroundColor; | |
} | |
/** | |
* Returns the font of the component. | |
* @return The font of the component. | |
*/ | |
public Font getFont(){ | |
return font; | |
} | |
/** | |
* Returns the dimensions of the component. | |
* @return The dimensions of the component. | |
*/ | |
public Dimension getSize(){ | |
return size; | |
} | |
/** | |
* Returns the shape of the component. | |
* @return The shape of the component. | |
*/ | |
public Shape getShape(){ | |
return shape; | |
} | |
/** | |
* Returns whether or not the component is anti aliased. | |
* @return Whether or not the component is anti aliased. | |
*/ | |
public boolean isAntiAliased(){ | |
return antiAlias; | |
} | |
/* | |
* Interaction methods. | |
*/ | |
public boolean isHovered(Point point){ | |
Point that = this.point; | |
double x = point.getX(); | |
double y = point.getY(); | |
if(x >= that.getX() && x <= that.getX() + getSize().getWidth()){ | |
if(y >= that.getY() && y <= that.getY() + getSize().getHeight()){ | |
return true; | |
} | |
else{ | |
return false; | |
} | |
} | |
else{ | |
return false; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sandbox; | |
import javax.swing.*; | |
import java.awt.*; | |
import java.awt.event.*; | |
import boxx.*; | |
import boxx.component.Button; | |
public class Sandbox extends JFrame{ | |
private Button butt; | |
private Button butt2; | |
private Button butt3; | |
private Boolean hover1 = false; | |
private Boolean hover2 = false; | |
private Boolean hover3 = false; | |
public Sandbox(){ | |
addMouseMotionListener(new MouseAdapter(){ | |
public void mouseMoved(MouseEvent e){ | |
if(!hover1 && !hover2 && !hover3){ | |
if(butt.isHovered(e.getPoint())){ | |
hover1 = true; | |
repaint(); | |
} | |
else if(butt2.isHovered(e.getPoint())){ | |
hover2 = true; | |
repaint(); | |
} | |
else if(butt3.isHovered(e.getPoint())){ | |
hover3 = true; | |
repaint(); | |
} | |
} | |
else | |
if(!butt.isHovered(e.getPoint()) && !butt2.isHovered(e.getPoint()) && !butt3.isHovered(e.getPoint())){ | |
hover1 = hover2 = hover3 = false; | |
repaint(); | |
} | |
} | |
}); | |
} | |
public void paint(Graphics g){ | |
g.setColor(Color.green); | |
g.fillRect(0, 0, getWidth(), getHeight()); | |
butt = new Button(new Point(100, 100), new Dimension(100, 40), g); | |
butt.setBackgroundColor(hover1 ? Color.black : Color.white); | |
butt.setForegroundColor(hover1 ? Color.white : Color.black); | |
butt.setLabel("aaa"); | |
butt.setFont(new Font("Arial", Font.PLAIN, 30)); | |
butt.setAntiAlias(true); | |
butt.repaint(); | |
butt2 = new Button(new Point(200, 200), new Dimension(100, 40), g); | |
butt2.setBackgroundColor(hover2 ? Color.black : Color.white); | |
butt2.setForegroundColor(hover2 ? Color.white : Color.black); | |
butt2.setLabel("bbb"); | |
butt2.setFont(new Font("Arial", Font.PLAIN, 30)); | |
butt2.setAntiAlias(true); | |
butt2.repaint(); | |
butt3 = new Button(new Point(300, 300), new Dimension(100, 40), g); | |
butt3.setBackgroundColor(hover3 ? Color.black : Color.white); | |
butt3.setForegroundColor(hover3 ? Color.white : Color.black); | |
butt3.setLabel("ccc"); | |
butt3.setFont(new Font("Arial", Font.PLAIN, 30)); | |
butt3.setAntiAlias(true); | |
butt3.repaint(); | |
} | |
public static void main(String[] args){ | |
Sandbox sand; | |
sand = new Sandbox(); | |
sand.setSize(500, 500); | |
sand.show(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment