PMD and bugfix #2 on FPanel.

This commit is contained in:
Doublestrike
2012-02-13 04:16:00 +00:00
parent 53c1692b14
commit 88edc5a0ad

View File

@@ -22,6 +22,7 @@ import java.awt.Color;
import java.awt.Graphics; import java.awt.Graphics;
import java.awt.Graphics2D; import java.awt.Graphics2D;
import java.awt.Image; import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.event.MouseAdapter; import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent; import java.awt.event.MouseEvent;
import java.awt.geom.Area; import java.awt.geom.Area;
@@ -29,6 +30,7 @@ import java.awt.geom.RoundRectangle2D;
import javax.swing.ImageIcon; import javax.swing.ImageIcon;
import javax.swing.JPanel; import javax.swing.JPanel;
import javax.swing.RepaintManager;
import javax.swing.border.Border; import javax.swing.border.Border;
import forge.Command; import forge.Command;
@@ -61,14 +63,12 @@ public class FPanel extends JPanel {
private boolean selected, hovered; private boolean selected, hovered;
private Command cmdClick; private Command cmdClick;
// Width/height of panel, bg img, scaled bg img, texture img. Coords. // Width/height of panel, bg img, scaled bg img, texture img. Coords.
private int pw, ph, iw, ih, sw, sh, tw, th, x, y; private int pnlW, pnlH, imgW, imgH, scaledW, scaledH, textureW, textureH, tempX, tempY;
// Image aspect ratio (width / height) // Image aspect ratio (width / height)
private double iar; private double iar;
// Graphics clone to avoid clobbering original
private Graphics2D g2d;
// Clip rounded corner shape // Clip rounded corner shape
private Area clip; private Area clip;
private BasicStroke borderStroke = new BasicStroke(2.0f); private final BasicStroke borderStroke = new BasicStroke(2.0f);
/** Core panel used in UI. See class javadoc for more details. */ /** Core panel used in UI. See class javadoc for more details. */
public FPanel() { public FPanel() {
@@ -84,12 +84,12 @@ public class FPanel extends JPanel {
// Mouse event handler // Mouse event handler
private final MouseAdapter madEvents = new MouseAdapter() { private final MouseAdapter madEvents = new MouseAdapter() {
@Override @Override
public void mouseEntered(MouseEvent e) { hovered = true; repaint(); } public void mouseEntered(final MouseEvent evt) { hovered = true; repaint(); }
@Override @Override
public void mouseExited(MouseEvent e) { hovered = false; repaint(); } public void mouseExited(final MouseEvent evt) { hovered = false; repaint(); }
@Override @Override
public void mouseClicked(MouseEvent e) { public void mouseClicked(final MouseEvent evt) {
if (cmdClick != null) { cmdClick.execute(); } if (cmdClick != null) { cmdClick.execute(); }
if (!selectable) { return; } if (!selectable) { return; }
@@ -99,15 +99,14 @@ public class FPanel extends JPanel {
}; };
//========== Mutators //========== Mutators
/**@param i0   int */ /**@param int0   int */
public void setCornerDiameter(int i0) { public void setCornerDiameter(final int int0) {
if (i0 < 0) { i0 = 0; } this.cornerDiameter = (int0 <= 0 ? 0 : int0);
this.cornerDiameter = i0;
} }
/** @param c0 &emsp; {@link forge.Command} on click */ /** @param cmd0 &emsp; {@link forge.Command} on click */
public void setCommand(Command c0) { public void setCommand(final Command cmd0) {
this.cmdClick = c0; this.cmdClick = cmd0;
} }
/** @return {@link forge.Command} */ /** @return {@link forge.Command} */
@@ -115,77 +114,76 @@ public class FPanel extends JPanel {
return this.cmdClick; return this.cmdClick;
} }
/** @param b0 &emsp; boolean */ /** @param bool0 &emsp; boolean */
public void setHoverable(boolean b0) { public void setHoverable(final boolean bool0) {
hoverable = b0; hoverable = bool0;
this.confirmDrawEfficiency(); this.confirmDrawEfficiency();
if (!b0) { this.removeMouseListener(madEvents); } if (bool0) { this.addMouseListener(madEvents); }
else { this.addMouseListener(madEvents); } else { this.removeMouseListener(madEvents); }
} }
/** @param b0 &emsp; boolean */ /** @param bool0 &emsp; boolean */
public void setSelectable(boolean b0) { public void setSelectable(final boolean bool0) {
this.selectable = b0; this.selectable = bool0;
this.confirmDrawEfficiency(); this.confirmDrawEfficiency();
} }
/** @param b0 &emsp; boolean */ /** @param bool0 &emsp; boolean */
public void setSelected(boolean b0) { public void setSelected(final boolean bool0) {
selected = b0; selected = bool0;
if (b0) { this.setBackground(FSkin.getColor(FSkin.Colors.CLR_ACTIVE)); } if (bool0) { this.setBackground(FSkin.getColor(FSkin.Colors.CLR_ACTIVE)); }
else { this.setBackground(FSkin.getColor(FSkin.Colors.CLR_INACTIVE)); } else { this.setBackground(FSkin.getColor(FSkin.Colors.CLR_INACTIVE)); }
repaint(); repaint();
} }
/** @param i0 &emsp; {@link java.awt.Image} */ /** @param img0 &emsp; {@link java.awt.Image} */
public void setForegroundImage(Image i0) { public void setForegroundImage(final Image img0) {
if (i0 == null) { return; } if (img0 == null) { return; }
this.foregroundImage = i0; this.foregroundImage = img0;
this.iw = i0.getWidth(null); this.imgW = img0.getWidth(null);
this.ih = i0.getHeight(null); this.imgH = img0.getHeight(null);
this.iar = (double) iw / (double) ih; this.iar = (double) imgW / (double) imgH;
} }
/** @param i0 &emsp; {@link javax.swing.ImageIcon} */ /** @param ii0 &emsp; {@link javax.swing.ImageIcon} */
public void setForegroundImage(ImageIcon i0) { public void setForegroundImage(final ImageIcon ii0) {
setForegroundImage(i0.getImage()); this.foregroundImage = ii0.getImage();
} }
/** @param b0 &emsp; boolean, stretch the foreground to fit */ /** @param bool0 &emsp; boolean, stretch the foreground to fit */
public void setForegroundStretch(final boolean b0) { public void setForegroundStretch(final boolean bool0) {
this.foregroundStretch = b0; this.foregroundStretch = bool0;
} }
/** @param i0 &emsp; {@link java.awt.Image} */ /** @param img0 &emsp; {@link java.awt.Image} */
public void setBackgroundTexture(Image i0) { public void setBackgroundTexture(final Image img0) {
if (i0 == null) { return; } if (img0 == null) { return; }
this.backgroundTexture = i0; this.backgroundTexture = img0;
this.tw = i0.getWidth(null); this.textureW = img0.getWidth(null);
this.th = i0.getHeight(null); this.textureH = img0.getHeight(null);
} }
/** @param i0 &emsp; {@link javax.swing.ImageIcon} */ /** @param ii0 &emsp; {@link javax.swing.ImageIcon} */
public void setBackgroundTexture(ImageIcon i0) { public void setBackgroundTexture(final ImageIcon ii0) {
setBackgroundTexture(i0.getImage()); setBackgroundTexture(ii0.getImage());
} }
/** @param b0 &emsp; boolean */ /** @param bool0 &emsp; boolean */
public void setBorderToggle(final boolean b0) { public void setBorderToggle(final boolean bool0) {
this.borderToggle = b0; this.borderToggle = bool0;
} }
/** @param c0 &emsp; {@link java.awt.Color} */ /** @param clr0 &emsp; {@link java.awt.Color} */
public void setBorderColor(final Color c0) { public void setBorderColor(final Color clr0) {
this.borderColor = c0; this.borderColor = clr0;
} }
@Override @Override
public void setBorder(Border b0) { } public void setBorder(final Border bord0) {
// Intentionally empty
@Override }
public void setOpaque(boolean b0) { super.setOpaque(false); }
/* /*
* (non-Javadoc) * (non-Javadoc)
@@ -193,15 +191,16 @@ public class FPanel extends JPanel {
* @see javax.swing.JComponent#paintComponent(java.awt.Graphics) * @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
*/ */
@Override @Override
protected void paintComponent(final Graphics g) { protected void paintComponent(final Graphics graphics0) {
super.paintComponent(g); pnlW = this.getWidth();
pw = this.getWidth(); pnlH = this.getHeight();
ph = this.getHeight(); final Graphics2D g2d = (Graphics2D) graphics0;
g2d = (Graphics2D) g.create(); g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
RepaintManager.currentManager(this).markCompletelyDirty(this);
// Set clip for rounded area // Set clip for rounded area
if (cornerDiameter > 0) { if (cornerDiameter > 0) {
clip = new Area(new RoundRectangle2D.Float(0, 0, pw, ph, cornerDiameter, cornerDiameter)); clip = new Area(new RoundRectangle2D.Float(0, 0, pnlW, pnlH, cornerDiameter, cornerDiameter));
g2d.setClip(clip); g2d.setClip(clip);
} }
@@ -209,11 +208,11 @@ public class FPanel extends JPanel {
if (foregroundStretch && foregroundImage != null) { if (foregroundStretch && foregroundImage != null) {
drawForegroundStretched(g2d); drawForegroundStretched(g2d);
} }
else if (this.backgroundTexture != null) { else if (this.backgroundTexture == null) {
drawBackgroundTexture(g2d); drawBackgroundColor(g2d);
} }
else { else {
drawBackgroundColor(g2d); drawBackgroundTexture(g2d);
} }
// Draw foreground as required // Draw foreground as required
@@ -226,103 +225,98 @@ public class FPanel extends JPanel {
} }
// Clear memory // Clear memory
if (clip != null) { clip.reset(); } //if (clip != null) { clip.reset(); }
g2d.dispose(); //g2d.dispose();
} }
//========== Special draw methods //========== Special draw methods
private void drawBackgroundColor(final Graphics g0) { private void drawBackgroundColor(final Graphics2D g2d0) {
// Color background as appropriate // Color background as appropriate
if (selected) { g0.setColor(FSkin.getColor(FSkin.Colors.CLR_ACTIVE)); } if (selected) { g2d0.setColor(FSkin.getColor(FSkin.Colors.CLR_ACTIVE)); }
else if (hovered) { g0.setColor(FSkin.getColor(FSkin.Colors.CLR_HOVER)); } else if (hovered) { g2d0.setColor(FSkin.getColor(FSkin.Colors.CLR_HOVER)); }
else if (selectable) { g0.setColor(FSkin.getColor(FSkin.Colors.CLR_INACTIVE)); } else if (selectable) { g2d0.setColor(FSkin.getColor(FSkin.Colors.CLR_INACTIVE)); }
else { g0.setColor(getBackground()); } else { g2d0.setColor(getBackground()); }
// Parent must be drawn onto clipped object. // Parent must be drawn onto clipped object.
g0.fillRoundRect(0, 0, pw, ph, cornerDiameter, cornerDiameter); g2d0.fillRect(0, 0, pnlW, pnlH);
} }
private void drawBackgroundTexture(final Graphics g0) { private void drawBackgroundTexture(final Graphics2D g2d0) {
this.x = 0; this.tempX = 0;
this.y = 0; this.tempY = 0;
while (this.x < this.pw) { while (this.tempX < this.pnlW) {
while (this.y < this.ph) { while (this.tempY < this.pnlH) {
g0.drawImage(this.backgroundTexture, (int) this.x, (int) this.y, null); g2d0.drawImage(this.backgroundTexture, (int) this.tempX, (int) this.tempY, null);
this.y += this.th; this.tempY += this.textureH;
} }
this.x += this.tw; this.tempX += this.textureW;
this.y = 0; this.tempY = 0;
} }
this.x = 0; this.tempX = 0;
} }
private void drawForegroundScaled(final Graphics g0) { private void drawForegroundScaled(final Graphics2D g2d0) {
// Scaling 1: First dimension larger than panel // Scaling 1: First dimension larger than panel
if (iw >= pw) { // Image is wider than panel? Shrink to width. if (imgW >= pnlW) { // Image is wider than panel? Shrink to width.
sw = pw; scaledW = pnlW;
sh = (int) (sw / iar); scaledH = (int) (scaledW / iar);
} }
else if (ih >= ph) { // Image is taller than panel? Shrink to height. else if (imgH >= pnlH) { // Image is taller than panel? Shrink to height.
sh = ph; scaledH = pnlH;
sw = (int) (sh * iar); scaledW = (int) (scaledH * iar);
} }
else { // Image is smaller than panel? No scaling. else { // Image is smaller than panel? No scaling.
sw = iw; scaledW = imgW;
sh = ih; scaledH = imgH;
} }
// Scaling step 2: Second dimension larger than panel // Scaling step 2: Second dimension larger than panel
if (sh > ph) { // Scaled image still taller than panel? if (scaledH > pnlH) { // Scaled image still taller than panel?
sh = ph; scaledH = pnlH;
sw = (int) (sh * iar); scaledW = (int) (scaledH * iar);
} }
else if (sw > pw) { // Scaled image still wider than panel? else if (scaledW > pnlW) { // Scaled image still wider than panel?
sw = pw; scaledW = pnlW;
sh = (int) (sw / iar); scaledH = (int) (scaledW / iar);
} }
// Scaling step 3: Center image in panel // Scaling step 3: Center image in panel
x = (int) ((pw - sw) / 2); tempX = (int) ((pnlW - scaledW) / 2);
y = (int) ((ph - sh) / 2); tempY = (int) ((pnlH - scaledH) / 2);
g0.drawImage(foregroundImage, x, y, sw + x, sh + y, 0, 0, iw, ih, null); g2d0.drawImage(foregroundImage, tempX, tempY, scaledW + tempX, scaledH + tempY, 0, 0, imgW, imgH, null);
} }
private void drawForegroundStretched(final Graphics g0) { private void drawForegroundStretched(final Graphics2D g2d0) {
g0.drawImage(foregroundImage, 0, 0, pw, ph, 0, 0, iw, ih, null); g2d0.drawImage(foregroundImage, 0, 0, pnlW, pnlH, 0, 0, imgW, imgH, null);
} }
private void drawBorder(final Graphics2D g0) { private void drawBorder(final Graphics2D g2d0) {
g0.setColor(borderColor); g2d0.setColor(borderColor);
g0.setStroke(borderStroke); g2d0.setStroke(borderStroke);
g0.drawRoundRect(0, 0, pw, ph, cornerDiameter, cornerDiameter); g2d0.drawRoundRect(0, 0, pnlW, pnlH, cornerDiameter, cornerDiameter);
} }
/** Improves performance by checking to see if any graphics will cancel /** Improves performance by checking to see if any graphics will cancel
* each other out, so unnecessary out-of-sight graphics will not stack up. */ * each other out, so unnecessary out-of-sight graphics will not stack up. */
private void confirmDrawEfficiency() { private void confirmDrawEfficiency() {
final String str = "\nAn FPanel may not be simultaneously "
+ "%s and have a %s.\nPlease adjust the panel's declaration to use one or the other.";
if (hoverable && foregroundStretch) { if (hoverable && foregroundStretch) {
throw new IllegalArgumentException("\nAn FPanel may not be simultaneously " throw new IllegalArgumentException(String.format(str, "hoverable", "stretched foreground image"));
+ "hoverable and have a stretched foreground image.\n"
+ "Please adjust the panel's declaration to use one or the other.");
} }
if (hoverable && backgroundTexture != null) { if (hoverable && backgroundTexture != null) {
throw new IllegalArgumentException("\nAn FPanel may not be simultaneously " throw new IllegalArgumentException(String.format(str, "hoverable", "background texture"));
+ "hoverable and have a background texture.\n"
+ "Please adjust the panel's declaration to use one or the other.");
} }
if (selectable && foregroundStretch) { if (selectable && foregroundStretch) {
throw new IllegalArgumentException("\nAn FPanel may not be simultaneously " throw new IllegalArgumentException(String.format(str, "selectable", "stretched foreground image"));
+ "selectable and have a stretched foreground image.\n"
+ "Please adjust the panel's declaration to use one or the other.");
} }
if (selectable && backgroundTexture != null) { if (selectable && backgroundTexture != null) {
throw new IllegalArgumentException("\nAn FPanel may not be simultaneously " throw new IllegalArgumentException(String.format(str, "selectable", "background texture"));
+ "selectable and have a background texture.\n"
+ "Please adjust the panel's declaration to use one or the other.");
} }
} }
} }