How can I duplicate my code simply? - java

I have the code below; I want it also want the code in the 'action performed' to happen when it is hovered over. Instead of copy and pasteing the code again and have twice as many times, Is there a way to set it easily?
The code:
private void btnGreenActionPerformed(java.awt.event.ActionEvent evt) {
Color btnGrn = new Color(159, 191, 143); //Sets the colour to a class
Color txtGrn = new Color(201, 255, 191); //Sets the colour to a class
Color txtGry = new Color(89, 89, 89); //Sets the colour to a class
this.getContentPane().setBackground(new java.awt.Color(127,191,95)); //Sets background color to green
btnConvert.setBackground(btnGrn); //Changes the colors to green
btnReset.setBackground(btnGrn); //Changes the colors to green
btnClose.setBackground(btnGrn); //Changes the colors to green
btnInfo.setBackground(btnGrn); //Changes the colors to green
txtIncome.setBackground(txtGrn); //Changes the colors to green
txtPayable.setBackground(txtGrn); //Changes the colors to green
txtStatus.setBackground(txtGrn); //Changes the colors to green
txtIncome.setForeground(txtGry); //Changes the colors to grey
txtPayable.setForeground(txtGry); //Changes the colors to grey
txtStatus.setForeground(txtGry); //Changes the colors to grey
}
Note: I have 7 buttons that are all the same except for the color values.

I would add an inner class to your panel class, to look after the colours, then create as many instances of it as you have colour schemes, including whatever colours you like. Something like this.
This is all inside your panel class, by the way. I'm not envisaging this as a stand-alone class in its own .java file; because it's entirely bound to the characteristics of the panel. A more re-usable version of it would look a bit different.
private class ButtonColorScheme {
final Color paneBackground;
final Color buttonBackground;
final Color textBackground;
final Color textForeground;
ButtonColorScheme(Color paneBackground, Color buttonBackground, Color textBackground, Color textForeground) {
this.paneBackground = paneBackground;
this.buttonBackground = buttonBackground;
this.textBackground = textBackground;
this.textForeground = textForeground;
}
void apply() {
getContentPane().setBackground(paneBackground);
btnConvert.setBackground(buttonBackground);
btnReset.setBackground(buttonBackground);
btnClose.setBackground(buttonBackground);
btnInfo.setBackground(buttonBackground);
txtIncome.setBackground(textBackground);
txtPayable.setBackground(textBackground);
txtStatus.setBackground(textBackground);
txtIncome.setForeground(textForeground);
txtPayable.setForeground(textForeground);
txtStatus.setForeground(textForeground);
}
}
private final ButtonColorScheme greenAndGrey = new ButtonColorScheme(
new Color(127,191,95), new Color(159, 191, 143), new Color(201, 255, 191), new Color(89, 89, 89));
private final ButtonColorScheme redAndBlack = new ButtonColorScheme(
new Color(191,120,95), new Color(202, 160, 143), new Color(255, 180, 191), Color.BLACK);
public void btnGreenActionPerformed(ActionEvent evt){
greenAndGrey.apply();
}
public void btnRedActionPerformed(ActionEvent evt){
redAndBlack.apply();
}

Just create a method,
private void setWidgetColors(Color ... colours){
int i=0;
Color btnGrn = i++ <= colours.length?colours[i-1]:new Color(159, 191, 143); //Sets the colour to a class
Color txtGrn = i++ <= colours.length?colours[i-1]:new Color(201, 255, 191); //Sets the colour to a class
Color txtGry = i++ <= colours.length?colours[i-1]:new Color(89, 89, 89);
//Sets the colour to a class
this.getContentPane().setBackground(i++ <= colours.length ? colours[3] : new Color(127,191,95)); //Sets background color to green
btnConvert.setBackground(btnGrn); //Changes the colors to green
btnReset.setBackground(btnGrn); //Changes the colors to green
btnClose.setBackground(btnGrn); //Changes the colors to green
btnInfo.setBackground(btnGrn); //Changes the colors to green
txtIncome.setBackground(txtGrn); //Changes the colors to green
txtPayable.setBackground(txtGrn); //Changes the colors to green
txtStatus.setBackground(txtGrn); //Changes the colors to green
txtIncome.setForeground(txtGry); //Changes the colors to grey
txtPayable.setForeground(txtGry); //Changes the colors to grey
txtStatus.setForeground(txtGry); //Changes the colors to grey
}
And then
private void btnGreenActionPerformed(ActionEvent evt){
Color btnGrn = new Color(159, 191, 143); //Sets the colour to a class
Color txtGrn = new Color(201, 255, 191); //Sets the colour to a class
Color txtGry = new Color(89, 89, 89);
Color backgroundColor = new Color(127,191,95);
setWidgetColors(btnGrn,txtGrn,txtGry,backgroundColor);
}

Methods are quite useful in such cases. Place this code into a method.
Add whatever parameters needed (e.g. you may decide to turn these values
159, 191, 143, etc. into parameters of the method say named r,g,b). Then
just call your method with the arguments you need (e.g. with r=166, g=202, b=192).

Related

From RGB to HSV (Color.RGBtoHSB) in Java returns a different result

I'm trying to change the color of some images in JavaFX. If, for example, I insert these RGB values (185, 74, 72) I get a different result. I checked the RGB result with Paint and it is (205, 183, 183). Anyone of you knows why?
This is the code:
VBox icon = new VBox();
HBox cell = new HBox(5);
Circle circle = new Circle(12, 12, 18);
ImageView iv = new ImageView(ICON_URL);
ColorAdjust ca = new ColorAdjust();
float[] hsb = new float[3];
Color.RGBtoHSB(185, 74, 72, hsb);
ca.setHue(hsb[0]);
ca.setSaturation(hsb[1]);
ca.setBrightness(hsb[2]);
iv.setClip(circle);
iv.setEffect(ca);
icon.getChildren().addAll(iv);
cell.getChildren().addAll(icon);
I tried the code below but got white instead of blue when i set the contrast to 1.0
//Instantiating the ColorAdjust class
ColorAdjust colorAdjust = new ColorAdjust();
// https://stackoverflow.com/questions/37561747/from-rgb-to-hsv-color-rgbtohsb-in-java-returns-a-different-result
//Setting the contrast value
colorAdjust.setContrast(1.0);
//Setting the hue value
colorAdjust.setHue(color.getHue());
//Setting the brightness value
colorAdjust.setBrightness(color.getBrightness());
//Setting the saturation value
colorAdjust.setSaturation(color.getSaturation());
//Applying color adjust effect to the ImageView node
imageView.setEffect(colorAdjust);
I then experimented with the code in to see that png and even svg did not work properly. So I ende up
https://stackoverflow.com/a/51726678/1497139

how does a swing timer work?

hello i am having trouble trying to understand swing timers. to help me could someone show me a simple flickering animation? i have looked arround on the internet but still dont fully understand how they work. it would be so helpful if someone could give me an example like this:
say if i created a circle:
g.setColor(colors.ORANGE);
g.fillOval(160, 70, 50, 50);
how could i then use a swing timer to change the color from orange to say Gray using a swing timer with a delay?
thank you so much for helping me understand :)
First of all, you wouldn't hard-code your color use like this:
g.setColor(colors.ORANGE);
g.fillOval(160, 70, 50, 50);
Since this prevents all ability to change the color's state. Instead use a class field to hold the Color used, and call it something like ovalColor:
private Color ovalColor = SOME_DEFAULT_COLOR; // some starting color
And then use that color to draw with:
g.setColor(ovalColor);
g.fillOval(160, 70, 50, 50);
I'd then give my class an array of Color or ArrayList<Color> and an int index field:
private static final Color[] COLORS = {Color.black, Color.blue, Color.red,
Color.orange, Color.cyan};
private int index = 0;
private Color ovalColor = COLORS[index]; // one way to set starting value
Then in the Swing Timer's ActionListener I'd increment the index, I'd mod it by the size of the array or of the ArrayList, I'd get the Color indicated by the index and call repaint();
index++;
index %= COLORS.length;
ovalColor = COLORS[index];
repaint();
Also here's a somewhat similar example.
Also please look at the Swing Timer Tutorial.
maybe this would help:
public class object{
Color color = Color.GREEN;
Timer timer;
public object() {
timer = null;
timer = new Timer(5000, new ActionListener(){
public void actionPerformed(ActionEvent e) {
if (color.equals(Color.GREEN)) {
color = Color.RED;
timer.setDelay(2000);
} else {
color = Color.GREEN;
timer.setDelay(8000);
}
repaint();
}
});
timer.start();}}
I think a paint method would work.like this:
public void paint(Graphics g){
super.paint(g);
g.setColor(Color.green);
g.filloval(30,40,50,50);
}

JFreeChart Bar chart custom color?

I am using JFreeCharts in java to create a bar chart. My question is fairly simple... how can I choose a custom color for all of the bars in a bar chart? I'm not sure if this customization would be done in a GradientPaint. An example of my code that determines bar color is:
final GradientPaint gp0 = new GradientPaint(
0.0f, 0.0f, Color.blue,
0.0f, 0.0f, Color.blue
);
I'm not sure if this is the right way to go for custom colors or not. Basically, I don't know if GradientPaint is the right way to go or not. If it is, could someone let me know how I could edit this code to make it a custom color rather than blue?
I'm not sure if this helps, but say the information for the custom color was
hue: 142
Sat: 109
Lum:126
Red: 79
Green: 129
Blue: 189
With this is there a way to customize the color of the chart?
It was a while since i coded with jfreechart.Bud if i remember corectly this was code that i wrote to change bar paint ;).
CategoryPlot cplot = (CategoryPlot)chart.getPlot();
cplot.setBackgroundPaint(SystemColor.inactiveCaption);//change background color
//set bar chart color
((BarRenderer)cplot.getRenderer()).setBarPainter(new StandardBarPainter());
BarRenderer r = (BarRenderer)chart.getCategoryPlot().getRenderer();
r.setSeriesPaint(0, Color.blue);
Im looking at the code for my first application ever written.Im not sure if it will work now.
For future i recommend to google out or purchase PDF guide to jfreechart.You find all the references and samples there.Bud if you can ,skip to JavaFX i strongly recommend it ,working with jfreechart is pain.To be honest.Implementing charts in javafx is easy and looks way better ;)
CategoryPlot plot = chart.getCategoryPlot();
BarRenderer renderer = (BarRenderer) plot.getRenderer();
// set the color (r,g,b) or (r,g,b,a)
Color color = new Color(79, 129, 189);
renderer.setSeriesPaint(0, color);
This will set all bars to that specific color. If you would like the colors to change for each row (say, for a stacked bar chart), you can call dataset.getRowCount(), with dataset being of type CategoryDataset, to return to you the number of rows involved for each column of the bar chart. Then, you could index the series in the renderer.setSeriesPaint() call based on the index of the row.
for (int i = 0; i < dataset.getRowCount(); i++){
switch (i) {
case 0:
// red
color = new Color(255, 0, 0);
break;
case 1:
// blue
color = new Color(0, 0, 255);
break;
default:
// green
color = new Color(0, 255, 0);
break;
}
}
Custom Colors in Bar Chart using JfreeChart
CategoryItemRenderer barColor = new CustomRenderer(new Paint[]{});
plot.setRenderer(barColor);
create a new class name is CustomRenderer extends BarRenderer3D or you choose BarRenderer
class CustomRenderer extends BarRenderer3D {
private Paint[] colors;
public CustomRenderer(final Paint[] colors) {
this.colors = colors;
}
public Paint getItemPaint(final int row, final int column) {
if(column==0)
return Color.blue;
else if(column==1)
return Color.CYAN;
else
return Color.RED;
}
}
I think easiest way is using getRenderer().setSeriesPaint(index, color) method.
So as an example you can try the below code for a bar chart which has 3 bars grouped.
JFreeChart barChart = ChartFactory.createBarChart(
"Bar Chart Titke",
"Category", "Score",
dataset,PlotOrientation.HORIZONTAL,
true, true, false);
CategoryPlot plot = barChart.getCategoryPlot();
plot.getRenderer().setSeriesPaint(0, new Color(128, 0, 0));
plot.getRenderer().setSeriesPaint(1, new Color(0, 0, 255));
plot.getRenderer().setSeriesPaint(2, new Color(0, 230, 255));

Java - How can I assign lots of colors simply?

I just wanted to know if there was a way of making this code simpler.
Thanks.
private void btnBlueActionPerformed(java.awt.event.ActionEvent evt) {
btnConvert.setBackground(new java.awt.Color(84, 209, 241)); //Changes the colors to blue
btnReset.setBackground(new java.awt.Color(84, 209, 241)); //Changes the colors to blue
btnClose.setBackground(new java.awt.Color(84, 209, 241)); //Changes the colors to blue
btnInfo.setBackground(new java.awt.Color(84, 209, 241)); //Changes the colors to blue
txtIncome.setBackground(new java.awt.Color(127, 228, 255)); //Changes the colors to blue
txtPayable.setBackground(new java.awt.Color(127, 228, 255));//Changes the colors to blue
txtStatus.setBackground(new java.awt.Color(127, 228, 255)); //Changes the colors to blue
txtIncome.setForeground(new java.awt.Color(89, 89, 89)); //Changes the colors to blue
txtPayable.setForeground(new java.awt.Color(89, 89, 89)); //Changes the colors to blue
txtStatus.setForeground(new java.awt.Color(89, 89, 89)); //Changes the colors to blue
}
Instead of re-creating the same color over and over you could just create each one once, and then assign them as necessary:
Color btnColor = new Color(84, 209, 241);
Color txtColor1 = new Color(127, 228, 255);
Color textcolor2 = new Color(89, 89, 89);
Then:
private void btnBlueActionPerformed(java.awt.event.ActionEvent evt) {
btnConvert.setBackground(btnColor);
btnReset.setBackground(btnColor);
btnClose.setBackground(btnColor);
btnInfo.setBackground(btnColor);
txtIncome.setBackground(txtColor1);
txtPayable.setBackground(txtColor1);
txtStatus.setBackground(txtColor1);
txtIncome.setForeground(textcolor2);
txtPayable.setForeground(textcolor2);
txtStatus.setForeground(textcolor2);
}
Looks like your objects should actually be parts of collections that are grouped together.
// assuming "mainMenuButtons" (or whatever) contains your first four objects
// note I don't know what color (84, 209, 241) actually is
for(Button btn : mainMenuButtons) {
btn.setBackground(new java.awt.Color(84, 209, 241));
}
Secondly those colors are immutable. There's no need to create so many objects
Color reddish = new java.awt.Color(84, 209, 241);
// then just use reddish everywhere instead of creating a new color each time.
To simplify both the code and changes you may want to make in the future, do the following:
Create a container for components that should share the same foreground or background
Create a container for colors (indexed by state)
Make a small ComponentUtil Class that lets you change colors for all members of a container
When an action changes the state of the components, you pick up the correct color for the component and call ComponentUtil.setBackground(components, color); or ComponentUtil.setForeground(components, color);
import java.awt.Color;
import java.awt.Component;
public class ComponentUtil {
public static void setBackground(Component[] components, Color color) {
for (int i=0; i < components.length; i++) {
components[i].setBackground(color);
}
}
public static void setForeground(Component[] components, Color color) {
for (int i=0; i < components.length; i++) {
components[i].setForeground(color);
}
}
}

How to add resizing slider to rectangle shape in java?

Here is my rectangle shape code:
if(rect){
gphcs.setColor(Color.BLUE);
if(orangeshp)
gphcs.setColor(Color.ORANGE);
if(greenshp)
gphcs.setColor(Color.GREEN);
gphcs.fillRect(20,35,100,30);
}
I want to to add a resizing slider in order to change size of the rectangle.
Here is a sample pic of the slider I want to add:
I just need any simple code to create this slider.
Thanks for your time..
JSlider size = new JSlider(JSlider.HORIZONTAL, 0, 250, 100);
JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 250, 100);
slider.addChangeListener(this);
public void stateChanged (ChangeEvent event){
JSlider slider = (JSlider)event.getSource();
int value = slider.getValue();
//manipulate the value in the proption you wants to increase your coordinate of rectangle
//change the size of rectangle here
}

Categories

Resources