GradientTexture problem

So, I like to redo code from memory from the previous day, for practice. I redid this bit this morning and added a list to change things up.


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

class DrawPanel extends JPanel
{
	public void paintComponent(Graphics g)
	{
		Graphics2D g2d = (Graphics2D) g;
		ArrayList<Integer> rnd = new ArrayList<Integer>();
		g.fillRect(0, 0, this.getWidth(), this.getHeight());
		
		for(int i = 0; i < 6; i++)
		{
			rnd.add((int)(Math.random() * 256));
		}
		int red = rnd.get(0);
		int blue = rnd.get(1);
		int green = rnd.get(2);
		Color randomColor = new Color(red, blue, green);
		red = rnd.get(3);
		blue = rnd.get(4);
		green = rnd.get(5);
		Color randomColor2 = new Color(red, blue, green);
		
		GradientPaint gradient = new GradientPaint(75, 75, randomColor, 150, 150, randomColor2);
		g2d.setPaint(gradient);
		g2d.fillOval(125, 125, 100, 100);
	}
}

Compiles just fine, but the color is not gradient. In the next example, it does what I expect it to do.


import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;

class DrawPanel extends JPanel
{
	public void paintComponent(Graphics g)
	{
		Graphics2D g2d = (Graphics2D) g;
		
		g.fillRect(0, 0, this.getWidth(), this.getHeight());
		
		int red = (int)(Math.random() * 256);
		int blue = (int)(Math.random() * 256);
		int green = (int)(Math.random() * 256);
		
		Color randomColor = new Color(red, blue, green);
		
		red = (int)(Math.random() * 256);
		blue = (int)(Math.random() * 256);
		green = (int)(Math.random() * 256);
		Color randomColor2 = new Color(red, blue, green);
		
		GradientPaint gradient = new GradientPaint(75, 75, randomColor, 150, 150, randomColor2);
		g2d.setPaint(gradient);
		g2d.fillOval(75, 75, 100, 100);
	}
}

I put in println’s after the color’s to make sure they were changing. In both examples, they displayed variation in numbers, but only the original works correctly. Is there something the list is doing that i’m not aware of?

JFrame Code:


public class WokeUp implements ActionListener
{
	JFrame frame;
	JButton colorButton;
	
	public static void main(String[] args)
	{
		WokeUp gui = new WokeUp();
		gui.go();
	}
	
	public void go()
	{
		frame = new JFrame();
		DrawPanel panel = new DrawPanel();
		colorButton = new JButton("Click me");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		colorButton.addActionListener(this);
		
		frame.getContentPane().add(BorderLayout.SOUTH, colorButton);
		frame.getContentPane().add(BorderLayout.CENTER, panel);
		
		frame.setSize(300, 300);
		frame.setVisible(true);
	}
	
	public void actionPerformed(ActionEvent event)
	{
		frame.repaint();
	}
}

The arguments of gradient and fillOval do not match.

My eye was caught by the same thing that Kefwar points out. You’ll only see the change from 125 to 150 (1/4th of the gradient change). It should look mostly like your second color.

By the way, you can post a screen shot jpg at MYFILES and link it, in order to show us exactly what the results are.

Wow, I feel stupid. Didn’t even bother to think about the arguments I was passing into the gradientpaint. Was only concerning myself with the color values and forgot the other numbers represented x/y positions. Thank you for the help.