#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    8
    Rep Power
    0

    Project: A Simple DrawBox


    Hey Guys!

    I have been assigned a project from my Java Professor to create a simplistic Painting program.Nothing too fancy, but still quite challenging for a beginner. It requires me to make use of the packages java.awt.Color, java.awt.Graphics and java.awt.event.MouseEvent.

    I already have the layout set up, i.e I created the framework for panels with which one can later choose the colors and shapes and a panel in which you then draw, but I still haven't figured out how to get MouseEvent listeners to react to my actions with the mouse.

    Here's the class I created for the drawing panel of the frame:


    Code:
    public class CreatePanel extends JPanel{
    	
    	CreatePanel(){
    		setSize(500, 500);
    	}
    	
    	protected void paintComponent(Graphics g){			 
    		g.setColor(Color.white);	
    		g.fillRect(50, 85, 500, 500); 
    	}
    	
        public void mousePressed(MouseEvent me)
        {
            String str="Mouse Pressed at "+me.getX()+","+me.getY();
            System.out.println(str);	
        }
    
    }
    What I thought this would do is print out onto the console the Coordinates of the cursor at the moment I pressed the left mouse button. However, nothing seems to happen (i.e. no printout on the console). What could I do to fix this?
    Btw, I just

    Thanks for any help,
    Dän

    P.S I may have more questions concerning this project later on, so I'll more than likely be using this thread to any and all questions connected to my project as not to flood the forum with my problems.
  2. #2
  3. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,022
    Rep Power
    1285
    Where did you add the MouseListener?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    8
    Rep Power
    0
    Oops.

    No wonder it didn't work.

    Code:
    public class CreatePanel extends JPanel{
    	
    	CreatePanel(){
    		setSize(500, 500);
    		setLocation(50, 85);
    		addMouseListener(new MyMouseListener());
    	}
    	
    	protected void paintComponent(Graphics g){			 
    		g.setColor(Color.white);			
    		g.fillRect(0, 0, 500, 500);		
    	}
    	
    	class MyMouseListener extends MouseAdapter
    	{
    		
    	    public void mousePressed(MouseEvent me)
    	    {
    	        String str="Maustaste gedrückt bei "+me.getX()+","+me.getY();
    	        System.out.println(str);
    	    }
    	    
    	    public void mouseReleased(MouseEvent me)
    	    {
    	        String str="Maustalste losgelassen bei "+me.getX()+","+me.getY();
    	        System.out.println(str);
    	    }
    
    
    	}
    	
    
    
    }
    I think i fixed it now.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    8
    Rep Power
    0
    From one problem to the next...
    This time it has to do with the setLocation(int x, int y) Method of the swing package. I have three classes in total which implement this method, however only one of those where the method actually does what's expected. I personally can't see where I could have made the mistake.

    Here's the Class where it doesn't work:

    Code:
    public FormPanel(){							//Constructor:
    		setSize(35, 500);						//Creates a panel size 35x500 Pixels
    		setLocation(20, 50);					//Places the panel at Frame-Coordinates 20x50
    		addMouseListener(new MyMouseListener());	//Adds MouseListener
    		
    	}
    
    	
    	protected void paintComponent(Graphics g){		//Draws the Icons which will later be selectable
    		g.drawRect(5, 5, 25, 25);				//to draw correspondig items within the frame
    		g.drawOval(5, 40, 25, 25);				//of Class CreatePanel
    		g.drawLine(5, 65, 30, 90);
    	}
    	
    	
    	
    	class MyMouseListener extends MouseAdapter								//Simple MouseListener as an
    	{															//inner Class to test whether the MouseListener
    																//reacts to input.
    	    public void mousePressed(MouseEvent me)								//Prints Panel-Coordinates of the Cursor to the console 
    	    {															//according to where it whas when the mous button was pressed/released
    	        String str="Maustaste gedrückt bei "+me.getX()+","+me.getY();
    	        System.out.println(str);
    	    }
    	    
    	    public void mouseReleased(MouseEvent me)
    	    {
    	        String str="Maustalste losgelassen bei "+me.getX()+","+me.getY();
    	        System.out.println(str);
    	    }
    
    
    	}
    
    
    }
    See my post above for an example where the setLocation(int x, int y) method does in fact work.

    Thanks for your time and any help.



    EDIT:

    I have found out some additional information concerning this Problem:

    The Following is the Class which contains the main function of my program:

    Code:
    public class GraphicsTest {
          
    public static void main(String[] args){
                
                GraphicEditor wnd = new GraphicEditor();
                wnd.setLocation(300, 300);
                wnd.setTitle("DrawBox");
                wnd.setVisible(true);
    
                CreatePanel crep = new CreatePanel();
                wnd.add(crep);
                
                ColorPanel cp = new ColorPanel();
                wnd.add(cp);
                
                FormPanel fp = new FormPanel();
                wnd.add(fp);
    
                
          }
    
    }
    It calls the Class GraphicEditor, which is the Frame for my panels. The problem mentioned above is not restricted to the FormPanel class, tt doesn't even matter if there are only one or two panels created (tested by commenting out multiple combinations of the three obects), but the last object to be created in the main function is the one which gets stuck in the corner as mentioned above.

    So I'm guessing the problem is not with the panel classes, but either in the main Class or the Class which generates the Frame, but I can't see where that mistake might be.
  8. #5
  9. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,022
    Rep Power
    1285
    I think your issue is due to your LayoutManager. By default the Frame uses the BorderLayout.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    8
    Rep Power
    0
    I have since received help from another source concerning this exercise and have successfully completed it.
    I'm sorry that I have not replied sooner but I totally forgot about this.

    thank you very much, bullet for your help.

IMN logo majestic logo threadwatch logo seochat tools logo