Results 1 to 4 of 4

Thread: Need help with a programing assignment in Java! +Rep!

Hybrid View

  1. #1

    Joined
    Jul 2012
    Posts
    717
    Userbars
    3
    Thanks
    105
    Thanked
    201/114
    DL/UL
    24/0
    Mentioned
    88 times
    Time Online
    22d 22h 4m
    Avg. Time Online
    7m

    Need help with a programing assignment in Java! +Rep!

    I signed up for a Java class and to be honest I've gotten lost already. Here is the assignment; I do NOT want you to simply do it because I know it would take a real programmer all of two seconds to do. I want you to help EXPLAIN this assignment to me and help me understand so I can be prepared for future assignments and tests. I assume you already have a program to run whatever, but if not here is a link to the one we are using. It's free. (you need an account to see links)

    Now, here is the assignment. I have to make three colored buttons. Each button is a switch that turns the background to a different color. Here is a picture of what pressing some buttons looks like:


    I've written code to make the shapes and colors right but I need help setting up variables and whatnot. Most likely what I have written is totally wrong in terms of what the finished program should look like, but visually it looks right (size/colors of squares; have not yet made the borders bold.)

    This project is due tuesday but I would really like to get some understanding of it today. I have some "hints" given too that I can write up if it will help. Thanks!


  2. #2
    Zachafer's Avatar
    Joined
    Dec 2011
    Posts
    1,235
    Userbars
    11
    Thanks
    769
    Thanked
    1,466/678
    DL/UL
    98/0
    Mentioned
    512 times
    Time Online
    24d 13h 9m
    Avg. Time Online
    8m
    Use a MouseListener...

    Code:
    public class HomeworkAssignment implements MouseListener {
        public HomeworkAssignment()
        {
                button1.addMouseListener(this);
                button2.addMouseListener(this);
                button3.addMouseListener(this);
                addMouseListener(this);
                //add paint code
        }
    
        public void mousePressed(MouseEvent e) {
            if(e.getSource() == button1)
            {
                //change color according to button1
            }
        }
    }

  3. #3

    Joined
    Jul 2012
    Posts
    717
    Userbars
    3
    Thanks
    105
    Thanked
    201/114
    DL/UL
    24/0
    Mentioned
    88 times
    Time Online
    22d 22h 4m
    Avg. Time Online
    7m
    Quote Originally Posted by Zachafer View Post
    Use a MouseListener...

    Code:
    public class HomeworkAssignment implements MouseListener {
        public HomeworkAssignment()
        {
                button1.addMouseListener(this);
                button2.addMouseListener(this);
                button3.addMouseListener(this);
                addMouseListener(this);
                //add paint code
        }
    
        public void mousePressed(MouseEvent e) {
            if(e.getSource() == button1)
            {
                //change color according to button1
            }
        }
    }
    Thank you for the help but I have no idea what a mouse listener is or how to use one. I am in an introductory class and what I am mainly looking for is a way to make the program recognize that the mouse is being pressed in a certain area (the squares) and to then do something (change border of square and background color) because of this.

    ---------- Post added at 06:23 PM ---------- Previous post was at 06:05 PM ----------

    Here's another attempt, based on modifying a switch example that was given:

    Code:
    boolean switchPressed;
    boolean on = false;
    int x, y, w, h; 
    
    void setup() {
      size(500,500);
      w = width/5;
      h = width/5; 
      x = width/2;
      y = height/2;
      rectMode(CENTER);
    }
    
    void draw() {
      strokeWeight(2);
      if(on){
        background(0,255,0);
        stroke(0);
      } else {
        background(0);
        stroke(255);
      }
      
      fill(0,255,0);
      rect(y,x,100,100);
    }
    
    
    
    void mousePressed(){
      switchPressed =    (x-w/2<mouseX) && (mouseX<x+w/2) 
                      && (y-h/2<mouseY) && (mouseY<y+h/2);
      
      if(switchPressed) on = !on;
    }
    I understand the boolean switchPressed being set to false and then to on when the mouse is pressed in the area given by the last part. What I don't understand is how to set up THREE squares to do this because of the single variable switchPressed. In addition, I'm a bit stumped on making the grey borders.

  4. #4
    Zachafer's Avatar
    Joined
    Dec 2011
    Posts
    1,235
    Userbars
    11
    Thanks
    769
    Thanked
    1,466/678
    DL/UL
    98/0
    Mentioned
    512 times
    Time Online
    24d 13h 9m
    Avg. Time Online
    8m
    Code:
    Rectangle button1Area = new Rectangle(x, y, width, height);
    //Rectangle button2Area = ...;
    int getButtonPressed()
    {
      if(button1Area.contains(mouseX, mouseY))
        return 1;
      else if(button2Area.contains(mouseX,mouseY))
        return 2;
      else if(button3Area.contains(mouseX,mouseY))
        return 3;
      else
        return 0;
    }
    
    /*
    int buttonPressed = getButtonPressed();
    on = buttonPressed > 0;*/

Posting Permissions

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