I am having a problem changing the color of the diamond I had created when I am using methods in java. I don't know how to inherit or to correct it by using methods. I just want it to become object oriented I don't know how to properly do it. Please help me.
package Trial;
import javax.swing.*;
import java.awt.*;
public class ColorRed extends JApplet {
public GradientPaint gradientPaint;
public void paint(Graphics g){
super.paint(g);
Graphics2D g2 = (Graphics2D)g;
GradientPaint black = new GradientPaint(50,20,Color.BLACK,50,50,Color.BLACK);
blackDiamond(g2,black);
GradientPaint yellowOrange = new GradientPaint(50,20,Color.YELLOW,50,50,Color.RED);
redDiamond(g2,yellowOrange);
}
public void blackDiamond(Graphics2D g2,GradientPaint gradientPaint){
int a [] = {100,50,100,150,100};
int b [] = {10,60,110,60,10};
setGradientPaint(gradientPaint);
g2.setPaint(getGradientPaint());
fillPolygon(a,b,5,g2);
}
public void redDiamond(Graphics2D g2,GradientPaint gradientPaint){
int a2 [] = {100,60,100,140,100};
int b2 [] = {20,60,100,60,20};
setGradientPaint(gradientPaint);
g2.setPaint(getGradientPaint());
fillPolygon(a2,b2,5,g2);
}
public void fillPolygon(int a [], int b [] ,int c,Graphics2D g2){
getGraphics().fillPolygon(a,b,c);
}
public GradientPaint getGradientPaint() {
return gradientPaint;
}
public void setGradientPaint(GradientPaint gradientPaint) {
this.gradientPaint = gradientPaint;
}
}
This is the code that I try to create object-oriented but it doesn't inherit the color of gradient paint.
That is the result of my code
This the code that not yet create multiple methods
import javax.swing.*;
import java.awt.*;
public class ColorRed extends JApplet {
public GradientPaint gradientPaint;
public void paint(Graphics g){
super.paint(g);
Graphics2D g2 = (Graphics2D)g;
int a [] = {100,50,100,150,100};
int b [] = {10,60,110,60,10};
g.fillPolygon(a,b,5);
GradientPaint red = new GradientPaint(50,10,Color.RED,10,70,Color.ORANGE);
g2.setPaint(red);
int a2 [] = {100,60,100,140,100};
int b2 [] = {20,60,100,60,20};
g.fillPolygon(a2,b2,5);
}
}
This is the result that I want: enter image description here
I just want to correct it to organize so that I wont declare all the local variables in paint method I just want to seperate it in different methods the problem is that the Color doesn't change. Please help me thanks a lot. :)
Maybe organize it like this, so that the color is easily changed:
Unfortunately I didn't locate an online swing runner to test it.