Reputation: 689
I am currently working on a program with the raspberry pi that goes full screen and when I run this program in eclipse "on the pi" it works fine and looks something like this
But when I export the file to a runnable jar, this happens
But I get no errors and it doesn't close it just shows the windows but nothing happens.
Here is the code for the frame that you see,
package Main;
import java.awt.Button;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.io.IOException;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Tester extends JFrame {
// R3T Object
r3t R3T = new r3t();
// Random Stuff
public static boolean updateMe = false;
private int count = 0;
public static int R3TCount = 0;
private ImageIcon tank_full = new ImageIcon("src/res/level.png");
private ImageIcon tank_empty = new ImageIcon("src/res/tank_empty.png");
// For drawing the rectangle Y = NON DEFAULT
public static int NDRECT_Y = -0;
public static int TankValue = -0;
public static String TankLevel = "0.0";
//RATIOS
private int X1 = (int)(WIDTH * 0.06);
private int Y1 = (int)(HEIGHT * 0.111);
private int Y2 = (int)(HEIGHT * 0.781);
//Screen
public static Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
public static int WIDTH = (int)screenSize.getWidth();
public static int HEIGHT = (int)screenSize.getHeight();
public Tester() {
//Screen Size
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setTitle("Tank Level");
this.setUndecorated(true);
this.setLocation(0, 0);
this.setSize(new Dimension(WIDTH, HEIGHT));
this.setVisible(true);
}
public void paint(Graphics g) {
g.drawImage(tank_empty.getImage(), 0, 0, WIDTH, HEIGHT, this);
// REMINDER : x , y, x, y
g.drawImage(tank_full.getImage(), X1, Y1, TankValue, Y2, this);
drawTankLevel(g);
g.dispose();
doTask();
count += 1;
}
public void drawTankLevel(Graphics temp) {
temp.setFont(new Font("Vendana", 0, 30));
temp.drawString(" ", (WIDTH / 2) - 20, (HEIGHT / 2));
temp.drawString(TankLevel, (WIDTH / 2)- 20, (HEIGHT / 2));
temp.dispose();
}
public static void updateTankLevel(double level) {
TankValue = (int) ((WIDTH * level / 100) - (WIDTH * 0.12));
TankLevel = String.valueOf(level);
}
public static double getTankLevel() {
return Double.parseDouble(TankLevel);
}
public void doTask() {
if (count == 0) {
Thread t = new Thread(new Runnable() {
public void run() {
while (true) {
try {
Thread.sleep(2500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
System.out.println();
System.out.println ("CNT: "+R3TCount);
System.out.println ("-----------------");
R3T.GetDeviceInfo();
R3TCount += 1;
repaint();
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("ERROR: " + e.getMessage());
} catch (InterruptedException t) {
System.out.println("ERROR: " + t.getMessage());
}
}
}
});
t.start();
repaint();
}
if (updateMe) {
System.out.println("R3T: Updated Guage!");
updateMe = false;
}
}
public static void main(String[] args) {
Tester test = new Tester();
}
}
This is the code that changes the value, Its a Bluetooth scan and it gets the name of the device, I know this works fine though because it prints the value it just doesn't update it on screen
public void calculateTankLevel(String tempDevice) {
String deviceName = tempDevice.substring(18, tempDevice.length());
int byt_div = 0xE005;
int num_div = 10;
byte[] valueRay;
valueRay = hexStringToByteArray(deviceName.substring(deviceName.length() - 4, deviceName.length()));
double value = twoBytesToShort(valueRay[0], valueRay[1]);
double val = ((value - byt_div) / num_div);
System.out.println ("R3T: Calculated Tank Level");
if (val == Tester.getTankLevel()) {
//The value hasnt change dont update.
System.out.println ("R3T: Tank Level *NU*");
} else {
//Value has changed update UI and send to webservice
//R3TWebCall.sendTankLevel(val);
System.out.println("Tank Level: "+val);
Tester.updateTankLevel(val);
Tester.updateMe = true;
}
}
I added another part to my code this is it.
File f = new File("res/level.png");
if(f.exists() && !f.isDirectory()) {
System.out.println("TANK FULL EXISTS");
} else {
System.out.println("TANK FULL DOES NOT EXISTS");
}
File f1 = new File("res/tank_empty.png");
if(f1.exists() && !f.isDirectory()) {
System.out.println("TANK EMPTY EXIST");
} else {
System.out.println("TANK FULL DOES NOT EXISTS");
}
After adding this it will tell me if the time exists where I am looking for it and when I run it in eclipse it says the files does exist but! When I export to a jar file it says it does not exist!
Upvotes: 1
Views: 148
Reputation: 68
I had this problem a long time ago, you probably didn't declare your ressource folder as "class folder". Right click your project, then go to properties, then java build path, and click on "add class folder". A window will pop up, select your ressource folder "res" by checking the checkbox. Your may need to also modify your path.
Also move your res folder outside src folder, src folder is only for the sources of the code not for ressources. To load a ressource you could also use MyClass.class.getRessourceAsStream("path");
Upvotes: 1
Reputation: 825
Try using relative paths to your images files, may be that help. Please post also code for your JLabel or whatever presents number in the middle of frame.
I am not sure but you may missing repaint method in your update level tank method. You change value of string more effectively by using setText(String s)
for your JLabel
.
Relative path is something that doesn't depends on directories on your local machine but would be machine independent.
This is short article to better understand a topic of relative vs absolute:
http://www.coffeecup.com/help/articles/absolute-vs-relative-pathslinks/
Upvotes: 0