如何将(红色,绿色,蓝色)值添加到Java中?例如:
setColor(255, 0, 0);
上下文如下:
public void render() {
BufferStrategy bs = getBufferStrategy();
if (bs == null) {
createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
g.setColor(); // <-- This line
g.fillRect(0, 0, getWidth(), getHeight());
g.dispose();
bs.show();
}
我想使用像RGB(200,200,200)这样的RGB值给我的矩形颜色;那就像灰色。
答案 0 :(得分:15)
您可以使用简单代码获取Color实例:
Color myWhite = new Color(255, 255, 255); // Color white
然后,您可以使用以下内容为对象设置RGB颜色:
g.setColor(myWhite);
希望它可以帮到你!
答案 1 :(得分:2)
或者你可以这样做:
<TreeView>
<TreeView.ItemContainerStyle>
<Style TargetType="TreeViewItem">
<!-- This style changes the item's border appearance -->
<Style.Resources>
<Style TargetType="Border">
<Setter Property="Effect">
<Setter.Value>
<DropShadowEffect Color="Gray" BlurRadius="2"/>
</Setter.Value>
</Setter>
<!-- Set the same corner radius here as in your data template -->
<Setter Property="CornerRadius" Value="8"/>
</Style>
</Style.Resources>
<Setter Property="Margin" Value="2"/>
</Style>
</TreeView.ItemContainerStyle>
<TreeView.ItemTemplate>
<HierarchicalDataTemplate>
<Grid>
<Border x:Name="RoundedBorder" CornerRadius="8">
<!-- Your content here -->
</Border>
</Grid>
<HierarchicalDataTemplate.Triggers>
<DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource FindAncestor, AncestorType=TreeViewItem}}" Value="False">
<!-- Only setting the item's background when it's not selected -->
<Setter TargetName="RoundedBorder" Property="Background" Value="White"/>
</DataTrigger>
</HierarchicalDataTemplate.Triggers>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
例如:
setColor(new Color(r, g, b));
答案 2 :(得分:0)
您可以使用Graphics.setColor
例如:
g.setColor(Color.getHSBColor(255, 79, 18));
答案 3 :(得分:0)
public static final Color VERY_LIGHT_GRAY = new Color(238,238,238);
,然后在paint组件中添加g.setColor(VERY_LIGHT_GRAY);
或者简单地,gc.setColor(new Color(238,238,238));
检查此网站: (https://teaching.csse.uwa.edu.au/units/CITS1001/colorinfo.html)
答案 4 :(得分:0)
你可以试试这个:
bj1.setBackground(new Color(0, 62, 83));
bj2.setForeground(new Color(0, 00, 83));
bj3.setColor(new Color(0, 34, 83));
或者,您也可以将 Color 对象作为参数传递:
Color c = new Color(Color.yellow);
obj4.setBackground(c);