📄 compierecolor.java
字号:
* Set Prinary Color
* @param color primary color
*/
protected void setPrimaryColor (Color color)
{
if (color != null)
m_primaryColor = color;
} // setPrimaryColor
/**
* Set CompiereColor from CompiereColor
* @param cc CompiereColor
*/
public void setColor (CompiereColor cc)
{
if (cc == null)
return;
m_type = cc.getType();
//
if (cc.isFlat())
m_primaryColor = cc.getFlatColor();
else if (cc.isGradient())
{
m_primaryColor = cc.getGradientUpperColor();
m_secondaryColor = cc.getGradientLowerColor();
m_startPoint = cc.getGradientStartPoint();
m_repeatDistance = cc.getGradientRepeatDistance();
}
else if (cc.isTexture())
{
setTextureURL(cc.getTextureURL());
m_primaryColor = cc.getTextureTaintColor();
m_compositeAlpha = cc.getTextureCompositeAlpha();
}
else if (cc.isLine())
{
m_primaryColor = cc.getLineBackColor();
m_secondaryColor = cc.getLineColor();
m_lineWidth = cc.getLineWidth();
m_lineDistance = cc.getLineDistance();
}
else
log.severe("Invalid Color");
//
m_dirty = true;
} // setColor
/**************************************************************************
* Fill with CompiereColor Background
* @param g the <code>Graphics</code> context in which to paint
* @param c the component being painted
*/
public void paint (Graphics g, JComponent c)
{
getColorBackground(c).paint (g, c);
} // paint
/**
* Fill with Compiere Background
* @param g graphics
* @param c component
* @param x x pos
* @param y y pos
* @param w with
* @param h height
*/
public void paintRect (Graphics g, JComponent c, int x, int y, int w, int h)
{
getColorBackground(c).paintRect (g,c, x,y, w,h);
} // paintRect
/**
* Get Background
* @param c Componenr
* @return Background
*/
private ColorBackground getColorBackground (JComponent c)
{
if (m_back == null)
{
Rectangle bounds = c.getBounds();
Container container = c.getParent();
while (container != null)
{
bounds = container.getBounds(bounds);
container = container.getParent();
}
m_back = new ColorBackground (bounds);
}
return m_back;
} // getBackground
/**************************************************************************
* String representation
* @return string representation
*/
public String toString()
{
StringBuffer sb = new StringBuffer ("CompiereColor[");
if (isFlat())
sb.append("Flat")
.append(" ").append(CompiereTheme.getColorAsString(getFlatColor()));
else if (isGradient())
sb.append("Gradient")
.append(" Upper=").append(CompiereTheme.getColorAsString(getGradientUpperColor()))
.append(",Lower=").append(CompiereTheme.getColorAsString(getGradientLowerColor()))
.append(",Start=").append(getGradientStartPoint())
.append(",RDistance=").append(getGradientRepeatDistance());
else if (isLine())
sb.append("Line")
.append(" Color=").append(CompiereTheme.getColorAsString(getLineColor()))
.append(",BackColor=").append(CompiereTheme.getColorAsString(getLineBackColor()))
.append(",Width=").append(getLineWidth())
.append(",Distance=").append(getLineDistance());
else if (isTexture())
sb.append("Texture")
.append(" GraphURL=").append(getTextureURL())
.append(",Taint=").append(CompiereTheme.getColorAsString(getTextureTaintColor()))
.append(",Alpha=").append(getTextureCompositeAlpha());
sb.append("]");
return sb.toString();
} // toString
/**
* Parse String Representation and set Attributes
* @param str parse string
*/
private void parseAttributres (String str)
{
if (str.indexOf("[Flat ") != -1)
{
m_type = TYPE_FLAT;
m_primaryColor = CompiereTheme.parseColor(str,
new ColorUIResource(m_primaryColor));
}
else if (str.indexOf("[Gradient ") != -1)
{
m_type = TYPE_GRADIENT;
m_primaryColor = CompiereTheme.parseColor(str.substring(str.indexOf(" Upper=")+7, str.indexOf(",Lower=")),
new ColorUIResource(m_primaryColor));
m_secondaryColor = CompiereTheme.parseColor(str.substring(str.indexOf(",Lower=")+7, str.indexOf(",Start=")),
new ColorUIResource(m_secondaryColor));
m_startPoint = Integer.parseInt(str.substring(str.indexOf(",Start=")+7, str.indexOf(",RDistance=")));
setGradientRepeatDistance(str.substring(str.indexOf(",RDistance=")+11, str.lastIndexOf("]")));
}
else if (str.indexOf("[Line ") != -1)
{
m_type = TYPE_LINES;
m_primaryColor = CompiereTheme.parseColor(str.substring(str.indexOf(" Color=")+7, str.indexOf(",BackColor=")),
new ColorUIResource(m_primaryColor));
m_secondaryColor = CompiereTheme.parseColor(str.substring(str.indexOf(",BackColor=")+11, str.indexOf(",Width=")),
new ColorUIResource(m_secondaryColor));
setLineWidth(str.substring(str.indexOf(",Width=")+7, str.indexOf(",Distance=")));
setLineDistance(str.substring(str.indexOf(",Distance=")+10, str.lastIndexOf("]")));
}
else if (str.indexOf("[Texture ") != -1)
{
m_type = TYPE_TEXTURE;
setTextureURL (str.substring(str.indexOf(" GraphURL=")+10, str.indexOf(",Taint=")));
m_primaryColor = CompiereTheme.parseColor(str.substring(str.indexOf(",Taint=")+7, str.indexOf(",Alpha=")),
new ColorUIResource(m_primaryColor));
setTextureCompositeAlpha (str.substring(str.indexOf(",Alpha=")+7, str.lastIndexOf("]")));
}
} // parseString
/**
* Does the background needs to be redone
* @return true if there were changes
*/
boolean isDirty()
{
return m_dirty;
} // isDirty
/**
* Set Dirty
* @param dirty if true, the background will be re-painted
*/
void setDirty (boolean dirty)
{
m_dirty = dirty;
} // setDirty
/******************************************************************************
* Background contains a Buffered Image with the background.
* The initial size is determined by the constructor.
* It is resized if required when painting.
* <br>
* The Buffered image is a 8-bit RGBA color components packed into integer pixels.
* The image has a DirectColorModel with alpha. The color data in this image
* is considered to be premultiplied with alpha
*/
public class ColorBackground
{
/**
* Create Color Background
* @param bounds Rectangle to fit in
*/
public ColorBackground (Rectangle bounds)
{
createColorBackground (bounds);
fillColorBackground ();
} // Background
private int m_height = 200;
private int m_width = 200;
private BufferedImage m_backImage;
private int m_colorBlind = ColorBlind.getColorType();
/**
* Create Color Background
* @param bounds Rectangle to fit in
*/
private void createColorBackground (Rectangle bounds)
{
m_height = Math.max(bounds.y + bounds.height, m_height);
m_width = Math.max(bounds.x + bounds.width, m_width);
m_backImage = new BufferedImage (m_width, m_height, BufferedImage.TYPE_INT_ARGB_PRE);
} // create Background
/**
* Fill Background with Color
*/
public void fillColorBackground ()
{
Graphics2D g2D = m_backImage.createGraphics();
if (isGradient())
{
Point start = null;
Point end = null;
int r = 1; // repeats
switch (m_startPoint)
{
case SwingConstants.NORTH_WEST:
start = new Point (0, 0);
if (m_repeatDistance > 0)
end = new Point (m_repeatDistance, m_repeatDistance);
// end = new Point (Math.min(m_repeatDistance, m_width), Math.min(m_repeatDistance, height));
else
end = new Point (m_width/r, m_height/r);
break;
case SwingConstants.WEST:
start = new Point (0, m_height/2);
if (m_repeatDistance > 0)
end = new Point (m_repeatDistance, m_height/2);
// end = new Point (Math.min(m_repeatDistance, m_width), m_height/2);
else
end = new Point (m_width/r, m_height/2);
break;
case SwingConstants.SOUTH_WEST:
start = new Point (0, m_height);
if (m_repeatDistance > 0)
end = new Point (m_repeatDistance, m_height-m_repeatDistance);
// end = new Point (Math.min(m_width, m_repeatDistance), Math.max(0, m_height-m_repeatDistance));
else
end = new Point (m_width/r, m_height-(m_height/r));
break;
case SwingConstants.SOUTH:
start = new Point (0, m_height);
if (m_repeatDistance > 0)
end = new Point (0, m_height-m_repeatDistance);
// end = new Point (0, Math.max(0, m_height-m_repeatDistance));
else
end = new Point (0, m_height-(m_height/r));
break;
case SwingConstants.SOUTH_EAST:
start = new Point (m_width, m_height);
if (m_repeatDistance > 0)
end = new Point (m_width-m_repeatDistance, m_height-m_repeatDistance);
// end = new Point (Math.min(0, m_width-m_repeatDistance), Math.max(0, m_height-m_repeatDistance));
else
end = new Point (m_width-(m_width/r), m_height-(m_height/r));
break;
case SwingConstants.EAST:
start = new Point (m_width, m_height/2);
if (m_repeatDistance > 0)
end = new Point (m_width-m_repeatDistance, m_height/2);
// end = new Point (Math.min(0, m_width-m_repeatDistance), m_height/2);
else
end = new Point (m_width-(m_width/r), m_height/2);
break;
case SwingConstants.NORTH_EAST:
start = new Point (m_width, 0);
if (m_repeatDistance > 0)
end = new Point (m_width-m_repeatDistance, m_repeatDistance);
// end = new Point (Math.min(0, m_width-m_repeatDistance), Math.min(m_height, m_repeatDistance));
else
end = new Point (m_width-(m_width/r), m_height/r);
break;
default:
case SwingConstants.NORTH:
start = new Point (0, 0);
if (m_repeatDistance > 0)
end = new Point (0, m_repeatDistance);
// end = new Point (0, Math.min(m_height, m_repeatDistance));
else
end = new Point (0, m_height/r);
}
GradientPaint paint = new GradientPaint(
start,
ColorBlind.getDichromatColor(getGradientUpperColor()),
end,
ColorBlind.getDichromatColor(getGradientLowerColor()),
true); // cyclic
g2D.setPaint(paint);
g2D.fillRect(0, 0, m_width, m_height);
}
else if (isTexture())
{
BufferedImage image = getTextureImage();
if (image == null)
{
g2D.setPaint(ColorBlind.getDichromatColor(getFlatColor()));
g2D.fillRect(0, 0, m_width, m_height);
}
else
{
Rectangle anchor = new Rectangle (0,0, image.getWidth(), image.getHeight());
TexturePaint texture = new TexturePaint (image, anchor);
g2D.setPaint(texture);
g2D.fillRect(0, 0, m_width, m_height);
g2D.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, getTextureCompositeAlpha()));
g2D.setPaint(ColorBlind.getDichromatColor(getTextureTaintColor()));
g2D.fillRect(0, 0, m_width, m_height);
}
}
else if (isLine())
{
// Background
g2D.setPaint(ColorBlind.getDichromatColor(getLineBackColor()));
g2D.fillRect(0, 0, m_width, m_height);
// Lines
g2D.setPaint(ColorBlind.getDichromatColor(getLineColor()));
g2D.setStroke(new BasicStroke(getLineWidth()));
for (int y = 0; y < m_height; y += getLineDistance())
g2D.drawLine(0, y, m_width, y);
}
else // flat
{
g2D.setPaint(ColorBlind.getDichromatColor(getFlatColor()));
g2D.fillRect(0, 0, m_width, m_height);
}
setDirty (false);
} // fillBackground
/**
* Paint/copy background to component
* @param g graphics
* @param c component
*/
public void paint (Graphics g, JComponent c)
{
Rectangle bounds = c.getBounds();
check (bounds);
//
int h = c.getHeight();
int w = c.getWidth();
// Copy Background
g.drawImage (m_backImage,
0, 0, // destination start point
w, h, // destination end point
bounds.x, bounds.y, // source start
bounds.x+w, bounds.y+h, // source end
c);
} // paint
/**
* Paint/copy background to component rectangle
* @param g graphics
* @param c compnent
* @param x x pos
* @param y y pos
* @param w width
* @param h height
*/
public void paintRect (Graphics g, JComponent c, int x, int y, int w, int h)
{
Rectangle bounds = c.getBounds();
check (bounds);
// Copy Background
g.drawImage (m_backImage,
x, y, // destination start point
x+w, h+y, // destination end point
x, y, // source start
x+w, y+h, // source end
c);
} // paint
/**
* Check size of background and repaint if required
* @param bounds Bounds of component
*/
private void check (Rectangle bounds)
{
// Re-Create, if Color Type changed
if (ColorBlind.getColorType() != m_colorBlind)
{
m_colorBlind = ColorBlind.getColorType();
setDirty(true);
}
// we need to create new background
if ((m_height < (bounds.y + bounds.height))
|| (m_width < (bounds.x + bounds.width)))
{
createColorBackground (bounds);
fillColorBackground();
}
else if (isDirty())
fillColorBackground();
} // check
} // ColorBackground
} // CompiereColor
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -