UI button rendering optimization

cusax-fix
Benoit Pradelle 18 years ago
parent 28a3d04a65
commit e6d4e016d6

@ -26,6 +26,10 @@ public class SIPCommButtonUI extends MetalButtonUI {
private final static BufferedImage buttonRolloverBG
= ImageLoader.getImage(ImageLoader.BUTTON_ROLLOVER);
private boolean bufferIsRollover = false;
private BufferedImage paintBuffer = null;
private Component bufferedComponent = null;
// ********************************
// Create PLAF
@ -56,46 +60,69 @@ public void uninstallDefaults(AbstractButton b) {
}
public void paint(Graphics g, JComponent c) {
AntialiasingManager.activateAntialiasing(g);
AbstractButton button = (AbstractButton)c;
ButtonModel model = button.getModel();
BufferedImage leftImg;
BufferedImage middleImg;
BufferedImage rightImg;
int imgWidth;
int imgHeight;
int indentWidth = 10;
if(model.isRollover()){
imgWidth = buttonRolloverBG.getWidth();
imgHeight = buttonRolloverBG.getHeight();
leftImg = buttonRolloverBG.getSubimage(0, 0, indentWidth, imgHeight);
middleImg = buttonRolloverBG.getSubimage(indentWidth, 0,
imgWidth-2*indentWidth,
imgHeight);
rightImg = buttonRolloverBG.getSubimage(imgWidth-indentWidth, 0,
indentWidth, imgHeight);
}
else{
imgWidth = buttonBG.getWidth();
imgHeight = buttonBG.getHeight();
leftImg = buttonBG.getSubimage(0, 0, 10, imgHeight);
middleImg = buttonBG.getSubimage(10, 0, imgWidth-20, imgHeight);
rightImg = buttonBG.getSubimage(imgWidth-10, 0, 10, imgHeight);
// check if the context of the buffer is consistent or else recreate it
if (paintBuffer == null || c != bufferedComponent
|| bufferIsRollover != model.isRollover())
{
// create a buffer in the best available format
paintBuffer = ((Graphics2D) g).getDeviceConfiguration().
createCompatibleImage(c.getWidth(), c.getHeight(),
Transparency.TRANSLUCENT);
// save the context
bufferedComponent = c;
bufferIsRollover = model.isRollover();
// draw in the buffer
BufferedImage leftImg;
BufferedImage middleImg;
BufferedImage rightImg;
int imgWidth;
int imgHeight;
int indentWidth = 10;
if(bufferIsRollover){
imgWidth = buttonRolloverBG.getWidth();
imgHeight = buttonRolloverBG.getHeight();
leftImg = buttonRolloverBG.getSubimage(0, 0, indentWidth,
imgHeight);
middleImg = buttonRolloverBG.getSubimage(indentWidth, 0,
imgWidth-2*indentWidth,
imgHeight);
rightImg = buttonRolloverBG.getSubimage(imgWidth-indentWidth, 0,
indentWidth, imgHeight);
}
else{
imgWidth = buttonBG.getWidth();
imgHeight = buttonBG.getHeight();
leftImg = buttonBG.getSubimage(0, 0, 10, imgHeight);
middleImg = buttonBG.getSubimage(10, 0, imgWidth-20, imgHeight);
rightImg = buttonBG.getSubimage(imgWidth-10, 0, 10, imgHeight);
}
Graphics2D g2 = paintBuffer.createGraphics();
AntialiasingManager.activateAntialiasing(g2);
g2.drawImage(leftImg, 0, 0, indentWidth, c.getHeight(), null);
g2.drawImage(middleImg, indentWidth, 0,
c.getWidth() - 2 * indentWidth, c.getHeight(), null);
g2.drawImage(rightImg, c.getWidth() - indentWidth, 0,
indentWidth, c.getHeight(), null);
}
g.drawImage(leftImg, 0, 0, indentWidth, c.getHeight(), null);
g.drawImage(middleImg, indentWidth, 0,
c.getWidth()-2*indentWidth, c.getHeight(), null);
g.drawImage(rightImg, c.getWidth()-indentWidth, 0,
indentWidth, c.getHeight(), null);
AntialiasingManager.activateAntialiasing(g);
// draw the buffer in the graphics object
g.drawImage(paintBuffer, 0, 0, c.getWidth(), c.getHeight(),
0, 0, c.getWidth(), c.getHeight(), null);
super.paint(g, c);
}

@ -24,6 +24,10 @@ public class SIPCommToggleButtonUI
private final static BufferedImage buttonPressedBG
= ImageLoader.getImage(ImageLoader.TOGGLE_BUTTON_PRESSED);
private boolean bufferIsPressed = false;
private BufferedImage paintBuffer = null;
private Component bufferedComponent = null;
// ********************************
// Create PLAF
// ********************************
@ -52,43 +56,70 @@ public void uninstallDefaults(AbstractButton b) {
public void paint(Graphics g, JComponent c)
{
AntialiasingManager.activateAntialiasing(g);
AbstractButton button = (AbstractButton)c;
ButtonModel model = button.getModel();
BufferedImage leftImg;
BufferedImage middleImg;
BufferedImage rightImg;
boolean isShownPressed = model.isArmed() && model.isPressed()
|| model.isSelected();
int imgWidth;
int imgHeight;
int indentWidth = 10;
if (model.isArmed() && model.isPressed() || model.isSelected())
{
imgWidth = buttonPressedBG.getWidth();
imgHeight = buttonPressedBG.getHeight();
leftImg = buttonPressedBG.getSubimage(0, 0, 10, imgHeight);
middleImg = buttonPressedBG.getSubimage(10, 0, imgWidth-20, imgHeight);
rightImg = buttonPressedBG.getSubimage(imgWidth-10, 0, 10, imgHeight);
}
else
// check if the context of the buffer is consistent or else recreate it
if (paintBuffer == null || c != bufferedComponent
|| bufferIsPressed != isShownPressed)
{
imgWidth = buttonBG.getWidth();
imgHeight = buttonBG.getHeight();
leftImg = buttonBG.getSubimage(0, 0, 10, imgHeight);
middleImg = buttonBG.getSubimage(10, 0, imgWidth-20, imgHeight);
rightImg = buttonBG.getSubimage(imgWidth-10, 0, 10, imgHeight);
}
// create a buffer in the best available format
paintBuffer = ((Graphics2D) g).getDeviceConfiguration().
createCompatibleImage(c.getWidth(), c.getHeight(),
Transparency.TRANSLUCENT);
// save the context
bufferedComponent = c;
bufferIsPressed = isShownPressed;
BufferedImage leftImg;
BufferedImage middleImg;
BufferedImage rightImg;
int imgWidth;
int imgHeight;
int indentWidth = 10;
if (isShownPressed)
{
imgWidth = buttonPressedBG.getWidth();
imgHeight = buttonPressedBG.getHeight();
leftImg = buttonPressedBG.getSubimage(0, 0, 10, imgHeight);
middleImg = buttonPressedBG.getSubimage(10, 0, imgWidth-20,
imgHeight);
rightImg = buttonPressedBG.getSubimage(imgWidth-10, 0, 10,
imgHeight);
}
else
{
imgWidth = buttonBG.getWidth();
imgHeight = buttonBG.getHeight();
leftImg = buttonBG.getSubimage(0, 0, 10, imgHeight);
middleImg = buttonBG.getSubimage(10, 0, imgWidth-20, imgHeight);
rightImg = buttonBG.getSubimage(imgWidth-10, 0, 10, imgHeight);
}
Graphics2D g2 = paintBuffer.createGraphics();
g.drawImage(leftImg, 0, 0, indentWidth, c.getHeight(), null);
g.drawImage(middleImg, indentWidth, 0,
c.getWidth()-2*indentWidth, c.getHeight(), null);
g.drawImage(rightImg, c.getWidth()-indentWidth, 0,
indentWidth, c.getHeight(), null);
AntialiasingManager.activateAntialiasing(g2);
g2.drawImage(leftImg, 0, 0, indentWidth, c.getHeight(), null);
g2.drawImage(middleImg, indentWidth, 0,
c.getWidth()-2*indentWidth, c.getHeight(), null);
g2.drawImage(rightImg, c.getWidth()-indentWidth, 0,
indentWidth, c.getHeight(), null);
}
AntialiasingManager.activateAntialiasing(g);
// draw the buffer in the graphics object
g.drawImage(paintBuffer, 0, 0, c.getWidth(), c.getHeight(),
0, 0, c.getWidth(), c.getHeight(), null);
super.paint(g, c);
}

Loading…
Cancel
Save