How to update images with imageview from code - java

I'm trying to make an app that displays images continuosly (4 or 5 times per second) so i need to update an image held inside a ImageView object, it works if i trigger the functions from a button press, every time i press the button the next image gets displayed:
int i = 0;
public void buttonPressed(View view) {
ImageView image = (ImageView) findViewById(R.id.imageView5);
String path = String.format("/storage/emulated/0/Download/%d.bmp", i);
File imgFile = new File(path);
bMap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
image.setImageBitmap(bMap);
i++;
}
But if i try to call the same functions more than once in the same parent function or i try to run them in a loop the first images are not loaded at all and only the last is finally displayed after loopFunc() completed:
public void loopFunc() {
ImageView image = (ImageView) findViewById(R.id.imageView5);
for (i = 1; i < 3; i++) {
String path = String.format("/storage/emulated/0/Download/%d.bmp", i);
File imgFile = new File(path);
bMap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
image.setImageBitmap(bMap);
//wait some time or do other things...
}
}
In few words i need to be able to execute image.setImageBitmap(bMap); from code and not from buttons anyone knows how? thanks

The problem lies in the time you used to compute other things. Is there some kind of delay function code you wrote in it.

Try this. Just make a function and call it onCreate method with some Thread or Handler.
public void imgViwer()
{
Thread thread=new Thread(new Runnable() {
#Override
public void run() {
try {
for (i = 1; i < 3; i++) {
String path = String.format("/storage/emulated/0/Download/%d.bmp", i);
File imgFile = new File(path);
bMap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
image.setImageBitmap(bMap);
}
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
}
After every 200ms your loop works.

Root cause
Because the loop is run too quickly in a short time, that why you see the first image is not displayed at all, and only the last is finally displayed after loopFunc() completed.
Solution
To display a sequence of images continuously per second, then you should use Handler.
public void loopFunc() {
Handler mainHandler = new Handler(Looper.getMainLooper());
int totalImage = 3;
for (int i = 1; i < totalImage; i++) {
int imageIndex = i;
mainHandler.postDelayed(new Runnable() {
#Override
public void run() {
String path = String.format("/storage/emulated/0/Download/%d.bmp", imageIndex);
File imgFile = new File(path);
bMap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
image.setImageBitmap(bMap);
}
}, 1000 / totalImage * imageIndex);
}
}

Related

JavaFX JSON parsing from URL threading freezes UI

I have this task run in a thread. The problem is that it freezes the UI every time it is executed. The freeze is longer when the internet is slow. How can I prevent the UI from freezing even if it is still gathering data from the url?
Task<Void> task = new Task<Void>(){
#Override
public Void call() throws Exception {
while (true) {
Platform.runLater(new Runnable() {
#Override
public void run(){
String json = null;
try {
psname = null;
PumpSites n = table.getSelectionModel().getSelectedItem();
psname = n.getPs();
if(psname == "Cubacub")
json = readUrl(""); //read json from thingspeak.com webpage
else if(psname == "Canduman")
json = readUrl("");
} catch (InterruptedIOException iioe)
{
btn1.setTextFill(Color.RED);
btn2.setTextFill(Color.RED);
btn3.setTextFill(Color.RED);
btn4.setTextFill(Color.RED);
btn5.setTextFill(Color.RED);
btn1.setText("NULL");
btn2.setText("NULL");
btn3.setText("NULL");
btn4.setText("NULL");
btn5.setText("NULL");
}
catch (IOException ioe)
{
btn1.setTextFill(Color.RED);
btn2.setTextFill(Color.RED);
btn3.setTextFill(Color.RED);
btn4.setTextFill(Color.RED);
btn5.setTextFill(Color.RED);
btn1.setText("NULL");
btn2.setText("NULL");
btn3.setText("NULL");
btn4.setText("NULL");
btn5.setText("NULL");
}
catch (Exception e1) {
btn1.setTextFill(Color.RED);
btn2.setTextFill(Color.RED);
btn3.setTextFill(Color.RED);
btn4.setTextFill(Color.RED);
btn5.setTextFill(Color.RED);
btn1.setText("NULL");
btn2.setText("NULL");
btn3.setText("NULL");
btn4.setText("NULL");
btn5.setText("NULL");
}
Gson gson = new Gson();
Page page = gson.fromJson(json, Page.class);
for (Item item : page.feeds)
{
det2 = 1;
btn1.setText(item.field1);
btn2.setText(item.field2);
btn3.setText(item.field3);
btn4.setText(item.field4);
btn5.setText(item.field5);
f2 = Float.parseFloat(item.field2);
f3 = Float.parseFloat(item.field3);
//float f5 = Float.parseFloat(item.field5);
if (f2 <= 10.0)
{
btn1.setTextFill(Color.RED);
btn2.setTextFill(Color.RED);
}
else
{
btn1.setTextFill(Color.BLUE);
btn2.setTextFill(Color.BLUE);
}
if (f3 < 0.9 || f3 > 1.2)
{
btn3.setTextFill(Color.RED);
}
else
{
btn3.setTextFill(Color.BLUE);
}
/*if (f5 > 5.0)
{
btn5.setTextFill(Color.RED);
}
else
{
btn5.setTextFill(Color.BLUE);
}*/
btn4.setTextFill(Color.BLUE);
}
if(det2 == 0)
{
btn1.setTextFill(Color.RED);
btn2.setTextFill(Color.RED);
btn3.setTextFill(Color.RED);
btn4.setTextFill(Color.RED);
btn5.setTextFill(Color.RED);
btn1.setText("NULL");
btn2.setText("NULL");
btn3.setText("NULL");
btn4.setText("NULL");
btn5.setText("NULL");
}
det2 = 0;
}
});
Thread.sleep(10000);
}
}
};
Thread th = new Thread(task);
th.setDaemon(true);
th.start();
The problem is that it freezes the UI every time it is executed. The freeze is longer when the internet is slow. How can I prevent the UI from freezing even if it is still gathering data from the url?
The UI thread freezes because you are still doing the all the logic on the JavaFX application Thread(Platform.runLater ).
You should do something like this instead:
public Void call() throws Exception
{
while (true)
{
try
{
//get json
} catch(Exception e)
{
Platform.runLater(new Runnable()
{
#Override
public void run()
{
//set buttons color and text here
}
}
}
//Rest of your logic here
}
}
The idea is that everything that is going to modify a UI component from a separate Thread should be handled in the Platform.runLater
If you use a background thread invoke Platform.runLater with a long-running Runnable as parameter, you've effectively achieved nothing. The Runnable is still run on the JavaFX application thread freezing your app.
Instead you should collect all the data on the background thread and process it to the point where you simply need to adjust some properties of the scene. Then you use Platform.runLater to do those updates.
But the good news is that there is a class designed for this scenario that could simplify your code a bit: ScheduledService.
Just make sure that you don't access the GUI in any way from the background thread (neither for reading nor for setting properties).
The following example simplified example should demonstrate the general approach. It calculates some multiples of the value chosen via Spinner on a background thread delaying 10 sec between each calculation:
#Override
public void start(Stage primaryStage) {
Spinner<Integer> spinner = new Spinner(1, 100, 1);
// ensure the value is available in a way that allows synchronisation
final AtomicReference<Integer> input = new AtomicReference<>(spinner.getValue());
spinner.valueProperty().addListener((o, oldValue, newValue) -> input.set(newValue));
final int outputCount = 10;
GridPane root = new GridPane();
root.add(spinner, 0, 0, 2, 1);
// create output grid
Text[] output = new Text[outputCount];
for (int i = 1; i <= output.length; i++) {
Text text = new Text(Integer.toString(spinner.getValue() * i));
output[i - 1] = text;
root.addRow(i, new Text("Value multiplied by " + i + " = "), text);
}
root.setPrefWidth(300);
ScheduledService<int[]> service = new ScheduledService<int[]>() {
#Override
protected Task<int[]> createTask() {
return new Task<int[]>() {
#Override
protected int[] call() throws Exception {
// retrieve value and set it to null to denote a change
// that was already handled to avoid doing unnecessary
// work
Integer value = input.getAndSet(null);
int[] result = null;
if (value != null) {
int valueAsInt = value;
result = new int[outputCount];
for (int i = 0; i < outputCount; i++) {
result[i] = (i + 1) * valueAsInt;
}
}
// simpulate delay
Thread.sleep(2000);
return result;
}
};
}
};
service.valueProperty().addListener((o, oldValue, newValue) -> {
// update GUI
if (newValue != null) {
for (int i = 0; i < outputCount; i++) {
output[i].setText(Integer.toString(newValue[i]));
}
}
});
service.setPeriod(Duration.seconds(10));
// make sure service uses a daemon thread
service.setExecutor(Executors.newSingleThreadScheduledExecutor((Runnable r) -> {
Thread t = new Thread(r);
t.setDaemon(true);
return t;
}));
service.start();
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
I recommend looking through the javadoc of ScheduledService to get familiar with it's capabilities. It also allows for things like reacting to exceptions in the task and specifying a backoff strategy.

Needing JAVA help in animation

I'm new in Java (still learning) all the other works have gone well but only this animation gives my headache and the coffee won't even help=(!
I should make an animation of Javaman (10 gif pictures named as T1, T2,...T10) I should use Thread, MediaTracker-class and addImage-method. Then I should specify the speed of the animation by using sleep-method (I used join-method if that's right??).
(MY JAVA CODE GOES LIKE THIS)
import java.applet.Applet;
import java.awt.*;
public class Animaatio extends Applet implements Runnable {
Image[] images = null;
MediaTracker tracker = null;
int current = 0;
Thread animThread;
#Override
public void init() {
// Creating a new media tracker, to track loading images
tracker = new MediaTracker(this);
// Creating an array of ten images
images = new Image[10];
// Downloading the images
for (int i = 0; i < 10; i++) {
// Loading the images
images[i] = getImage(getCodeBase(), (i + 1) + "T.gif");
tracker.addImage(images[i], 0);
}
try {
tracker.waitForAll();
} catch (InterruptedException e) {
}
}
#Override
public void start() {
if (animThread == null) {
animThread = new Thread(this);
animThread.start();
}
try {
animThread.join();
} catch (InterruptedException e) {
}
}
#Override
public void paint(Graphics g) {
g.drawImage(images[current++], 0, 0, this);
}
#Override
public void run() {
while (true) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
}
}
}
}
The problem is that I don't see any animation just an empty applet viewer which just keeps running. Problem might be caused if the images are storaged in the wrong place? If someone could wreally help me, I would be really thankful for my knight=).

How do I change values in a continuous thread in java fx?

With the following code,the thread is activated on button click in the controller class. Thing is, I want to be able to change the values in he Line array, to have to anchor pane erase old lines and add new lines.This is in the controller.java after main in launched.
static startVal = -1,endVal = -1;
Lines L;//this is supposed to store new lines after some methods are called
//this method is called on button click and I want it to start again every time the button is clicked
public void drawPath(){
Task task = new Task<Void>() {
#Override
public Void call() throws Exception {
while(true){
int i = 0;
while (i<5) {
final int finalI = i;
Platform.runLater(new Runnable() {
#Override
public void run() {
anchPane.getChildren().add(L[finalI]);
if(startVal == -1)
startVal = anchPane.getChildren.size()-1;
else
endVal = anchPane.getChildren().size()-1;
}
});
i++;
Thread.sleep(2000);
}
//removeLines();
anchPane.getChildren().remove(startVal,endVal);
}
}
};
Thread th = new Thread(task);
th.setDaemon(true);
th.start();
}

Update button color

I have to make an android memory game where a sequence of colors show up on the screen and you then have to click the colors that came up. To display the sequence of colors I'm just using an unclickable button that changes colors. I'm currently doing this using a thread with runOnUiThread but every time it will only change the button to the last color I want it to. Why wont it change to any previous color?
final ArrayList<Integer> colours = new ArrayList<Integer>();
Button buttonOne = (Button) findViewById(R.id.colourOne);
Button buttonTwo = (Button) findViewById(R.id.colourTwo);
Button buttonThree = (Button) findViewById(R.id.colourThree);
Button buttonFour = (Button) findViewById(R.id.colourFour);
ColorDrawable buttonOneColor = (ColorDrawable) buttonOne.getBackground();
ColorDrawable buttonTwoColor = (ColorDrawable) buttonTwo.getBackground();
ColorDrawable buttonThreeColor = (ColorDrawable) buttonThree.getBackground();
ColorDrawable buttonFourColor = (ColorDrawable) buttonFour.getBackground();
int one = buttonOneColor.getColor();
int two = buttonTwoColor.getColor();
int three = buttonThreeColor.getColor();
int four = buttonFourColor.getColor();
colours.add(one);
colours.add(two);
colours.add(three);
colours.add(four);
runOnUiThread(new Runnable() {
#Override
public void run() {
for(int x = 0; x < colours.size(); x++) {
try {
Button light = (Button) findViewById((R.id.light));
light.setBackgroundColor(colours.get(x));
Thread.sleep(1000);
} catch (Exception e) {
System.out.println(e);
}
}
}
});
The thread is sleeping after each setBackground color but the buttons color wont actually change until the loop reaches its last loop.
You have to create a new Thread and inside on the Thread call runOnUiThread and change the color. Also you have to initialize the button out of the Thread. You must declare variable x out of the onCreate method
new Thread(new Runnable() {
#Override
public void run() {
for(x = 0; x < colours.size(); x++) {
runOnUiThread(new Runnable() {
#Override
public void run() {
light.setBackgroundColor(colours.get(x));
}
});
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();

How to Register Java Swing Component to Garbage Collection?

I have an application to show a image of the user's data. Where each user has more than 1 image data.
When I select the user in the JTable, my image data show in JPanel. The picture actually also JPanel with background image.
My question is, memory consumption always increases, and not reduce. Sometimes the application hangs.
How to register the picture(JPanel) to garbage collection. And if it possible, when i must to register them?
I am new in performance issue in Java.
This is my code :
public void getStreamData(final PanelEntry view, final String data) {
String files = null;
String path = null;
if(isImages()) {
path = "data/"+data+"/images";
} else {
path = "data/"+data+"/videos/thumbs";
}
File folder = new File(path);
// Always remove previous image label when new data selected
view.getPanelStream().removeAll();
if (!folder.exists()) {
JLabel label = new JLabel("No Stream Data");
label.setForeground(Color.red);
label.setVisible(true);
// Adding to panelGallery
view.getPanelStream().add(label);
view.getPanelStream().revalidate();
view.getPanelStream().repaint();
} else {
File [] listOfFiles = folder.listFiles();
int maxFiles = listOfFiles.length;
int maxView = 15;
// Loop for get image from file
for (int i = listOfFiles.length; i > 0 ; i--) {
if(listOfFiles[i].isFile()) {
files = listOfFiles[i].getName();
final String videoFiles = files;
if(files.endsWith(".jpg") || files.endsWith(".JPG") ||
files.endsWith(".jpeg") || files.endsWith(".JPEG") ||
files.endsWith(".png") || files.endsWith(".PNG")) {
final String newPath = path+"/"+files;
try {
File showFile = new File(newPath);
ImageIcon imgSource = new ImageIcon(newPath);
JPanel labelGallery = new BackgroundImageRounded(showFile);
labelGallery.setLayout(null);
labelGallery.setPreferredSize(new Dimension(160, 120));
labelGallery.setVisible(true);
JLabel labelName = new JLabel(files);
labelName.setSize(150,15);
labelName.setLocation(8, 8);
labelName.setVisible(true);
labelName.setForeground(Color.ORANGE);
labelGallery.add(labelName);
String videoPath = "data/"+data+"/videos/";
String video = videoFiles.replace(".jpg", ".wmv");
String videoFile = video.replace("thumb_", "video_");
final String videoPlayer = videoPath+videoFile;
if (isImages()) {
labelGallery.setToolTipText("View Image");
} else {
labelGallery.setToolTipText("Play Video");
JLabel iconPlayer = new JLabel();
iconPlayer.setIcon(new ImageIcon(getClass().getResource("/com/ikbiz/gastroscope/resources/player.png")));
iconPlayer.setSize(61,42);
iconPlayer.setVisible(true);
iconPlayer.setLocation(50, 35);
labelGallery.add(iconPlayer);
}
labelGallery.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
if(isImages()) {
ImageViewer viewer = new ImageViewer(newPath);
viewer.setVisible(true);
} else {
VideoViewer videoViewer = new VideoViewer();
videoViewer.setViewer(videoPlayer);
videoViewer.setLocationRelativeTo(null);
videoViewer.pack();
videoViewer.setVisible(true);
}
}
});
// Adding to panelGallery
view.getPanelStream().add(labelGallery);
view.getPanelStream().revalidate();
view.getPanelStream().repaint();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
}
Please help,
Thank you.
You should use a memory profiler to find out what is going on.
Here, you talk about registering the JPanel, but how are you 100% sure that this is the kind of objects that consume memory ?
I think you should :
Profile your heap utilization
Find out the objects that consume a lot of memory
Find where they are instantiated
Find out why they are not reclaimed by the GC (what are the GC roots that links to them)
Without more information, you are just guessing that some portion of your code must be the culprit, but you do not have a clue.

Categories