Results 1 to 5 of 5
  1. #1
    tzeus is offline Member
    Join Date
    Dec 2013
    Posts
    20
    Rep Power
    0

    Default Mouse event inside loop

    Hello!
    I am new, both here, and in Java.
    I am trying to develop an image puzzle game in Java.

    The game is suppose to read image files from the pwd and list them in a JPanel. Then the user must click an image and that image should break in multiple smaller images and show these in the Jpanel.
    It doesn't matter what comes next because i cannot manage to solve one issue. I did get to display the image list, and split the image in a number of rows / columns, but i cannot display them. I simply do not get where i go wrong about this. I tried cards layout, grid layout, no layout. I created some test actions inside the mouse event and they do take place(like println). I also tried to create a new frame and display the new images there, but to no result.
    This is the mouse event code:
    Imagine is a custom data type, holding various information on the images: size related info, extension, and some methods(split image in chunks, create a list of images in the folder)
    The SpargeImagine is a method that takes the filename of a photo and splits that photo into a number of smaller images(based on rows/columns).
    NOTE: I did try to just replace,revalidate,invalidate, repaint the original JPanel in the application, or to use card layout with a different panel but no result. The best result that i could get was to delete the old contents of the panel. In it's current form the app draws a new empty frame. I also tried to put a bunch of labels in there, just to see if they are drawed, but they did not.


    Java Code:
    					public void mouseClicked(MouseEvent e) {
    						
    						// TODO Auto-generated method stub
    						
    						JFrame puzzleX = new JFrame("bPuzzle");
    						Container containerBucatiPuzzle= puzzleX.getContentPane();
    						containerBucatiPuzzle.setLayout(new GridLayout());
    					    final JPanel listaBucati = new JPanel();
    					    puzzleX.getContentPane().setPreferredSize(new Dimension(800, 600));
    						// setarea actiunii im momentul apasarii lui X
    						puzzleX.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    						System.out.println(buton.getName());
    						
    						Imagine puzzleIntreg = new Imagine();
    						puzzleIntreg.setNume(buton.getName());
    						System.out.println("Mouse Apasat");
    						
    						try {
    							@SuppressWarnings("unused")
    							BufferedImage[] bucatiPuzzle = Imagine.SpargeImagine(puzzleIntreg);
    
    							for (int i = 0; i < bucatiPuzzle.length; i++) {
    								JButton[] jLabel = new JButton[bucatiPuzzle.length];
    								jLabel[i] = new JButton(new ImageIcon(bucatiPuzzle[i]));
    								int coordx = (int) (Math.random() * 796);
    								int coordy = (int) (Math.random() * 596);
    								jLabel[i].setLocation(coordx, coordy);
    								//jLabel[i].setLocation(150, 150);
    								// jLabel.setBounds(100,50,300,50);
    								
    								JLabel testLabel = new JLabel();
    								testLabel.setText("label");
    								testLabel.setText("test");
    								
    								listaBucati.add(jLabel[i]);
    								jLabel[i].setVisible(true);
    								
    								System.out.println(i);
    								
    							}
    							
    							
    							
    							puzzleX.pack();
    							puzzleX.setVisible(true);
    						} catch (IOException e1) {
    							// TODO Auto-generated catch block
    							e1.printStackTrace();
    						}
    
    					}
    Where do i go wrong about this? It seems so simple, but i cannot get it.

    Thank you in advance!

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Mouse event inside loop

    I'm not sure I understand what you are trying to do. The posted code creates a new JFrame on every mouseclick with EXIT on close which will end the program when any of the windows are closed.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    tzeus is offline Member
    Join Date
    Dec 2013
    Posts
    20
    Rep Power
    0

    Default Re: Mouse event inside loop

    Yes, this is the last "version" of the code. Let's forget the part where i create a new JFrame for every for loop, and focus on the JButton part. The previous version of the code would add a new JButton for every for loop, but i wouldn't see any of those buttons. Even in the current state i should have a JButton on that frame, which i do not see.
    Do you understand what i am trying to do?
    Replace the frame with a JPanel declared outside of this form, which i populate with JButton[] elements.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Mouse event inside loop

    Do you understand what i am trying to do?
    I'm not sure. Are you trying to change the components shown in the GUI? That requires telling the container and the layout manager about the change and asking them to redo the layout.

    Can you make a small, complete program that compiles and executes to test your problem with?
    Last edited by Norm; 12-08-2013 at 05:46 PM.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    tzeus is offline Member
    Join Date
    Dec 2013
    Posts
    20
    Rep Power
    0

    Default Re: Mouse event inside loop

    I think i fixed it, although i still don't understand what i was doing wrong. But thx for the tip with the creation of the frame in the for loop. i was overlooking that too.

Similar Threads

  1. how to call mouse event code
    By such orb in forum New To Java
    Replies: 3
    Last Post: 09-17-2012, 12:11 AM
  2. Mouse event filtering?
    By Taiko in forum AWT / Swing
    Replies: 12
    Last Post: 08-10-2011, 03:00 PM
  3. get onbjet reference on mouse event
    By spimees in forum AWT / Swing
    Replies: 2
    Last Post: 05-18-2011, 06:28 AM
  4. popup without mouse event
    By cool in forum AWT / Swing
    Replies: 3
    Last Post: 11-04-2010, 01:57 PM
  5. Mouse Event + Image Thresholding
    By ojmayolebron in forum AWT / Swing
    Replies: 0
    Last Post: 03-27-2009, 01:17 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
  •