Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Rannoc
Java KeyListener isn't quite working for me?
I asked this already, but with adding more details, I ran out of space.
this is a test program I wrote to try to get the KeyListener to work, My file builds correctly, there are no errors that I can see. The mouse does the job and changes the color, but they keyboard doesn't. I am fuddled. thanks for any help you can offer
Viewer class:
import javax.swing.*;
import java.awt.*;
public class Viewer {
public static void main(String[] args) {
JFrame theGUI = new JFrame("Keyboard Test");
Container pane = theGUI.getContentPane();
MyPanel panel = new MyPanel();
theGUI.setSize(200, 200);
theGUI.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);
pane.add(panel);
theGUI.setVisible(true);
}
}//end of Viewer
//////////////////////////////////////…
//paint component class:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.Random;
public class MyPanel extends JPanel implements KeyListener, MouseListener{
int r = 50;
int gr = 50;
int b = 50;
Random random = new Random();
MyPanel(){
this.addKeyListener(this);
this.addMouseListener(this);
}
public void paintComponent(Graphics g){
super.paintComponent(g);
setBackground(new Color(r,gr,b));
}
public void changeColor(){
r = random.nextInt(256);
gr = random.nextInt(256);
b = random.nextInt(256);
repaint();
}
public void keyTyped(KeyEvent e) {changeColor();}
public void keyPressed(KeyEvent e) {changeColor();}
public void keyReleased(KeyEvent e) {changeColor();}
public void mouseExited(MouseEvent e) {changeColor();}
public void mouseEntered(MouseEvent e) {changeColor();}
public void mouseReleased(MouseEvent e) {changeColor();}
public void mousePressed(MouseEvent e) {changeColor();}
public void mouseClicked(MouseEvent e) {changeColor();}
}
1 AnswerProgramming & Design10 years agoJava KeyListener isn't quite working for me.?
It has been a few years since I've programmed in Java, so I'm trying to refresh my memory. I am making a snake game, and I can't quite get they keyboard input to work. My file builds correctly, there are no errors that I can see. I seem to remember that there was an easy fix to this problem, by moving the "addKeyListener(new keyListener());" to a different spot in the project. Either somewhere inside the Viewer class, or in the actual game class.
here is the relevant code
File: Games.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Scanner;
import java.util.Random;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.MemoryImageSource;
public class Games extends JPanel{
int sx = 200;
int sy = 700;
int s1dir = 1;
int counter=0;
Games(){
setMouse();
editImages();
addMouseListener(new PanelListener());
addMouseMotionListener(new PanelListener());
addMouseWheelListener(new PanelListener());
addKeyListener(new keyListener());
}
private Random random = new Random();
private javax.swing.Timer timer;
public void paintComponent(Graphics g){
super.paintComponent(g);
//bunch of graphics code here
timer = new javax.swing.Timer(10, new MoveListener());
timer.start();
}//end of paintComponent
private void setMouse(){...}//replaces the mouse with a custom image
private void editImages(){...}//resizes background images, and snake images
private class MoveListener implements ActionListener{...}//for the timer
// I tested the program with a mouse instead of a keyboard, and that works fine
private class PanelListener extends MouseAdapter {...}//for mouse tests
private class keyListener extends KeyAdapter{
public void keyTyped(KeyEvent e) {
//do nothing here yet
}//end keyTyped
public void keyPressed(KeyEvent e) {
sx = 200;
sy = 700;
}//end keyPressed
public void keyReleased(KeyEvent e) {
}//end keyReleased
}//end keyListener
}//end Games class
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
File - ClassViewer.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ClassViewer {
private static JFrame theGUI = new JFrame("Game play");
private static Container pane = theGUI.getContentPane();
private static Games game = new Games();
public static void main(String[] args) {
setWindow();
menu();
}//end main
//sets up the window
private static void setWindow(){
theGUI.setSize(850, 850);
theGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
theGUI.setResizable(false);
theGUI.setVisible(true);
}//end setWindow
//starts menu
private static void menu(){
theGUI.setTitle("Grid Games");
pane.add(game);
theGUI.setVisible(true);
}//end menu
}//end ClassViewer
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Thanks for the help
1 AnswerProgramming & Design10 years agoTi 89 Solve function?
Is there any way to use the values given by using the solve function without manually copying them (for use inside a program).
I am dealing with quadratics, so the answer is like
"x = -2 or x = -1"
I want a way to assign the values -2 and -1 to two different variables, or to a list etc.
Thanks
1 AnswerProgramming & Design1 decade agopaintIcon method outside of paintComponent in JAVA?
I have 3 classes and I am making an RPG type of game, and I ran into a little problem I can't figure our. I need to have a method in one class that draws the monster, but the paintComponent is in a different class, Normally, when you use paintIcon inside the paintComponent, you use the word 'this' to refer to the paintComponent, but I am wondering how you refer to it when outside. I'm posting the relevant code below
code in monster class, the method i want to use to draw it :
public void monsterDraw(Graphics g, int x, int y){
monsterImage.paintIcon(???????, g, x, y);
return;
}
monsterImage is already an IconImage
like I said, if it was in the paintComponent, i would put 'this' in for the question marks, but not now.
this is the error that pops, or something like it for whatever I try.
paintIcon(java.awt.Component, java.awt.Graphics,int,int) in javax.swing.ImageIcon cannot be applied to (monster, java.awt.Graphics,int,int)
I want to use the paintComponent in a class called SothComponent.
please tell me if any more details are needed, or if I am trying to do the impossible.
thanks ahead of time.
3 AnswersProgramming & Design1 decade agoA TI 89 question about functions.?
I just recently received my TI 89 (Titanium), and I'm still not quite sure of the calculator's capabilities.
I have these two functions I am using for physics right now, quite simple functions, just very repetitive.
f(a,b,c) = √(a²+b²-2ab*cos(θ))
and
h(a,b,c) arccos([f(a²-b²-(f(a,b,c))²] / [-2b(f(a,b,c))])
i use the same a, b, and c for both equations
the equations aren't really important, they work fine, but i was wondering if there was a way to make a function that gives two answers, when i just plug in the a, b, and c once.
for a simplified example
f(x) = 3x
g(x) = 5/x
h(2) = 6 and 5/2 (h is f(x) and g(x), just in one function)
i want to know if there is a way that I can just plug x into one equation, and have both answers come up like it does when you solve for x²-4=0, it gives you x = -2 or x = 2.
the one way i did find, but it isn't any easier than just typing both is to make a program, that takes a,b, and c as parameters. but i was wondering if there was a different way.
1 AnswerMathematics1 decade ago