I have an aplication where is picture with SIN, COS..
TextArea1 will tell somebody: "Click in picture where SIN"
if user do this, textArea2 tell him: "It is corect"
After that, textArea1.append "Click in picture where COS"
-but program is still waiting for clicking to SIN :(
Can you help me and give a little advice to me please?
There is some code:
private class KlikaniMysi implements MouseListener {
#Override
public void mouseClicked(MouseEvent e) {
System.out.println(e.getX() + " " + e.getY());
//for finding coordinates (by console)
//textArea2 = "try" at start !!
//textArea1 = "Find SIN" at start !!!!!!!!
if(textArea2.equals("try")){
if( (((e.getX()) > 420)&&((e.getX()) < 580)) && ( ((e.getY()) > 164)&&((e.getY()) < 178) )){
textArea2.setText("");
textArea2.append("RIGHT1");
textArea1.append("Find COS"); //!!!! work!!
//How to stop the IF cycle here and run class KlikaniMysi (mouselistener) again ?
}
else{
textArea1.setText("");
textArea1.append("try again");
}
}else{
if( (((e.getX()) > 586)&&((e.getX()) < 594)) && ( ((e.getY()) > 174)&&((e.getY()) < 282) )){
//This isnt nice, but I dont know how to expres range better.
textArea1.setText("");
textArea2.append("Right2");
textArea1.append("Find TAN");
}
else{
vystup.setText("");
textArea1.append("Try again");
}
}
}
There are many more aspects about this code that are 'suboptimal' but I think your problem is here:
if(textArea2.equals("try"))
You want to test the content of that area, not the area itself, hence change it to
if(textArea2.getText().equals("try"))
Related
I have a question regarding how someone was to make a JOptionPane.showMesageDialog(); appear within a duration of time. For example, my program is supposed to ask about the user's favorite movie, Subsequently at least 15 seconds after the user answers, my program is supposed to ask "are you there?", giving them the option of replying with Yes, no, or maybe.
How exactly can I do that? (if that's possible)
here's my code
```
if (null==moviename|| moviename.trim().isEmpty()) {
JOptionPane.showMessageDialog(null, "You did not enter anything");
} else { JOptionPane.showMessageDialog(null, moviename + ", sounds like a good watch.");
}
String ques=JOptionPane.showInputDialog("Are you there? Yes? No? Maybe?");
if (ques=="yes") {
JOptionPane.showMessageDialog(null,"Great To hear!");
} else if (ques=="no") {
JOptionPane.showMessageDialog(null,"Thats odd..");
} else if (ques=="maybe" || ques=="Maybe" ) {
JOptionPane.showMessageDialog(null, "That was rhetorical...");
}
}
```
I belive you just need to add Thread.sleep(n); before the JOptionPane
remember to import: java.lang.Thread;
time is in ms so 15seconds is gonna be 15000.
if (null==moviename|| moviename.trim().isEmpty()) {
try {
Thread.sleep(15000);
} catch (InterruptedException e) {
System.out.print("Sleep problem of type: "+e);
}
JOptionPane.showMessageDialog(null, "You did not enter anything");
} else {
JOptionPane.showMessageDialog(null, moviename + ", sounds like a good watch.");
}
When pressed the "Inregistrare" button a dialog pops, requesting the user to enter a password (set to "qwerty"). I want it keep displaying dialogs until the password is correct. The method is the following:
private void ItemInregistrareActionPerformed(java.awt.event.ActionEvent evt) {
JOptionPane dialog = new JOptionPane();
dialog.setWantsInput(true);
dialog.showInputDialog("Password please:");
while(dialog.getInputValue()!="qwerty")
dialog.showInputDialog("Mai baga o fisa.");
ItemInregistrare.setEnabled(false);
ItemOpen.setEnabled(true);
ItemSave.setEnabled(true);
}
The problem is it never gets out of the while, even if the password is correct. Any tips?
JOptionPane.showInputDialog is a static method and does not need any instance of JOptionPane. Moreover, it already returns the entered value or null if user pressed Cancel. So you don't need to call dialog.getInputValue().
You could try something like this:
String pwd;
do {
pwd = JOptionPane.showInputDialog("Password please:");
} while (pwd != null && !pwd.equals("qwerty"));
if (pwd == null) {
JOptionPane.showMessageDialog(null, "You pressed cancel");
} else {
JOptionPane.showMessageDialog(null, "Password is correct");
}
Try using
!dialog.getInputValue().equals("qwerty")
to compare strings
I am working on Parrot AR. Drone project. The libraries are downloaded and implemented in this project from JavaDrone website (https://code.google.com/p/javadrone/downloads/list). However, although I did included the all the correct libraries and make the right class call to get the method, it still cannot return me the correct information. All the results returned appeared to be "false". Any idea what happening on this code? Please help me :(
So what I did is I have 2 buttons : (i) connect (ii) take off buttons. The Connect button function is for establish connection to drone while Take off button is used for make the drone fly move a bit and return me the drone's NAV navigation data. Sadly all the returned NAV data appears not working.
Note : This code is working fine upon code compilation. But it just cannot return me the correct & valid NAV data from drone.
private void jButtonConnectActionPerformed(java.awt.event.ActionEvent evt) {
System.out.println("Connect?");
drone = new ARDrone();
data = new NavData();
drone.playLED(10,10,10);
drone.connect();
drone.clearEmergencySignal();
System.err.println("Ready to connect!!");
// Wait until drone is ready
drone.waitForReady(CONNECT_TIMEOUT);
System.err.println("Drone State: " + drone.getState());
// do TRIM operation
drone.trim();
System.err.println("Congratulation! You have connected to Drone!");
System.out.println("You can issue flight commands now!");
batteryStatus.setText("0" + "%");
batteryStatus.setForeground(Color.ORANGE);
batteryStatus.setText("" + data.getBattery());
}
private void jButtonTakeOffActionPerformed(java.awt.event.ActionEvent evt) {
System.err.println("Current Drone State : " + drone.getState().toString());
System.err.println("Taking off");
drone.takeOff();
Thread.sleep(4000);
System.err.println("**********\nMOVE\n**********");
drone.move(0.0f, 150.5f, 500.0f, 0.0f);
Thread.sleep(1000);
System.err.println("******************************************");
System.err.println("Drone Infomation");
System.err.println("Battery Too High ? " + data.isBatteryTooHigh());
System.err.println("Battery Too Low ? " + data.isBatteryTooLow());
System.err.println("Drone Flying ? " + data.isFlying());
System.err.println("Control Received ? " + data.isControlReceived());
System.err.println("Motor Down ? " + data.isMotorsDown());
System.err.println("Not Enough Power ?" + data.isNotEnoughPower());
System.err.println("Trim Received ? " + data.isTrimReceived());
System.err.println("Trim Running? " + data.isTrimRunning());
System.err.println("Trim succeded? " + data.isTrimSucceeded());
System.err.println("PIC Number OK? "+ data.isPICVersionNumberOK());
System.err.println("******************************************");
Thread.sleep(5000);
drone.sendAllNavigationData();
drone.land();
}
Output :
******************************************
Drone Infomation
Battery Life: 0.0%
Battery Too High ? false
Battery Too Low ? false
Drone Flying ? false
Control Received ? false
Motor Down ? false
Not Enough Power ?false
Trim Received ? false
Trim Running? false
Trim succeded? false
PIC Number OK? false
********************************************
Update:
What I did was followed John's suggestion. I did implemented all the neccessary methods and NavDataListener for getting the NavData from drone.
import com.codeminders.ardrone.ARDrone;
import com.codeminders.ardrone.ARDrone.VideoChannel;
import com.codeminders.ardrone.NavData;
import com.codeminders.ardrone.NavDataListener;
public class arDrone extends javax.swing.JFrame implements Runnable, NavDataListener{
public ARDrone drone;
public NavData data = new NavData();
public arDrone(String text) {
//FreeTTS speech text
this.text=text;
}
public arDrone() {
initComponents();
setIcon();
initDrone();
}
private void initDrone() {
try {
drone = new ARDrone();
data = new NavData();
drone.addNavDataListener(this);
} catch (UnknownHostException ex) {
System.err.println(ex);
return;
}
}
public void navDataReceived(NavData nd) {
System.err.println("Testing navDataReceived is running...");
updateBatteryStatus(nd.getBattery());
this.flying.set(nd.isFlying());
}
private void updateBatteryStatus(final int value) {
java.awt.EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
batteryStatus.setText(value + "%");
if (value < 15) {
batteryStatus.setForeground(Color.RED);
} else if (value < 50) {
batteryStatus.setForeground(Color.ORANGE);
} else {
batteryStatus.setForeground(Color.GREEN);
}
}
});
}
The problem is that you are not doing anything to actually get navdata. You can't just create a NavData object and hope it gets filled in with valid data--It won't.
You need to use the com.codeminders.ardrone.NavDataListener interface.
Implement the NavDataListener interface, and the
navDataReceived method.
Add your listener using the ARDrone
method addNavDataListener.
In your navDataRecieved method
you will receive a NavData object with valid telemetry data.
Do you set the Drone IP address? According to sources the default IP for the drone is 192.168.1.1.
You can call another constructor to set the IP:
drone = new ARDrone(InetAddress.getByName("xxx.xxx.xxx.xxx"));
replace xxx.xxx.xxx.xxx with the actual drone IP.
iam a beginner in Programming and trying to make a Cleaning Robot NXT
i attached ( Ultrasonic Sensor) ,and ( Sound Sensor )
the job of the Robot is that when i Clap it have to start moving Forward and when the UltraSonic Sensor sees Something on the way it must turns around and keep going Forward .
The Problem is that when it turns it doesn't keep moving Forward till i clap again !!!!!
and this is the code that i wrote :
public static void main(String[] args) {
// TODO Auto-generated method stub
TouchSensor touch = new TouchSensor(SensorPort.S2);
SoundSensor sound = new SoundSensor( SensorPort.S4 );
UltrasonicSensor sonic = new UltrasonicSensor( SensorPort.S3);
Motor.A.setSpeed( 400 );
Motor.C.setSpeed( 400 );
Button.waitForAnyPress();
int SoundValue;
SoundValue = sound.readValue();
System.out.print(SoundValue);
do {
if ( sound.readValue() > 50 ) {
// PROBLEM:
while ( sonic.getDistance() > 30 ){
Motor.B.backward();
Motor.A.backward();
Motor.C.backward();
}
{
Motor.A.rotate( -185, true );
Motor.C.rotate( 185, true );
}
};
}
while( Button.readButtons() != Button.ID_ESCAPE );
}
Can any one help solving this Problem please?????
thnx Any way .
The think the loop is slightly wrong...
Basically, I think you need a flag to indicate that the bot should be moving, so that when you clap, it flips the flag...
boolean move = false;
do {
if ( sound.readValue() > 50 ) {
move = !move;
}
while ( sonic.getDistance() > 30 ){
Motor.B.backward();
Motor.A.backward();
Motor.C.backward();
}
if (move) {
Motor.A.rotate( -185, true );
Motor.C.rotate( 185, true );
}
} while( Button.readButtons() != Button.ID_ESCAPE );
Or something similar. Otherwise, it will only move when there is another sound
I'd also just like to say, I'm very jealous ;)
I am making typing tutor web app for my college project. I want keyboard to be displayed in website and if i type any letter on keyboard that should highlight the key of web keyboard. So what technology should i use for displaying keyboard on web. I want to use java.
any help is appreciated.
Thanks in advance.
You need to implement Key Listeners for this.
Key events indicate when the user is typing at the keyboard. Specifically, key events are fired by the component with the keyboard focus when the user presses or releases keyboard keys. Please note these events will work only when your application has the System Focus.
1) Make sure the component's isFocusable method returns true. This state allows the component to receive the focus. For example, you can enable keyboard focus for a JLabel component by calling the setFocusable(true) method on the label.
2) Make sure the component requests the focus when appropriate. For custom components, implement a mouse listener that calls the requestFocusInWindow method when the component is clicked
Sample Code for Key Event Listener:
public class KeyEventDemo ... implements KeyListener ... {
...//where initialization occurs:
typingArea = new JTextField(20);
typingArea.addKeyListener(this);
//Uncomment this if you wish to turn off focus
//traversal. The focus subsystem consumes
//focus traversal keys, such as Tab and Shift Tab.
//If you uncomment the following line of code, this
//disables focus traversal and the Tab events
//become available to the key event listener.
//typingArea.setFocusTraversalKeysEnabled(false);
...
/** Handle the key typed event from the text field. */
public void keyTyped(KeyEvent e) {
displayInfo(e, "KEY TYPED: ");
}
/** Handle the key-pressed event from the text field. */
public void keyPressed(KeyEvent e) {
displayInfo(e, "KEY PRESSED: ");
}
/** Handle the key-released event from the text field. */
public void keyReleased(KeyEvent e) {
displayInfo(e, "KEY RELEASED: ");
}
...
private void displayInfo(KeyEvent e, String keyStatus){
//You should only rely on the key char if the event
//is a key typed event.
int id = e.getID();
String keyString;
if (id == KeyEvent.KEY_TYPED) {
char c = e.getKeyChar();
keyString = "key character = '" + c + "'";
} else {
int keyCode = e.getKeyCode();
keyString = "key code = " + keyCode
+ " ("
+ KeyEvent.getKeyText(keyCode)
+ ")";
}
int modifiersEx = e.getModifiersEx();
String modString = "extended modifiers = " + modifiersEx;
String tmpString = KeyEvent.getModifiersExText(modifiersEx);
if (tmpString.length() > 0) {
modString += " (" + tmpString + ")";
} else {
modString += " (no extended modifiers)";
}
String actionString = "action key? ";
if (e.isActionKey()) {
actionString += "YES";
} else {
actionString += "NO";
}
String locationString = "key location: ";
int location = e.getKeyLocation();
if (location == KeyEvent.KEY_LOCATION_STANDARD) {
locationString += "standard";
} else if (location == KeyEvent.KEY_LOCATION_LEFT) {
locationString += "left";
} else if (location == KeyEvent.KEY_LOCATION_RIGHT) {
locationString += "right";
} else if (location == KeyEvent.KEY_LOCATION_NUMPAD) {
locationString += "numpad";
} else { // (location == KeyEvent.KEY_LOCATION_UNKNOWN)
locationString += "unknown";
}
...//Display information about the KeyEvent...
}
}
Try Java Docs/ tutorials for more help.
Web pages end up running on client browsers, which understands only HTML, CSS and Javascript. You can accomplish this only with Javascript, with no server side code.
If you really want to play around and do it by Java code, I could suggest two (of many) approaches:
Develop a taglib for use along with JSP pages. That Taglib would print a virtual keyboard on screen, and along with some javascript code, would listen to keyboard events and highlight them on the virtual keyboard (this is more of a hybrid solution).
Play around with Google's GWT. It would let you create your virtual keyboard and event listeners entirely with java code, and result in a web page, without the need for you to develop HTML, CSS and Javascript. GWT basically gives you a way to construct a web page similar as creating a Swing GUI, and translates that to HTML, CSS and Javascript code.
http://code.google.com/p/google-web-toolkit
http://www.gwtproject.org