Java Challenge on A Gradient Color Canvas
This is probably an elementary question. However, I have completed reading the 9th Chapter of Java Programming for the Absolute Beginner and have approached the Challenges section. I cannot quite get the progam to show a gradient from dark to light.
I mainly need help with my main method as this is where the problem is located.
The question asks:
"Create a Canvas that paints a gradient that's dark on one side and slowly gets lighter as it moves to the other side."
I do not know JFrame, Swing, Points, JPanels, BufferedImage, or GradientPaint.
Thank you very much for your time and cooperation reagrding this matter.
HERE IS THE CODE:
Code :
import java.awt.*;
public class ColorfulShape extends GUIFrame {
Canvas digital;
public final static int MIN = 0,
MAX = 255;
int r, g, b;
Color paintr = new Color(0, 0, 0);
public ColorfulShape(int r, int g, int b) {
super("Colorful Shape");
r = r >= MIN && r <= MAX ? r : MIN;
g = g >= MIN && g <= MAX ? g : MIN;
b = r >= MIN && b <= MAX ? b : MIN;
digital = new Canvas();
digital.setBackground(new Color(r, g, b));
digital.setSize(200, 150);
add(digital, BorderLayout.CENTER);
pack();
setVisible(true);
}
public static void main(String args[]) {
new ColorfulShape(0, 0, 0);
}
public void paint(Graphics g) {
for(int i=MIN; i<=MAX; i++) {
paintr = paintr.brighter();
}
}
}
Re: Java Challenge on A Gradient Color Canvas
Quote:
I do not know JFrame, Swing, Points, JPanels, BufferedImage, or GradientPaint.
Take a look at the tutorial:
Trail: Creating a GUI With JFC/Swing (The Java™ Tutorials)