Skip to content

Commit

Permalink
Merge pull request #1 from shahrukhqasim/fix_crash
Browse files Browse the repository at this point in the history
Fix unresponsive canvas
  • Loading branch information
shahrukhqasim authored Oct 14, 2017
2 parents 29cf0af + 61bdc0a commit e3ab7bf
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import javafx.scene.control.ScrollPane;

import java.util.Timer;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import javafx.scene.control.ToggleButton;
import javafx.scene.input.KeyEvent;
Expand All @@ -23,6 +25,8 @@ public class Controller {
Drawable selectionConnection;
InteractionManager interactionManager;
final Object lock = new Object();
final Lock lockDraw = new ReentrantLock();

public ScrollPane scrollPane;
public Canvas canvas;
double scale = 1;
Expand All @@ -44,6 +48,8 @@ void initialize() {

void redraw() {
synchronized (lock) {
long startTime = System.nanoTime();

double vValue = scrollPane.getVvalue();
double hValue = scrollPane.getHvalue();

Expand All @@ -67,6 +73,9 @@ void redraw() {
selectionBox.draw(graphics2D, visibleRect, scale);
selectionConnection.draw(graphics2D, visibleRect, scale);
connections.draw(graphics2D, visibleRect, scale);

long endTime = System.nanoTime();
long duration = (endTime - startTime) / 1000000;
}
}

Expand Down Expand Up @@ -110,10 +119,8 @@ void onCanvasPressed(MouseEvent event) {
@FXML
void onCanvasReleased(MouseEvent event) {
synchronized (lock) {
synchronized (lock) {
Point2D endPoint = new Point2D(event.getX(), event.getY());
interactionManager.dragReleased(new Rectangle2D(Math.min(startPointClick.getX(), endPoint.getX()), Math.min(startPointClick.getY(), endPoint.getY()), Math.abs(endPoint.getX() - startPointClick.getX()), Math.abs(endPoint.getY() - startPointClick.getY())), scale, event.getButton());
}
Point2D endPoint = new Point2D(event.getX(), event.getY());
interactionManager.dragReleased(new Rectangle2D(Math.min(startPointClick.getX(), endPoint.getX()), Math.min(startPointClick.getY(), endPoint.getY()), Math.abs(endPoint.getX() - startPointClick.getX()), Math.abs(endPoint.getY() - startPointClick.getY())), scale, event.getButton());
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package io.github.shahrukhqasim.ties.label;

import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Platform;
import javafx.concurrent.Task;
import javafx.embed.swing.SwingFXUtils;
import javafx.geometry.Point2D;
import javafx.geometry.Rectangle2D;
Expand All @@ -10,6 +14,7 @@
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import javafx.util.Duration;
import java.util.*;

/**
Expand Down Expand Up @@ -170,8 +175,6 @@ void initialize() {
void load() {
synchronized (controller.lock) {
try {
if (controller.updater != null)
controller.updater.cancel();
controller.scale = 1;
loadOcrBoxes();
if (!loadCellBoxes())
Expand All @@ -186,17 +189,23 @@ void load() {
controller.canvas.setWidth(controller.image.getBoundingBox(controller.scale).getWidth());
controller.canvas.setHeight(controller.image.getBoundingBox(controller.scale).getHeight());

controller.updater = new Timer();
controller.updater.scheduleAtFixedRate(new TimerTask() {
Task task = new Task<Void>() {
@Override
public void run() {
try {
controller.redraw();
} catch (Exception e) {
e.printStackTrace();
public Void call() throws Exception {
while (true) {
Platform.runLater(new Runnable() {
@Override
public void run() {
controller.redraw();
}
});
Thread.sleep(100);
}
}
}, 100, 100);
};
Thread th = new Thread(task);
th.setDaemon(true);
th.start();
controller.fileLabel.setText(imagePath);
}
catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ public void draw(GraphicsContext graphics, Rectangle2D visibleArea, double scale
double height = visibleArea.getMaxY() - y;


graphics.drawImage(image, x / scale, y / scale, width / scale, height / scale, x, y, width, height);
for (int i=0;i<30;i++)
graphics.drawImage(image, x / scale, y / scale, width / scale, height / scale, x, y, width, height);

}

Expand Down

0 comments on commit e3ab7bf

Please sign in to comment.