Results 1 to 4 of 4

Thread: Change multiple images simultaneously in a table

  1. #1
    Join Date
    Nov 2008
    Posts
    32

    Change multiple images simultaneously in a table

    Hello,
    I made an array of buttons that I can change the picture when I click on the button. This will clear a pawn in a dark piece. Now I'd like that when I click on a piece, it will change some pieces of the table at the same time. in places set the table at the same time. Do you have any idea about this?
    Here is my code:

    Code:
    package test; 
    
    java.awt.GridLayout import; 
    import java.awt.event.ActionEvent; 
    import java.awt.event.ActionListener; 
    import java.awt.event.MouseEvent; 
    java.awt.event.MouseListener import; 
    
    javax.swing.AbstractButton import; 
    javax.swing.ImageIcon import; 
    import javax.swing.JButton; 
    import javax.swing.JFrame; 
    import javax.swing.JPanel; 
    
    public class Window extends JFrame implements ActionListener ( 
    / ** 
    * 
    * / 
    private static final long serialVersionUID = 1L; 
    private JPanel pan = new JPanel (); 
    protected ImageIcon b1; 
    protected ImageIcon b2; 
    
    public Window () ( 
    
    this.setTitle ( "Test"); 
    this.setSize (450, 500); 
    this.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); 
    this.setResizable (false); 
    this.setLocationRelativeTo (null); 
    setLayout (new GridLayout (5, 5, 0, 0)); 
    
    JButton [] b = new JButton [25]; 
    for (int i = 0; i <b.length i + +) ( 
    b1 = new ImageIcon ( "b1.gif"); 
    b2 = new ImageIcon ( "b2.gif"); 
    b [i] = new JButton (b1); 
    b [i]. addActionListener (this); 
    
    pan.add (b [i]); 
    ) 
    setContentPane (pan); 
    ) 
    
    ( 
    
    this.setContentPane (pan); 
    this.setVisible (true); 
    ) 
    
    @ Override 
    public void actionPerformed (ActionEvent e) ( 
    / / TODO Auto-generated method stub 
    JButton b = (JButton) (e.getSource ()); 
    ImageIcon mImageIconButton = (ImageIcon) b.getIcon (); 
    
    if (mImageIconButton.getDescription (). equals (b1.getDescription ())) ( 
    b.setIcon (b2); 
    ) Else if (mImageIconButton.getDescription () 
    . equals (b2.getDescription ())) ( 
    b.setIcon (b1); 
    ) 
    ) 
    
    )

  2. #2
    Join Date
    Feb 2008
    Posts
    1,852

    Re: Change multiple images simultaneously in a table

    I think you should just create a function that represents the logic of your program (the algorithm that you follow), and then you call the method actionPerformed of your listener, I do not think it's so complicated. This method must change the image of your buttons according to your circumstances, that's all.

  3. #3
    Join Date
    Nov 2008
    Posts
    32

    Re: Change multiple images simultaneously in a table

    I see the approach you suggest me but what I blocks is the method that allows me to retrieve the details of the various pieces that I want to change at the same time with one click. But I try And if you have an idea, it is welcome.

  4. #4
    Join Date
    Feb 2008
    Posts
    1,852

    Re: Change multiple images simultaneously in a table

    Well I considered two ways of seeing things may be there are there more .....

    The first:
    You set an attribute your class Frame, JButton matrix table in the following way ....

    Code:
    JButton [] [] mButtons = new JButton [5] [5];
    Your class JFrame implements ActionListener therefore the action performed method you do


    Code:
    public void actionPerformed (ActionEvent e) ( 
    for (int i = 0; i <mButtons.length i + +) ( 
    for (int j = 0, j <mButton [i]. length j + +) ( 
    if (((JButton) e.getSource ()). equals (mButtons [i] [j])) ( 
    / / Then you get the position in the table clicked button, simply change the color of the pieces you want in relation to this one. 
    ) 
    ) 
    ) 
    )
    For the second, it is a bit more complex, I also of course the fact that tt is in the former topic, see my last post

    But if not for the explanation, each button will have specific attributes that will be her color, and a list of buttons that will change color at the same time it is clicked. This list of buttons will be charged when you initialize your window.
    To do this in Java, a bit of inheritance:

    Code:
    ReversoPion class extends JButton ( 
    private List <ReversePion> mReversePionsVoisins; 
    / / Enumeration to indicate the color of the pawn value fonce (clear) 
    private ColorReversePioin mColorReversePion; 
    )
    The method becomes actionPerformed this "extracted from the other topic:
    Code:
    public void actionPerformed (ActionEvent pE) ( 
    changeColorReverse (); 
    changeColorReverseVoisinsColor (); 
    )
    with the methods felt goods:

    Code:
    public void changeReversepionColor () ( 
    if (mColorReversePion == ColorReversePion.clair) ( 
    mColorReversePion = ColorReversePion.fonce; 
    setIcon (sImageIconPionFonce); 
    ) Else if (mColorReversePion == ColorReversePion.fonce) ( 
    mColorReversePion = ColorReversePion.clair; 
    setIcon (sImageIconPionClair); 
    ) 
    ) 
    
    public void changeReversePionsVoisinsColor () ( 
    for (ReversePion vReversePionTmp: mReversePionsVoisins) ( 
    vReversePionTmp.changeReversePionCouleur (); 
    ) 
    )

Similar Threads

  1. How to sort multiple columns in Excel Pivot Table
    By Nicoloid in forum MS Office Support
    Replies: 2
    Last Post: 17-02-2012, 08:01 PM
  2. multiple identical table and field properties
    By chaingangextreme in forum Software Development
    Replies: 2
    Last Post: 28-06-2010, 09:40 AM
  3. Replies: 3
    Last Post: 23-06-2010, 02:57 AM
  4. How to select multiple table in MySql
    By Doroteo in forum Software Development
    Replies: 4
    Last Post: 05-11-2009, 09:28 PM
  5. Multiple Iso images on a dvd
    By Gagnesh in forum Operating Systems
    Replies: 3
    Last Post: 18-08-2009, 09:23 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,237,328.71437 seconds with 17 queries