Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: How do I make a picture-part interact with a user?

  1. #1
    Member
    Join Date
    Jul 2011
    Posts
    62
    Thanks
    12
    Thanked 4 Times in 4 Posts

    Default How do I make a picture-part interact with a user?

    What I am trying to do is draw a house with code relative to getting it's doors and windows closed when I click on them in an applet.

    Here is the house I drew with code,

    JavaHouse.png

    and if you look at it, you notice it's windows and doors being open already.

    The only problem I have now is that I don't know what form a component should take if it has to be like a drawing-part or be one.

    I tried figuring out a way to do this, but it didn't work: using if-statements relative to each of the house-opening coordinates, but I still think it has a chance. This is because there may be a connection between the getX- and getY methods and the paint- and/or paintComponent-methods. That is, unless any forum-member says otherwise, or I find out otherwise.

    Here is my code relative to the applet's reaction to a click for a closed door: (be sure to read each comment first)

         // This class is supposed to get the applet to respond to being clicked
         // on the right coordinates because of the mousePressed-method.
         private class MyMouseListener extends MouseAdapter
         {
              public void mousePressed(MouseEvent e)
              {
                   // Get the door to become a different-color.
                   if (currentX > 160 && currentX < 200
                       && currentY > 120 && currentY < 180)
                   {
                        door = true;
                        houseClosed = new HouseClosed(door, leftWindow, rightWindow);
                        houseClosed.repaint();
                   }
         }
     
         // This class is supposed to diagnose whether or not the cursor-click
         // gets an adequate-response from certain-coordinates using the 
         // mouseMoved-method.
         private class MyMouseMotionListener extends MouseMotionAdapter
         {
              public void mouseMoved(MouseEvent e)
              {
                   // Check on where the cursor is.
                   coords[0].setText("X: " + e.getX());
                   coords[1].setText("Y: " + e.getY());
     
                   // Store where it is in the right fields.
                   currentX = e.getX();
                   currentY = e.getY();
              }
         }


  2. #2
    Member
    Join Date
    Jul 2011
    Posts
    62
    Thanks
    12
    Thanked 4 Times in 4 Posts

    Default Re: How do I make a picture-part interact with a user?

    Just so someone knows, I am trying to paint over the black windows to make them cyan, and paint red over the door. My assignment requires me to use code to paint or draw over the existing-parts, or at least use code to make each opening seem closed when I click on it to make it the way I just described.

  3. #3
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: How do I make a picture-part interact with a user?

    Quote Originally Posted by SOG View Post
    This is because there may be a connection between the getX- and getY methods and the paint- and/or paintComponent-methods. That is, unless any forum-member says otherwise, or I find out otherwise.
    getX() and getY() return the position of the component, used in paintComponent() to determine where on the "canvas" to draw.
    The solution will depend on the implementation.. (How are the windows/doors handled in the code?)(Post the code)

  4. #4
    Member
    Join Date
    Jul 2011
    Posts
    62
    Thanks
    12
    Thanked 4 Times in 4 Posts

    Default Re: How do I make a picture-part interact with a user?

    Here is how the windows/doors are handled in the code, in fact I'll give you the whole paint-method:

         public void paint(Graphics g)
         {
              // Call the superclass paint-method.
              super.paint(g);
     
              // Setup the x- and y-coordinates for a triangle-roof.
              int[] xCoords = {60, 180, 300};
              int[] yCoords = {100, 30, 100};
     
              // Draw the color-filled, house-roof triangle.
              g.setColor(Color.RED);
              g.fillPolygon(xCoords, yCoords, 3);
     
              // Draw the house-rectangle.
              g.setColor(Color.GRAY);
              g.fillRect(80, 100, 200, 80);
     
              // Draw the door.
              g.setColor(Color.BLACK);
              g.fillRect(160, 120, 40, 60);
     
              // Draw the windows.
              g.setColor(Color.BLACK);
              g.fillRect(100, 120, 36, 36);
     
              g.setColor(Color.BLACK);
              g.fillRect(224, 120, 36, 36);
         }
    Last edited by jps; September 10th, 2013 at 08:39 AM. Reason: code=java in code tag

  5. #5
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: How do I make a picture-part interact with a user?

    In that case why not just change the color based on if the window/door is closed?
    if window/door is closed, use closed color, else use open color
    paint window/door

  6. #6
    Member
    Join Date
    Jul 2011
    Posts
    62
    Thanks
    12
    Thanked 4 Times in 4 Posts

    Default Re: How do I make a picture-part interact with a user?

    That is what I tried to do, but I think I'll need a sample of code to figure this out - getting the paintComponent-method to work with the getX- and getY-methods as you pointed out in post number-3.

  7. #7
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: How do I make a picture-part interact with a user?

    The comment in post #3 was more of a reply to the question I quoted than a suggestion on how to handle this. This project seems to lack in the way of object orientation, but that opinion would depend on what class you are in..

    If you know the position of the windows/door *1 and you can determine the point of the mouse click *2 then you can determine if and which one was clicked, and set the value of the variable which represents open/closed state. From there, inside the paint method, use this value to determine which color to use in painting the windows/door

    *1 If the windows/door were, for example, JComponents, or some subclass of such, you could write something like window.getX() to get the x coordinate. Looking at the code, those positions are hard coded into the paint method

    *2 It looks like you already have a method in the works for this part

  8. #8
    Member
    Join Date
    Jul 2011
    Posts
    62
    Thanks
    12
    Thanked 4 Times in 4 Posts

    Default Re: How do I make a picture-part interact with a user?

    The class I am in is called House.java and it extends JApplet - it's meant to draw the house from my textbook. (I'm not in a school-class for this - it's done by me alone for practice.) So there are no suggestions on your part when it comes to this hierarchical-connection that might be there in Java?

  9. #9
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: How do I make a picture-part interact with a user?

    I give suggestions in post #7.
    You know the position of the windows/door, because you hard coded them into the paint method.
    You are already working on a method that gets the location of the mouse click, finish it.
    When the mouse is clicked compare the point of the click to the door and each window, if the click hit one of them, toggle its open/closed state, otherwise just ignore the click

    This would be much easier with an object oriented approach in my opinion, but that approach will require some more studying / trial&error / experience. If you want to go that route (which is my suggestion at this point), put this project on hold for now, check out the Object-Oriented Programming Concepts, at least browse through the 2D Graphics tutorial, and then follow the Custom Painting tutorial

  10. #10
    Member
    Join Date
    Jul 2011
    Posts
    62
    Thanks
    12
    Thanked 4 Times in 4 Posts

    Default Re: How do I make a picture-part interact with a user?

    I solved the problem by noting the connection between the paintComponent-method, the paint-method, and the repaint-method. My code for the house and a new class is show below. (The new class was the solution. By the way, the website and/or computer had a hard time responding, so here is an update from my editing.)

     import javax.swing.*;
     import java.awt.*;
     import java.awt.event.*;
     import java.awt.image.*;
     
     // This applet's class should draw a house
     // that closes it's openings when you click on it.
     public class House extends JApplet
     {
          private HouseClosed houseClosed; // Use this field for closing the house.
     
          // Use the init-method as the applet-constructor.
          public void init()
          {
               // Make a houseClosed-object.
               houseClosed = new HouseClosed();
     
               // Add the JPanel-extension (the object) to the applet.
               add(houseClosed, BorderLayout.CENTER);
     
               // Now open and close the house by clicking on it.
               addMouseListener(new MyMouseListener());
          }
     
          // Use the paint method to build the house.
          public void paint(Graphics g)
          {
               // Call the superclass paint-method.
               super.paint(g);
     
               // Setup the x- and y-coordinates for a triangle-roof.
               int[] xCoords = {60, 180, 300};
               int[] yCoords = {100, 30, 100};
     
               // Draw the color-filled, house-roof triangle.
              g.setColor(Color.RED);
              g.fillPolygon(xCoords, yCoords, 3);
     
               // Draw the house-rectangle.
               g.setColor(Color.GRAY);
               g.fillRect(80, 100, 200, 80);
     
               // Draw the door.
               g.setColor(Color.BLACK);
               g.fillRect(160, 120, 40, 60);
     
               // Draw the windows.
               g.setColor(Color.BLACK);
               g.fillRect(100, 120, 36, 36);
     
               g.setColor(Color.BLACK);
               g.fillRect(224, 120, 36, 36);
          }
     
          // Use this inner-class with a mouse-pressing event for
          // opening and closing the house.
          private class MyMouseListener extends MouseAdapter
          {
               public void mousePressed(MouseEvent e)
               {
                    // Close the openings.
                    houseClosed.repaint();
     
                    // Now open them.
                    repaint();
               }
          }
     }

     import javax.swing.*;
     import java.awt.*;
     
     // Use this JPanel-extension class to 
     // redraw a closed-opening house.
     public class HouseClosed extends JPanel
     {
          // Use the constructor to setup the method below.
          public HouseClosed()
          {
               // Make the background white.
               setBackground(Color.WHITE);
     
               // Match the html-document version of the applet
               // when it comes to size.
               setPreferredSize(new Dimension(370, 300));
          }
     
          public void paintComponent(Graphics g)
          {
               super.paintComponent(g);
     
               // Setup the x- and y-coordinates for a triangle-roof.
               int[] xCoords = {60, 180, 300};
               int[] yCoords = {100, 30, 100};
     
               // Draw the color-filled, house-roof triangle.
               g.setColor(Color.RED);
               g.fillPolygon(xCoords, yCoords, 3);
     
               // Draw the house-rectangle.
               g.setColor(Color.GRAY);
               g.fillRect(80, 100, 200, 80);
     
               // Draw the closed-door.
               g.setColor(Color.RED);
               g.fillRect(160, 120, 40, 60);
     
               // Draw it's knob.
               g.setColor(Color.BLACK);
               g.fillOval(188, 150, 5, 5);
     
               // Draw the closed-windows.
               g.setColor(Color.CYAN);
               g.fillRect(100, 120, 36, 36);
     
               g.setColor(Color.CYAN);
               g.fillRect(224, 120, 36, 36);
     
               // Draw the window-dividers.
               g.setColor(Color.BLACK);
               g.drawLine(118, 120, 118, 156);
     
               g.setColor(Color.BLACK);
               g.drawLine(100, 138, 136, 138);
     
              g.setColor(Color.BLACK);
              g.drawLine(224, 138, 260, 138);
     
               g.setColor(Color.BLACK);
               g.drawLine(242, 120, 242, 156);
          }
     }

Similar Threads

  1. HELP - placing picture on top of another picture in Java - JLayerPane
    By smasm in forum What's Wrong With My Code?
    Replies: 39
    Last Post: April 27th, 2012, 07:16 PM
  2. Converting a picture made up of 0s and 1s into a colored picture??
    By Kranti1992 in forum Java Theory & Questions
    Replies: 10
    Last Post: November 21st, 2011, 05:25 PM
  3. Want to make java application available to all user of Network
    By nicool in forum Java Theory & Questions
    Replies: 5
    Last Post: September 25th, 2011, 07:38 AM
  4. Help with a program to make a landscape picture
    By noseeds in forum Java Theory & Questions
    Replies: 1
    Last Post: December 15th, 2009, 09:25 PM
  5. How to make user press enter to continue in program?
    By BC2210 in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: October 3rd, 2009, 05:08 AM