Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Java Challenge on A Gradient Color Canvas

  1. #1
    Banned
    Join Date
    Nov 2011
    Posts
    0
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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:
    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();
    	}
    	}
    }
    Last edited by Norm; January 2nd, 2013 at 08:43 AM. Reason: added code tags


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Java Challenge on A Gradient Color Canvas

    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)
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Programming challenge
    By pierce21 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 26th, 2012, 07:29 AM
  2. Why does the Color class have two variables for each color?
    By Melawe in forum Java Theory & Questions
    Replies: 5
    Last Post: May 10th, 2012, 04:21 PM
  3. text color changer based on words/ word classification by color
    By knoxy5467 in forum Java Theory & Questions
    Replies: 25
    Last Post: June 15th, 2011, 07:52 AM
  4. Java 3D Problems - Canvas 3d and QuadArray rendering
    By minime12358 in forum What's Wrong With My Code?
    Replies: 14
    Last Post: May 27th, 2011, 08:00 AM
  5. Gradient Translucent Window
    By Duvan in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 4th, 2011, 01:43 PM

Tags for this Thread