Results 1 to 8 of 8
  1. #1
    Ericyue is offline Member
    Join Date
    Mar 2012
    Posts
    18
    Rep Power
    0

    Exclamation continuouslyrefresh using java

    hi to all

    i wish to do a java program which can show a image and keep refresh every 0.1 seconds.

    below is my program

    import java.awt.*;
    import javax.swing.*;

    public class ShowImage extends JPanel{
    Image image; // Declare a name for our Image object.

    public ShowImage(){
    super();

    image = Toolkit.getDefaultToolkit().getImage("screenshot.j pg");
    }




    public void paintComponent(Graphics g){


    g.drawImage(image,0,0,1440,900, this);

    }

    public static void main(String arg[]){
    JFrame frame = new JFrame("ShowImage");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.setSize(1440,900);

    ShowImage panel = new ShowImage();
    frame.setContentPane(panel);
    frame.setVisible(true);
    }
    }

    this is only to display the image only but cannot auto refresh . can anyone tell me how to do that ?

    thanks in advance

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,185
    Rep Power
    20

    Default Re: continuouslyrefresh using java

    Have another thread launched that, every 0.1 seconds, calls repaint on the ShowImage object.
    Though I would ask why you want to redraw the same image over and over again.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Ericyue is offline Member
    Join Date
    Mar 2012
    Posts
    18
    Rep Power
    0

    Default Re: continuouslyrefresh using java

    i will receive image from another machine every 0.1 second adn i have to keep update the image .. and i am asking is there any way to do that due to i have no idea and in the google is all about the javascript to refresh webpage. AND I AM ASKING IDEA BUT NOT CODE .. THANK YOU !!!!

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,185
    Rep Power
    20

    Default Re: continuouslyrefresh using java

    Ah, so the image changes.
    Whenever you receive the image could you update the value in ShowImage and then simply call repaint()?
    That strikes me as the better way of doing this, though the Thread option will work.

    (and that's my sig, since we get a lot of requests. Though I probably ought to shrink the font...:))
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,453
    Rep Power
    20

    Default Re: continuouslyrefresh using java

    Quote Originally Posted by Tolls View Post
    Have another thread launched ...
    Better than that, use a Swing Timer.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,185
    Rep Power
    20

    Default Re: continuouslyrefresh using java

    That is another thread...:P
    I never said how to launch the other thread.
    :)
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    Ericyue is offline Member
    Join Date
    Mar 2012
    Posts
    18
    Rep Power
    0

    Default Re: continuouslyrefresh using java

    hi all / thanks for the reply ..
    this is the new code after add the repaint in ..


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

    /**
    * This class demonstrates how to load an Image from an external file
    */
    public class LoadImageApp extends Component {

    BufferedImage img;

    public void paint(Graphics g) {
    g.drawImage(img, 0, 0, null);
    }

    public LoadImageApp() {
    try {
    img = ImageIO.read(new File("screenshot.jpg"));
    } catch (IOException e) {
    }

    }

    public Dimension getPreferredSize() {
    if (img == null) {
    return new Dimension(100,100);
    } else {
    return new Dimension(img.getWidth(null), img.getHeight(null));
    }
    }

    public static void main(String[] args) {

    JFrame f = new JFrame("Load Image Sample");

    f.addWindowListener(new WindowAdapter(){
    public void windowClosing(WindowEvent e) {
    System.exit(0);
    }
    });


    boolean a = true;
    while(a)
    {
    f.add(new LoadImageApp());
    f.pack();
    f.setVisible(true);
    f.repaint();
    } }
    }


    it still cannot refresh the image .. is there any problem about the code ..

    thanks in advance for replying the code

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: continuouslyrefresh using java

    • Don't mix Swing with AWT. Use a JComponent such as a JPanel or JComponent itself and override the paintComponent method.
    • While (true) code is similar to nothing that anyone above has been recommending -- just don't do that.
    • You don't want to replace your component every xxx msec, you want to change an image only. So do this. Have you read any of the graphics tutorials?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •