please help? simple fix that someone who knows bout java can prolly help me
im making kind of like a shooting game where u have a rectangle on side of the screen and they shoot each other, im trying to make one fo the rectangles move up with the letter 'o' but it doesnt seem to be working, thanks.
Code Java:
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.ImageObserver;
import java.io.*;
import java.net.*;
import java.applet.AudioClip;
import javax.imageio.ImageIO;
import sun.audio.*;
public class Shooter extends JFrame{
int WIDTH = 500;
int HEIGHT = 500;
int x,y = 0;
int height,width = 0;
int health = 10;
Rectangle p1 = new Rectangle(480, 210, 15, 100);
Rectangle p2 = new Rectangle(0,210,15,100);
public Shooter(){
super("Shooter");
setSize(WIDTH,HEIGHT);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBackground(Color.black);
setVisible(true);
}
public class Player implements KeyListener{
public void run(){
addKeyListener(this);
}
public void keyPressed(KeyEvent event) {}
public void keyReleased(KeyEvent event) {}
public void keyTyped(KeyEvent event) {
if(event.getKeyChar()=='o'){
p1.y--;
}
}
}
public void paint(Graphics g){
g.setColor(Color.white);
g.fillRect(0,210,15,100);
g.fillRect(p1.x,p1.y, 15, 100);
Rectangle p1 = new Rectangle(480, 210, 15, 100);
Rectangle p2 = new Rectangle(0,210,15,100);
}
public static void main(String args[]){
new Shooter();
}
}
Re: please help? simple fix that someone who knows bout java can prolly help me
Quote:
Originally Posted by
joelmeler
it doesnt seem to be working.
So what does it do instead?
I just took a copy of your code and tried to run it. It doesn't paint correctly, so I would start there.
Re: please help? simple fix that someone who knows bout java can prolly help me
Try debugging your code by adding printlns to all the methods that you expect to be called to show that they are being called.
Re: please help? simple fix that someone who knows bout java can prolly help me
I looked at your code, fixed the Override and made Shooter actually call Player.. it prints two white boxes on either side of a black background
Code Java:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Shooter extends JFrame
{
/**
* serialVersionUID
*/
private static final long serialVersionUID = -4420393074204026863L;
int WIDTH = 500;
int HEIGHT = 500;
int x,y = 0;
int height,width = 0;
int health = 10;
Rectangle p1 = new Rectangle(480, 210, 15, 100);
Rectangle p2 = new Rectangle(0,210,15,100);
public Shooter(){
super("Shooter");
setSize(WIDTH,HEIGHT);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBackground(Color.black);
setVisible(true);
Thread t = new Thread(new Player()); //This actually calls the Player class
t.start(); //Start it!
}
public class Player implements KeyListener, Runnable
{
public Player()
{
}
@Override
public void run()
{
addKeyListener(this);
}
@Override
public void keyPressed(KeyEvent event) {}
@Override
public void keyReleased(KeyEvent event) {}
@Override
public void keyTyped(KeyEvent event) {
if(event.getKeyChar()=='o'){
p1.y--;
}
}
}
@Override
public void paint(Graphics g)
{
g.setColor(Color.white);
g.fillRect(0,210,15,100);
g.fillRect(p1.x,p1.y, 15, 100);
p1 = new Rectangle(480, 210, 15, 100);
p2 = new Rectangle(0,210,15,100);
}
public static void main(String args[])
{
new Shooter();
}
}
but then the movement wasn't working.. so I switched it into three classes.
Shooter.java
Code Java:
import javax.swing.*;
import java.awt.*;
public class Shooter extends JPanel
{
/**
* serialVersionUID
*/
private static final long serialVersionUID = -4420393074204026863L;
public JFrame frame;
static int WIDTH = 500;
static int HEIGHT = 500;
int x,y = 0;
int height,width = 0;
int health = 10;
public Rectangle p1 = new Rectangle(480, 210, 15, 100);
public Rectangle p2 = new Rectangle(0,210,15,100);
public Shooter(JFrame frame)
{
this.frame = frame;
}
@Override
public void paint(Graphics g)
{
Graphics2D g2d = (Graphics2D) g; //I prefer Graphics2D, not sure if it matters
g2d.fillRect(0, 0, HEIGHT, WIDTH); //Clear the background (This is necessary or it doesn't paint right..)
g2d.setColor(Color.white);
g2d.fillRect(0,210,15,100);
g2d.fillRect(p1.x,p1.y, 15, 100);
Toolkit.getDefaultToolkit().sync();
//p1 = new Rectangle(480, 210, 15, 100);
//p2 = new Rectangle(0,210,15,100);
}
public synchronized void decrementY() //Make guy go up
{
p1.y--;
repaint();
}
public synchronized void incrementY() //Make guy go down
{
p1.y++;
repaint();
}
}
Player.java
Code java:
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class Player implements KeyListener
{
private Shooter shooter;
public Player(Shooter shooter)
{
this.shooter = shooter;
}
@Override
public void keyPressed(KeyEvent event) {}
@Override
public void keyReleased(KeyEvent event) {}
@Override
public void keyTyped(KeyEvent event)
{
if(event.getKeyChar()=='o')
{
shooter.decrementY(); //Make the guy go up
}else if(event.getKeyChar()=='l')
{
shooter.incrementY(); //Make the guy go down
}
}
}
Main.java
Code Java:
import java.awt.Color;
import javax.swing.JFrame;
class Main extends JFrame
{
private static final long serialVersionUID = 1L;
public Main()
{
super("Shooter");
Shooter s = new Shooter(this);
add(s); //Add the Shooter
addKeyListener(new Player(s)); //Add the key listener
setBackground(Color.black); //Set background
setDefaultCloseOperation(EXIT_ON_CLOSE); //Make it so it closes
setSize(Shooter.WIDTH, Shooter.HEIGHT);
setLocationRelativeTo(null); //Not sure
setVisible(true); //Set visible
setResizable(false); //Don't let it get resized
}
public static void main(String[] args)
{
new Main(); //Self-Explanatory
}
}
Sorry that I kind of did it for you.. but I added lots of comments
Re: please help? simple fix that someone who knows bout java can prolly help me
Quote:
Sorry that I kind of did it for you.
What good is giving the OP code? Did you take the time to comment what the problem was, tell the OP how you found the problem and the approach you took to solve it? Or did you just want to show-off?
Read this: http://www.javaprogrammingforums.com...n-feeding.html
You should override paintComponent in a Swing class. Don't use paint()
Re: please help? simple fix that someone who knows bout java can prolly help me
While I see what you are going at, I find it about 95% useless to just say "Just add printlns" at every problem, operational code is much more useful in both understanding and debugging. I tried adding println's to what he was doing.. it didn't help at all. This is the same argument as people have on this against this
Re: please help? simple fix that someone who knows bout java can prolly help me
As for paintComponent vrs paint, thanks I didn't realize there was a difference
Re: please help? simple fix that someone who knows bout java can prolly help me
If you don't have an interactive debugger, the only way to see what your code is doing is by adding printlns. This is a useful skill for programmers to know: how and where to add print outs.
Believe it or not most beginning programmers don't know how to debug their code.
Many of them post their code here and wait for people like you to fix it for them. What do they learn?
Quote:
I tried adding println's to what he was doing.. it didn't help at all.
Then obviously you don't know how to use them. The first thing you could see was that the key listener methods were NOT being called. Why was that??? Because it had never been added as a listener.
Re: please help? simple fix that someone who knows bout java can prolly help me
Oh that I saw without printlns.. it was just obvious.. hence the Threads o.O I suppose you could use a println there.. What I was more worried about at that stage was that the method wasn't printing quite right.
Re: please help? simple fix that someone who knows bout java can prolly help me
However I admit that println's could help, but I strongly feel that the use of every thread has a "Add println's" post on it. If you looked at it he was trying to use the run method like a main method. Anyway Dead Thread