Results 1 to 10 of 10
- 04-20-2011, 12:11 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 10
- Rep Power
- 0
PLEASE HELP! how do i change the color of a pattern?
Hi how can i change the color of my pattern, this is the code i have so far
import java.awt.*;
import javax.swing.*;
public class Pattern extends JComponent
{
public static final int GRID_SIZE = 20;
public static final int ROWS = 10;
public static final int COLUMNS = 10;
public boolean fill(int row, int column)
{
// Change the Boolean expression to produce different patterns
return !(row == 0 && column == 0 || row == 1 && column == 0 || row == 2 && column == 0 || row == 3 && column == 0 || row == 4 && column == 0 || row == 5 && column == 0 || row == 6 && column == 0 || row == 7 && column == 0 || row == 8 && column == 0 || row == 9 && column == 0 || row == 0 && column == 3 || row == 1 && column == 3 || row == 2 && column == 3 || row == 3 && column == 3 || row == 4 && column == 3 || row == 5 && column == 3 || row == 6 && column == 3 || row == 7 && column == 3 || row == 8 && column == 3 || row == 9 && column == 3);
}
public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.RED);
Pattern pattern = new Pattern();
for (int i = 0; i < ROWS; i++)
for (int j = 0; j < COLUMNS; j++)
if (pattern.fill(i, j))
g2.fill(new Rectangle(
j * GRID_SIZE,
i * GRID_SIZE,
GRID_SIZE - 1,
GRID_SIZE - 1));
}
public Dimension getPreferredSize()
{
return new Dimension(GRID_SIZE * COLUMNS,
GRID_SIZE * ROWS);
}
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.add(new Pattern());
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
frame.setVisible(true);
}
}
How do i change it so that the clear part would turn green, instead of it being clear. That way i can create a christmas color pattern? please help, im very new to java and im really stressing to figure it out
- 04-20-2011, 12:19 AM #2
You need to provide more details. I for one have no idea what you are talking about.
- 04-20-2011, 12:21 AM #3
Member
- Join Date
- Mar 2011
- Posts
- 10
- Rep Power
- 0
- 04-20-2011, 12:29 AM #4
Taking a stab in the dark here:
Java Code:loop { loop { if ..... { set color to red draw rectangle } else { set color to green draw rectangle } } }
- 04-20-2011, 12:43 AM #5
Member
- Join Date
- Mar 2011
- Posts
- 10
- Rep Power
- 0
im so bad with if statements =/. is there way to just set my boolean method colors to green?
- 04-20-2011, 12:52 AM #6
Huh?
You already have the if statement in your code to do the red rectangles. Simply add the else statement to do the green rectangles.
- 04-20-2011, 02:31 AM #7
Member
- Join Date
- Mar 2011
- Posts
- 10
- Rep Power
- 0
so would i use the else statement after my for statement
like this
for (int i = 0; i < ROWS; i++)
for (int j = 0; j < COLUMNS; j++)
if (pattern.fill(i, j))
g2.fill(new Rectangle(
j * GRID_SIZE,
i * GRID_SIZE,
GRID_SIZE - 1,
GRID_SIZE - 1));
else
- 04-20-2011, 03:57 AM #8
- 04-20-2011, 04:18 AM #9
No, after the if statement.
By the way I strongly urge you to always use brackets around loops and if statements.Java Code:Graphics2D g2 = (Graphics2D) g; g2.setColor(Color.RED); // delete this line Pattern pattern = new Pattern(); for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLUMNS; j++) { if (pattern.fill(i, j)) { // set colour g2.fill(new Rectangle(j * GRID_SIZE, i * GRID_SIZE, GRID_SIZE - 1, GRID_SIZE - 1)); } else { // add else here } } }
- 04-20-2011, 08:51 PM #10
Member
- Join Date
- Mar 2011
- Posts
- 10
- Rep Power
- 0
Similar Threads
-
Can't change the color of a button...
By mrbeast87 in forum AWT / SwingReplies: 5Last Post: 11-16-2010, 09:22 PM -
Change color of a region
By sky in forum AWT / SwingReplies: 5Last Post: 11-24-2009, 03:47 PM -
Color Change of data
By Java.child in forum AWT / SwingReplies: 20Last Post: 02-12-2009, 06:51 AM -
How to Change the color of MultiColumnListBox
By Java.child in forum AWT / SwingReplies: 1Last Post: 01-22-2009, 12:07 AM -
How to change string Color
By Java.child in forum AWT / SwingReplies: 3Last Post: 01-06-2009, 04:27 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks