Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / August 2008

Tip: Looking for answers? Try searching our database.

Show the most recent image, display only it.

Thread view: 
bH - 30 Aug 2008 05:50 GMT
Hi All,
This is a demo program only of my problem. My real
program is too big to be included here.

In this program, given a button panel, a label on a panel,
a image icon on that label then making a single press of
button b1, gets  the image from the url. To see the image
button b2 is pressed. However if I then repeately press
button b2, to show the image, each image  is parked along
side of the previous one, or in the next row. Again show
most recent one only, not all the previous ones, showi it
in the location of the first one.

TIA
bH

import java.awt.image.*;
import javax.imageio.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;

public class GDP  extends JFrame
 implements ActionListener {

 private Icon myImage = new ImageIcon();
 private JButton b1 = new JButton("Get Drawing");
 private JButton b2 = new JButton("Drawing is >>>");
 private JButton b3= new JButton("Close");
 private Boolean counterFlag = false;
 private BufferedImage image;
 public static void main(String[] args){
   new GDP ().go();
 }
 void go()
 {
   JPanel labelPanel = new JPanel();
   JPanel btnPanel = new JPanel();
   btnPanel.add(b1);
   b1.addActionListener(this);
   btnPanel.add(b2);
   b2.addActionListener(this);
   btnPanel.add(b3);
   b3.addActionListener(this);

   Container contentPane= getContentPane();
   contentPane.setLayout( new FlowLayout() );
   setSize(350,200);
   contentPane.add(btnPanel );
   contentPane.add(labelPanel);
   this.setResizable(false);
   setVisible(true);
 }

 public void actionPerformed(ActionEvent e) {
   String udp1 = "http://www.extension.iastate.edu/gif/"+
   "Balls/l_green.gif";
   String udp2 = "http://www.extension.iastate.edu/gif/"+
   "Balls/l_red.gif";
   String udp = "";
   if(e.getSource() == b1){
     try {
       if(counterFlag == false){
         udp = udp1;
       }
       else{
         udp = udp2;
       }
       URL url = new URL(udp);
       image = ImageIO.read(url);
       counterFlag = true;
     }
     catch (IOException em) {
       System.out.println("import Data: I/O exception");
     }
     System.out.println
       ("Now press Drawing Is >>>  if you want to see it");

   }  //end b1
   if(e.getSource() == b2){
     Image img = image ;
     JLabel label = new JLabel(new ImageIcon(img)) ;
     label.setLayout( new GridLayout(0,1) );
     getContentPane().add(label) ;
     setVisible( true );

   } //end b2

   if(e.getSource() == b3){
     System.exit(0);

   }  //end b3
 } // action draw picture
}
Andrew Thompson - 30 Aug 2008 06:20 GMT
> Hi All,
> This is a demo program only of my problem.

I am still not sure I understand what you want,
but if that is 'only ever one image on-screen',
you might do it this way.

<sscce>
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;

import javax.imageio.*;

import java.io.*;

import java.net.*;

public class GDP  extends JFrame
 implements ActionListener {

//  private Icon myImage = new ImageIcon();
 private JButton b1 = new JButton("Get Drawing");
 private JButton b2 = new JButton("Drawing is >>>");
 private JButton b3= new JButton("Close");
 private Boolean counterFlag = false;
 private BufferedImage image;

 /** We keep a reference to a single JLabel, and
 reuse it for each icon. */
 JLabel label;

 public static void main(String[] args){
   new GDP ().go();
 }
 void go()
 {
   JPanel labelPanel = new JPanel();
   JPanel btnPanel = new JPanel();
   btnPanel.add(b1);
   b1.addActionListener(this);
   btnPanel.add(b2);
   b2.addActionListener(this);
   btnPanel.add(b3);
   b3.addActionListener(this);

   Container contentPane= getContentPane();
   contentPane.setLayout( new FlowLayout() );
   setSize(350,200);
   contentPane.add(btnPanel );
   contentPane.add(labelPanel);
   this.setResizable(false);
   setVisible(true);
 }

 public void actionPerformed(ActionEvent e) {
   String udp1 = "http://www.extension.iastate.edu/gif/"+
   "Balls/l_green.gif";
   String udp2 = "http://www.extension.iastate.edu/gif/"+
   "Balls/l_red.gif";
   String udp = "";
   if(e.getSource() == b1){
     try {
       if(counterFlag == false){
         udp = udp1;
       }
       else{
         udp = udp2;
       }
       URL url = new URL(udp);
       image = ImageIO.read(url);
       counterFlag = true;
     }
     catch (IOException em) {
       System.out.println("import Data: I/O exception");
     }
     System.out.println
       ("Now press Drawing Is >>>  if you want to see it");

   }  //end b1
   if(e.getSource() == b2){
     Image img = image ;
     if (label==null) {
       label = new JLabel(new ImageIcon(img)) ;
         getContentPane().add(label) ;
   } else {
     label.setIcon(new ImageIcon(img)) ;
   }
     setVisible( true );

   } //end b2

   if(e.getSource() == b3){
     System.exit(0);

   }  //end b3
 } // action draw picture
}
</sscce>

--
Andrew Thompson
http://pscode.org/
bH - 30 Aug 2008 13:08 GMT
> > Hi All,
> > This is a demo program only of my problem.
[quoted text clipped - 99 lines]
> --
> Andrew Thompsonhttp://pscode.org/

Hi Andrew,

Thanks for the revision to the program. For me, it is a
question of where to place the coding of panels and
other related parts within the program, especially when
a sequence of order is needed. Your solution is appreciated.

Thanks,
bH


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2010 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.