Question:
Need help with my java code. It randomly does not work.Help plz.?
anonymous
2009-11-17 07:41:32 UTC
import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import java.awt.Image;
import static java.lang.System.*;

public class KeypressDemo {

public static void main(String args[]){
Image play;
play = new ImageIcon ("M:\\My Pictures\\chuck_norris_cat.jpg").getImage();
JFrame j=new JFrame();
DisplayMode DM = new DisplayMode(800,600,32,60);//width, height, bit-resolution, refresh rate
GraphicsEnvironment enviro = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice device = enviro.getDefaultScreenDevice();
//j.setVisible(true);
j.setUndecorated(true);
device.setFullScreenWindow(j);
device.setDisplayMode(DM);

j.getContentPane().add(new KeypressPanel(play));

}
}

class KeypressPanel extends JPanel implements KeyListener {

static int x,y; //initialize
Rectangle land = new Rectangle(375,275,50,50);
Rectangle player = new Rectangle(100,100,10,10);
Image edg;
public KeypressPanel(Image play){
edg = play;
Toolkit tk = getToolkit();
Cursor HiddenCursor = tk.createCustomCursor(tk.getImage("null.jpg"), new Point(0,0),"null");
setCursor(HiddenCursor);

setBackground(Color.red);
x=100; y=100;
addKeyListener(this);
}

public void paint(Graphics g){

super.paint(g);
//g.setColor(Color.black);
g.drawImage(edg,player.x,player.y,50,50, null);
g.setColor(Color.magenta);
g.fillRect(land.x,land.y,50,50);
requestFocus();
}

// To fulfill our obligations as a KeyListener, we implement the following...
public void keyPressed(KeyEvent e){ }
public void keyReleased(KeyEvent e){ }
public void keyTyped(KeyEvent e){
switch(e.getKeyChar()){
case 'w': player.y=player.y-10;
if(player.intersects(land))
{
player.y=player.y+10;
}break;
case 'q': player.y=player.y-10;x=x-10;
if(player.x>=380 /*left side*/ && player.x<=410/*right side*/ && player.y<=310 /*bottom*/ && player.y>=280/*top*/)
{
player.y=player.y+10;player.x=player.x+10;
}break;
case 'e': player.y=player.y-10;player.x=player.x+10;
if(player.x>=380 /*left side*/ && player.x<=410/*right side*/ && player.y<=310 /*bottom*/ && player.y>=280/*top*/)
{
player.y=player.y+10;player.x=player.x-10;
}break;
case 'z': player.y=player.y+10;player.x=player.x-10;
if(player.x>=380 /*left side*/ && player.x<=410/*right side*/ && player.y<=310 /*bottom*/ && player.y>=280/*top*/)
{
player.y=player.y-10;player.x=player.x+10;
}break;
case 'c': player.y=player.y+10;player.x=player.x+10;
if(player.x>=380 /*left side*/ && player.x<=410/*right side*/ && player.y<=310 /*bottom*/ && player.y>=280/*top*/)
{
player.y=player.y-10;player.x=player.x-10;
}break;
case 'a': player.x=player.x-10;
if(player.x>=380 /*left side*/ && player.x<=410/*right side*/ && player.y<=310 /*bottom*/ && player.y>=280/*top*/)
{
player.x=player.x+10;
}break;
case 'd': player.x=player.x+10;
if(player.x>=380 /*left side*/ && player.x<=410/*right side*/ && player.y<=310 /*bottom*/ && player.y>=280/*top*/)
{
player.x=player.x-10;
}break;
case 's': player.y=player.y+10;
if(player.x>=380 /*left side*/ && player.x<=410/*right side*/ && player.y<=310 /*bottom*/ && player.y>=280/*top*/)
{
player.y=player.y-10;
}break;
case 'l': System.exit(0); break;
case '1': out.println("bang bang");break;
case '2': out.println("slide!");break;
case '3': out.println("quick draw...change!");break;
default:
}
if(x>=380 /*left side*/ && x<=410/*right side*/ && y<=310 /*bottom*/ && y>=280/*top*/)
{
x=10;
y=10;
}
if(player.y>=590)
player.y=590;
else if(player.y<=0)
player.y=0;
if(player.x>=790)
player.x=790;
else if(player.x<=0)
player.x=0;
repaint();
}
}
Three answers:
anonymous
2009-11-18 16:00:38 UTC
THIS IS DIRECTED AT R, I am the person who asked, but lost my account info, so I just made a new one. Anyway, the program is supposed to make a fullscreen window with no border, cursor or task bar. In said screen I wanted a picture to move ( the image code is near the top ) . The program does all this, however, if I run the program, let's say five times without touching the code, 3 out of 5 times it will work, the other two times I just get a blank screen and I have to alt-tab out of it.
lasley
2017-01-09 13:09:41 UTC
Agha's answer is a robust one. It provides you an know-how of ways it relatively works. besides the undeniable fact that, Java's String API has that built into it. merely make your characters in a string, and use the toLowerCase() function, like this String mixed = "CapiTals AND LoWERCaseS mutually"; String decrease = mixed.toLowerCase(); gadget.out.println(mixed); gadget.out.println(decrease); take exhilaration in!
r
2009-11-18 09:42:22 UTC
Try reposting with comments in your code. Also, give more of a description than "randomly does not work". What is the program suppose to do? How does it not work? Have you tried inserting println's to help pinpoint what lines are being executed, what lines are not?


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...