Results 1 to 8 of 8

Thread: [SOLVED] Change the color by button.

Threaded View

  1. #1

    Thread Starter
    Hyperactive Member gjon's Avatar
    Join Date
    Nov 2004
    Location
    Inescapable Void
    Posts
    442

    Resolved [SOLVED] Change the color by button.

    I am currently reading learn java in 21 days and I am trying to combine two examples from the book to create an applet that lets you draw on the page. I put into it the buttons example that lets you change colors of the background, but what I am trying to do is instead of changing the background, change the color to draw with.

    I tried making it so that the if statements would change the color of the graphics object g but I believe since it is in a different method, paint, that it just won't work this way. I then also tried to keep it changing the foreground color in hopes it would change the color used to paint with, but still no go.

    If someone has the time, could you please explain what needs to happen in order for the color during paint to change to what button is pressed?

    Everything looks like its good up until the point where the color needs to change after the button is pressed. Or so I believe.

    VB Code:
    1. import java.awt.*;
    2.  
    3.   public class DrawingApplet extends java.applet.Applet {
    4.  
    5.       final int MAXSPOTS = 100350;
    6.       int xspots[] = new int[MAXSPOTS];
    7.       int yspots[] = new int[MAXSPOTS];
    8.      int currspots = 0;
    9.  
    10.      public void init() {
    11.          setBackground(Color.white);
    12.      add(new Button("Red"));
    13.      add(new Button("Blue"));
    14.     add(new Button("Green"));
    15.     add(new Button("White"));
    16.     add(new Button("Black"));
    17.      }
    18.  
    19.      public boolean mouseDown(Event evt, int x, int y) {
    20.          if (currspots < MAXSPOTS) {
    21.             addspot(x,y);
    22.             return true;
    23.         }
    24.         else {
    25.             System.out.println("Too many spots.");
    26.             return false;
    27.         }
    28.     }
    29.  
    30.  
    31. public boolean mouseDrag(Event evt, int x, int y) {
    32.     addspot(x,y);
    33.             return true;
    34. }
    35.  
    36.   public boolean action(Event evt, Object arg) {
    37.     if (evt.target instanceof Button) {
    38.       changeColor((String)arg);
    39.       return true;
    40.     } else return false;
    41.   }
    42.  
    43.      void addspot(int x,int y) {
    44.          xspots[currspots] = x;
    45.           yspots[currspots] = y;
    46.         currspots++;
    47.          repaint();
    48.      }
    49.      
    50.     public void paint(Graphics g) {
    51.         g.setColor(Color.blue);
    52.          for (int i = 0; i < currspots; i++) {
    53.               g.fillOval(xspots[i] - 5, yspots[i] - 5, 10, 10);
    54.         }
    55.     }
    56.  
    57. void changeColor(String bname) {
    58.     if (bname.equals("Red")) setForeground(Color.red);
    59.     else if (bname.equals("Blue")) setForeground(Color.blue);
    60.     else if (bname.equals("Green")) setForeground(Color.green);
    61.     else if (bname.equals("White")) setForeground(Color.white);
    62.     else setForeground(Color.black);
    63.  
    64.     repaint();
    65.   }
    66.  
    67.  }
    Last edited by gjon; Jun 1st, 2006 at 11:52 PM.

Posting Permissions

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



Click Here to Expand Forum to Full Width