class ClassCanvas extends Canvas{
private int keyboard_key = 0;
protected void paint(Graphics g){
//Never mind the drawing, essence is that keyRepeated isn't working!
g.drawString(""+keyboard_key,0,0,Graphics.TOP|Graphics.LEFT);
}
protected void keyRepeated(int keyCode) {
keyboard_key = keyCode;
}
}
I compiled that, and paint always draws 0. Like that keyRepeated isn't even executed.
I tested "hasRepeatEvents()" and it says "true" so why keyRepeated doesn't work?
I'm out of ideas what could be happening.
keyPressed and keyReleased work, but keyRepeated not
Additional info:
CLDC-1.1,
MIDlet-2.0
IDE: Netbeans7.0.1 (or Java(TM) ME
Platform SDK 3.0)
Phone: CamshellCldc1, DefaultCldcPhone1 (I
tested both, both didn't work)
Related
I want to write an application that runs with two different windows, one with JavaFX (v18) and one with Processing (v4). I'm on Linux 64-bit using IntelliJ-Idea as IDE and Maven to manage dependencies. The project is created as a JavaFX project and the processing.core library is added as a separate project library.
I spent A LOT of TIME understanding how to set up the project correctly and I succeeded in launching a simple JFX app that creates a new window where a 2D sketch runs fine (a circle that follows the mouse's coordinates).
The problem comes out when I try to launch a 3D sketch (because that's what i need for my application) with the P3D render and I receive an exception with:
java.lang.UnsatisfiedLinkError: Can't load library: /home/.../natives/linux-amd64/libgluegen_rt.so
As suggested here I already tried to solve adding the jogl-fat.jar library as a project's library or dependencies for the jogl libraries from maven, without effects.
I don't think that something is wrong with my code but it's Processing or JFX's fault.
PS: I have to use processing to make a 3D environment for simulation, but I don't have restrictions for JavaFX. If someone knows another framework that can let me launch a P3D sketch and interact with it with some controls (i.e. buttons), you are welcome!
This is an example of the java classes that I have:
public class HelloApplication extends Application {
#Override
public void start(Stage stage) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("hello-view.fxml"));
Scene scene = new Scene(fxmlLoader.load(), 320, 240);
stage.setTitle("Hello!");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
}
public class ProcessingTest extends PApplet{
private PeasyCam cam;
#Override
public void setup() {
background(255);
cam = new PeasyCam(this, 400);
}
public void settings(){
size(200, 200, P3D);
}
public void draw(){
background(255);
rotateX(-.5f);
rotateY(-.5f);
lights();
scale(5);
strokeWeight(1 / 10f);
fill(96, 255, 0);
box(30);
pushMatrix();
translate(0, 0, 20);
fill(0, 96, 255);
box(5);
popMatrix();
}
public void run(){
String[] args = {"com.effibot.provafx.ProcessingTest"};
PApplet.runSketch(args,this);
}
}
public class HelloController {
#FXML
private Label welcomeText;
private ProcessingTest pt;
#FXML
protected void onHelloButtonClick() {
welcomeText.setText("Welcome to JavaFX Application!");
pt = new ProcessingTest();
pt.run();
}
}
Unfortunately I don't have experience with your exact setup (64-bit Linux, IntelliJ, JavaFX application), but hopefully I can point you in the right direction.
The OpenGL native libraries should ship with Processing:
processing-4.0b8-linux-x64.tar/processing-4.0b8/core/library/linux-amd64
should contain:
libgluegen_rt.so
libnativewindow_awt.so
libnativewindow_drm.so
libnewt_head.so
libnativewindow_x11.so
libjogl_desktop.so
libnewt_drm.so
libjogl_mobile.so
Hopefully the IntelliJ docs can help with the native library setup.
The idea is that you'd tell IntelliJ not only to use core.jar, jogl-all.jar and gluegen-rt.jar but also set the native library paths to point to the .so files.
(as jewelsea mentions, in the run options you could set the native path as a command line argument (e.g. if Processing in unzipped in the home folder -Djava.library.path=/home/processing-4.0b8-linux-x64.tar/processing-4.0b8/core/library/linux-amd64) but I hope IntelliJ's interface is straight forward enough to easily allow you to set the native library path for the gluegen and jogl libraries)
(Also, bare in mind Processing ships with it's own JDK packaged (in processing-4.0b8/java): in case you run into some java version related issues you could switch the JDK in IntelliJ))
I'm building my app using the Nokia SDK 1.0 for the Asha 501
What I want to know, is how to capture events by pressing a TextArea. I'm porting an app from the S40 and using the code below, the TextArea doesn't capture the events
TextArea itemText = new TextArea("Hello I'm a TextArea", 2, 22) {
public void pointerPressed(int x, int y) {
System.out.println("PRESSED");
}
public void pointerReleased(int x, int y) {
System.out.println("HI!");
}
};
itemText.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
System.out.println("HI");
}
});
itemText.setEditable(false);
itemText.setFocusable(false);
itemText.getStyle().setBorder(null);
itemText.getStyle().setFgColor(Constants.Style.Color.GREY_DATE);
itemText.getStyle().setBgTransparency(Constants.Style.TRANSPARENT);
itemText.getStyle().setFont(Font.createSystemFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL));
EDIT
Someone in the Nokia Forum, tells me to delete the itemText.setFocusable(false); line, but it doesn't work.
I've finally found what is the problem, I don't know the reason why but that's it.
Removing this line
itemText.setEditable(false);
the TextArea catches the events.
I don't find the reason that in Nokia SDK 2.0 this code is working and why here is not. Ande there is another fact. I set the TextAreato editable, and it is not editable o_O
For the life of me, I cannot figure out why this program does not work in Java 7. I've run it with no problems in using Java 6, but as soon as I run it with Java 7, it doesn't work.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class HelloWorld implements ActionListener {
JButton button;
boolean state;
public HelloWorld(){
init();
state = false;
System.out.println("state - "+state);
while (true){
if (state == true){
System.out.println("Success");
}
}
}
private void init(){
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
button = new JButton("Button");
button.addActionListener(this);
frame.add(button);
frame.pack();
frame.setVisible(true);
}
#Override
public void actionPerformed(ActionEvent e) {
JButton source = (JButton)e.getSource();
if (source == button){
state = !state;
System.out.println("state - "+state);
}
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
new HelloWorld();
}
}
Using Java 6, if I press the button, it will print out the phrase "Success" until I hit the button again. Using Java 7 registers that the button was pressed and the value of state was changed to true, but the phrase "Success" is never printed. What's going on?
Add volatile to the field declaration.
Without volatile, changes in the field are not guaranteed to be visible on other threads.
In particular, the JITter is free to believe that the field never changes on the main thread, allowing it to remove the if entirely.
When you show the JFrame
frame.setVisible(true);
The Java Show the window and stop the execution on this line.
You configured the window to exit on close:
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
This program will terminate after you close the window.
So the code after the init() call will never be executed.
I encoutered a strange behaviour of handling mouse position by Swing when maximizing JFrame:
When I execute this very simple code...
public class Test {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame();
JMenuBar menubar = new JMenuBar();
JMenu menu = new JMenu("File");
menu.add(new JMenuItem("New"));
menubar.add(menu);
frame.setJMenuBar(menubar);
frame.setSize(200, 200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
... I can normally click File (first click - press,release) -> New (second click). But when I maximize JFrame and click on File - context menu disappears on mouse release immediately.
Moreover when I hold mouse button - to prevent disappearance - I must move my mouse much further to focus on New item.
The red dot represents area (more or less) where I must move my mouse to focus on New after pressing File and holding mouse button.
I've observed the same behaviour when using "right-click context menu" for example when right clicking on chart from JFreeChart.
I thought it was JDK problem because I used Oracle's JDK, but after installing OpenJDK I have the same results.
Did somebody observe this strange behaviour? Or am I missing something obvious?
I use:
1.7.0_147-icedtea (or 1.7.0_04 for java-7-oracle)
OpenJDK Runtime Environment (IcedTea7 2.0) (7~b147-2.0-0ubuntu0.11.10.1)
OpenJDK 64-Bit Server VM (build 21.0-b17, mixed mode)
Linux Mint 12 (lisa) GNOME 3.2.1
Yes - this is a bug in JDK7 as #nIcE cOw mentioned.
I've installed JDK6 and I couldn't reproduce this bug.
java version "1.6.0_23"
OpenJDK Runtime Environment (IcedTea6 1.11pre) (6b23~pre11-0ubuntu1.11.10.2)
OpenJDK 64-Bit Server VM (build 20.0-b11, mixed mode)
There is also a workaround when it is necessary to use the Oracle Java 7 (e.g. when using JavaFX). Just add the following lines of code to your window/frame class:
if (Arrays.asList("gnome-shell", "mate", "other...").contains(System.getenv("DESKTOP_SESSION"))) {
try {
Class<?> xwm = Class.forName("sun.awt.X11.XWM");
Field awt_wmgr = xwm.getDeclaredField("awt_wmgr");
awt_wmgr.setAccessible(true);
Field other_wm = xwm.getDeclaredField("OTHER_WM");
other_wm.setAccessible(true);
if (awt_wmgr.get(null).equals(other_wm.get(null))) {
Field metacity_wm = xwm.getDeclaredField("METACITY_WM");
metacity_wm.setAccessible(true);
awt_wmgr.set(null, metacity_wm.get(null));
}
}
catch (Exception x) {
x.printStackTrace();
}
}
This code snippet is based on a workaround from the Netbeans developers.
I would like to complement the solution given by problemzebra.
As it still happens for me with any swing application on Linux (using Cinnamon desktop), even on Java 6 (update 45)
The issue will reoccur every time I move the window or resize it, so you need to reapply the workaround every time the window changes. I created the following class and use it whenever I create a new window:
class LinuxWindowFix implements WindowStateListener {
private final String desktop;
private Field metacity_wm;
private Field awt_wmgr;
private boolean applyFix;
private static LinuxWindowFix instance = new LinuxWindowFix();
public static LinuxWindowFix getInstance() {
return instance;
}
private LinuxWindowFix() {
applyFix = false;
List<String> linuxDesktops = Arrays.asList("gnome-shell", "mate", "cinnamon"); //add more desktop names here.
desktop = System.getenv("DESKTOP_SESSION");
if (desktop != null && linuxDesktops.contains(desktop.toLowerCase())) {
try {
Class<?> xwm = Class.forName("sun.awt.X11.XWM");
awt_wmgr = xwm.getDeclaredField("awt_wmgr");
awt_wmgr.setAccessible(true);
Field other_wm = xwm.getDeclaredField("OTHER_WM");
other_wm.setAccessible(true);
if (awt_wmgr.get(null).equals(other_wm.get(null))) {
metacity_wm = xwm.getDeclaredField("METACITY_WM");
metacity_wm.setAccessible(true);
applyFix = true;
}
} catch (Exception ex) {
//ignore
}
}
}
#Override
public void windowStateChanged(WindowEvent e) {
try {
awt_wmgr.set(null, metacity_wm.get(null));
} catch (Exception ex) {
//ignore
}
}
public void apply(Window w) {
if (!applyFix) {
return;
}
w.removeWindowStateListener(this);
w.addWindowStateListener(this);
}
}
Simply call this for every window you create and it will work as expected.
LinuxWindowFix.getInstance().apply(myWindow);
I am working my way through the NeHe OpenGL examples, using the LWJGL for the OpenGL binding inside an Eclipse RCP application.
My OpenGL graphics are displayed inside the RCP canvas, not in a separate window.
Lesson 07 shows how to use the keyboard. If I try to do a:
Keyboard.create();
I get an error that the (OpenGL) "Display" has not been created.
If I create an OpenGL "Display" with org.lwjgl.opengl.Display.create(), then I get a new Window.
So how do I access the Keyboard without creating a new Window?
You cannot use the Keyboard without a Display, because of how LWJGL works behind the scenes. The best way is to just use AWT events. You can write your own input class, that could go something like this.
public class Input implements KeyListener {
private boolean aDown; //is the A key down?
//Ect, for all needed keys
public void keyPressed(KeyEvent ke) {
switch (ke.getKeyCode()) {
case KeyEvent.VK_A: aDown = true; break;
//and so on for all other needed keys.
}
}
public void keyReleased(KeyEvent ke) {
switch (ke.getKeyCode()) {
case KeyEvent.VK_A: aDown = false; break;
//and so on for all other needed keys.
}
}
public void keyTyped(KeyEvent ke) {} //Do nothing
public void isADown() {return aDown;}
}