Results 1 to 10 of 10
  1. #1
    armedrabbit is offline Member
    Join Date
    Mar 2011
    Posts
    10
    Rep Power
    0

    Exclamation 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

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    You need to provide more details. I for one have no idea what you are talking about.

  3. #3
    armedrabbit is offline Member
    Join Date
    Mar 2011
    Posts
    10
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    You need to provide more details. I for one have no idea what you are talking about.
    okay im sorry!

    what i mean to say is when i run this program it prints a pattern of red and clear. what i want it to print is a pattern of red and green, how can fix my program so it does that?

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    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
            }
        }
    }

  5. #5
    armedrabbit is offline Member
    Join Date
    Mar 2011
    Posts
    10
    Rep Power
    0

    Default

    im so bad with if statements =/. is there way to just set my boolean method colors to green?

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    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.

  7. #7
    armedrabbit is offline Member
    Join Date
    Mar 2011
    Posts
    10
    Rep Power
    0

    Default

    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

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    Quote Originally Posted by armedrabbit View Post
    so would i use the else statement after my for statement
    No, after the if statement.
    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
    
            }
        }
    }
    By the way I strongly urge you to always use brackets around loops and if statements.

  10. #10
    armedrabbit is offline Member
    Join Date
    Mar 2011
    Posts
    10
    Rep Power
    0

Similar Threads

  1. Can't change the color of a button...
    By mrbeast87 in forum AWT / Swing
    Replies: 5
    Last Post: 11-16-2010, 09:22 PM
  2. Change color of a region
    By sky in forum AWT / Swing
    Replies: 5
    Last Post: 11-24-2009, 03:47 PM
  3. Color Change of data
    By Java.child in forum AWT / Swing
    Replies: 20
    Last Post: 02-12-2009, 06:51 AM
  4. How to Change the color of MultiColumnListBox
    By Java.child in forum AWT / Swing
    Replies: 1
    Last Post: 01-22-2009, 12:07 AM
  5. How to change string Color
    By Java.child in forum AWT / Swing
    Replies: 3
    Last Post: 01-06-2009, 04:27 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •