LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-22-2003, 07:28 AM   #1
secprovider
Member
 
Registered: Jul 2003
Distribution: Debian sarge
Posts: 40

Rep: Reputation: 15
best way of handling images in Java


Hi,

I need a way of handling images in Java. What I want to do is as follows?

I have a number of images. Probably I need to put each of them inside a panel. Each panel will have two images. So when you click on the panel the first image is shown, when you click again, the other images is shown. First image is same for all panels. 2nd images differ from panel to panel and I should keep track of each 2nd image inside panels.
One more thing, when switching from first image to second, some animation would be great. What is the basic way to handle such kind of images?

I did this way, but this is not suitable. Put the images inside JLabel and JLabel inside JPanel.
 
Old 09-22-2003, 04:22 PM   #2
Looking_Lost
Senior Member
 
Registered: Apr 2003
Location: Eire
Distribution: Slackware 12.0, OpenSuse 10.3
Posts: 1,120

Rep: Reputation: 45
Sounds a bit like a deck of cards what you've got, a simple way to do it is override an existing class, its paint method and preferred size, such as JButton. I actually did use overrode the JButton class for a simple card game. The class below works best if both images are the same size or the first image displayed is the largest though it could be rewritten. For simple animation guess you could just create an array with each animation image in it and display them alternatively, though admittedly I'm no Walt Disney.

Code:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;


public class ImageFlip extends JButton implements ActionListener
{
    
   private Dimension picSize;

   private boolean isFaceUp=false;

   private  Image front,back;
 
  
   public ImageFlip(Image im1,Image im2)
   {

    front=im1;
    back=im2;
    
    picSize=new Dimension(front.getWidth(this),front.getHeight(this));
  
    this.setPreferredSize(picSize);
    this.setBorderPainted(false);
 

    addActionListener(this);   

    }
   
      
    public void actionPerformed(ActionEvent ae)
       {

          repaint();
             
          isFaceUp= ! isFaceUp;

             }
         

   public Dimension getPrefferedSize()
   {


    return picSize;
    

   }


  public void paint(Graphics g)
  {

   super.paint(g);


   if(isFaceUp)
     g.drawImage(back,0,0,this);
   else
     g.drawImage(front,0,0,this);


   }    

}
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert cd images to dvd images rcsuk Debian 2 07-11-2005 10:34 AM
Signal handling......... rajsun Programming 2 06-28-2005 08:10 AM
Transmit images between 2 Java applications Elec490 Linux - General 1 04-16-2005 09:10 AM
Images on Java poeta_boy Programming 3 03-03-2004 12:29 AM
API handling KaktusKnight Linux - General 0 03-14-2003 06:25 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:31 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration