Results 1 to 8 of 8
Thread: continuouslyrefresh using java
- 03-27-2012, 10:07 AM #1
Member
- Join Date
- Mar 2012
- Posts
- 18
- Rep Power
- 0
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
- 03-27-2012, 10:53 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.
- 03-27-2012, 11:01 AM #3
Member
- Join Date
- Mar 2012
- Posts
- 18
- Rep Power
- 0
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 !!!!
- 03-27-2012, 11:07 AM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.
- 03-28-2012, 10:46 AM #5
- 03-28-2012, 11:01 AM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.
- 03-30-2012, 05:29 AM #7
Member
- Join Date
- Mar 2012
- Posts
- 18
- Rep Power
- 0
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
-
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?


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks