i have been trying to make a ball change its direction as any key is pressed. if the ball is moving sideways as the key will be pressed the ball will start moving downward and as it touch the bottom it moves back in upward position...i think i have written the right code , i cant find anything wrong in it.
so can someone please tell me what is problem in this code ?
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.geom.Ellipse2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ASS2 extends JFrame {
public static void main(String args[]) {
ASS2 g = new ASS2();
g.setLayout(new BorderLayout());
g.setSize(500, 500);
MyPanel mp = new MyPanel();
g.add(mp);
mp.setSize(500, 500);
mp.setBackground(Color.black);
//mp.addKeyListener(mp);
g.setVisible(true);
g.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
class MyPanel extends JPanel implements KeyListener {
{
addKeyListener(this);
}
int xpos = 20, ypos = 200;
int xtop = 15, ytop = 15;
int xtemp = 250, ytemp = 250;
int xbot = 450, ybot = 400;
int flag = 1, flag1 = 1;
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.white);
g2d.fill(new Ellipse2D.Double(xpos, ypos, 50, 50));
if (xpos < xbot && flag == 1) {
xpos++;
if (xpos == xbot) {
flag = 0;
}
} else if (xpos > xtop && flag == 0) {
xpos--;
if (xpos == xtop) {
flag = 1;
}
}
try {
Thread.sleep(05);
} catch (Exception e) {
}
repaint(1000);
}
public void keyPressed(KeyEvent ae) {
}
public void keyReleased(KeyEvent ae) {
Object t = ae.getKeyCode();
if (t.equals(KeyEvent.VK_DOWN)) {
if (ypos < ybot && flag1 == 1) {
ypos++;
if (ypos == ybot) {
flag1 = 0;
}
} else if (ypos > ytop && flag1 == 0) {
ypos--;
if (ypos == ytop) {
flag1 = 1;
}
}
repaint();
} else if (t.equals(KeyEvent.VK_RIGHT)) {
if (xpos < xbot && flag == 1) {
xpos++;
if (xpos == xbot) {
flag = 0;
}
} else if (xpos > xtop && flag == 0) {
xpos--;
if (xpos == xtop) {
flag = 0;
}
}
repaint();
}
}
}
Welcome to the wonderful world of "why you shouldn't use KeyListeners".
Basically, KeyListener will only raise events when the component that the listener is attached to is focusable AND has focus.
Instead, you should be using Key Bindings which allow you to control the focus level at which they will trigger key events.
Do not use Thread.sleep in any method that is called within the EDT, especially paint methods.
Do not call repaint(1000); inside any paint method or call any method that might trigger a repaint from within paint methods
Do not modify state's within paint methods, paint methods paint, that's all.
Use some kind of "update" thread/process which is responsible for updating the game model and requesting an update to the view. java.swing.Timer is good simple choice to start with. See How to use Swing Timers
When the Swing Timer becomes to limiting for what you are trying to do and you start exploring the use of Threads, don't modify any UI components outside the EDT. See Concurrency in Swing for more details
Always start your programs from within the context of the EDT, see Initial Threads for more details...
Related
It's like if you were in a mirror maze, you might have trouble finding one specific reflections of yourself. So you would have multiple reflections of yourself, confusing you when you try to find that direct reflection of yourself.
Sadly I cannot post an image..........
So...
Character is "C"
It goes from
C
to
C C
when I attempt to move right.
package WASD;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.*;
public class WASD extends JFrame implements KeyListener, ActionListener{
private int playerX = 400;
private int playerY = 400;
int C = 1;
private int RESTART = 0; // Three ENTER = Restart
private Timer timer;
JFrame j = new JFrame();
private int previousX = -50;
private int previousY = -50;
public WASD() {
JPanel panel = new JPanel();
setTitle("Not supposed to be a painting program.");
setSize(2000, 2000);
timer = new Timer(8, this);
this.getContentPane().add(panel);
addKeyListener(this);
setVisible(true);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args){
WASD W = new WASD();
}
public void paint(Graphics g) { //TODO paint is only working once, then failing forever.
//////////////////////////
g.setColor(Color.BLACK);
g.drawRect(0, 0, 3000, 3000); // Background
if(C == 1) g.setColor(Color.RED);
else if (C == 2) g.setColor(Color.BLUE);
else if (C == 3) g.setColor(Color.GREEN);
else if (C == 4) g.setColor(Color.YELLOW);
else if(C>4){
g.setColor(Color.GRAY);
try {
Thread.sleep(1000); //Gray pauses for a second I guess...
}
catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
C = 1;
}
g.fillRect(playerX, playerY, 50, 50); // Player/
g.setColor(Color.BLACK);
g.fillRect(previousX, previousY, 50, 50);
g.dispose(); //Last thing
}
#Override
public void actionPerformed(ActionEvent e) {
timer.start();
repaint();
}
#Override
public void keyPressed(KeyEvent K) {
// TODO Auto-generated method stub
int e = K.getKeyCode();
repaint(); //timer.start(); does not affect this. g.dispose() is unavailable.
/*
If this repaint is used, then the previous locations
that the player was at will be painted as well.
Basically, instead of moving, all instances
made will not be deleted. #PaintingApplication
However, if it is not used, then the CHARACTER will not move at all.
It seems that the problem is not about
playerX, or playerY,
instead, it is about the program not refreshing.
*/
if((e == KeyEvent.VK_RIGHT || e == KeyEvent.VK_D) && playerX<2000){
playerX += 200;
}
else if((e == KeyEvent.VK_LEFT || e == KeyEvent.VK_A) && playerX>0){
playerX -= 200;
}
else if((e == KeyEvent.VK_UP || e == KeyEvent.VK_W) && playerY>0){
playerY -= 200;
}
else if(e == KeyEvent.VK_DOWN || e == KeyEvent.VK_S && playerY<2000){ // Easter egg bug 1
playerY += 200;
}
else if (e == KeyEvent.VK_C){
C++;
}
else if(e == KeyEvent.VK_ENTER){
if(RESTART == 3){
playerX = 400;
playerY = 400;
RESTART = 0; //TODO
repaint();
}
else{
RESTART++;
}
}
else{
RESTART = 0;
}
}
#Override public void keyTyped(KeyEvent arg0) {}
#Override public void keyReleased(KeyEvent arg0) {}
}
I apologize for making such a simple mistake. The problem is in the function "paint". The line
g.drawRect(0, 0, 3000, 3000); //Outline of Rectangle
should be
g.fillRect(0, 0, 3000, 3000); //Actually filling the background.
I'm creating a Java game for fun and I want to add many things to a JFrame at once. But for some reason, one class and the main method class executes, but the third class containing the second object I want to add doesn't execute. I'm still new to Java so I might get some terms wrong.
Basically I have 3 classes:
main.java (main method class + JFrame constructor class)
Infout.java (class that draws a keyboard-controlled circle + some stationary rectangles)
obj2.java (class that draws a second stationary circle)
Here is the code:
main.java
import javax.swing.JFrame;
public class main
{
public static void main(String[] args)
{
JFrame frame = new JFrame();
Infout m = new Infout();
obj2 o = new obj2();
frame.add(o);
frame.add(m);
frame.setVisible(true);
frame.setDefaultCloseOperation(3);
frame.setSize(300, 400);
frame.setTitle("Circle");
}
}
Infout.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
public class Infout extends JPanel implements ActionListener, KeyListener {
Timer t = new Timer(5, this);
double x = 0, y = 0, velx = 0, vely = 0;
public Infout(){
t.start();
addKeyListener(this);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.fill(new Ellipse2D.Double(x, y, 40, 40));
g2.fill(new Rectangle2D.Double(0, 270, 300, 5));
g2.fill(new Rectangle2D.Double(140, 270, 5, 300));
g2.fill(new Rectangle2D.Double(140, 60, 70, 5));
g2.fill(new Rectangle2D.Double(50, 140, 5, 70));
g2.fill(new Rectangle2D.Double(150, 130, 5, 40));
g2.fill(new Rectangle2D.Double(190, 210, 40, 5));
if (x >= 120 && y >= 270) {
System.out.println("sum1 has reached tha corner");
g.drawString("You win!",115,35);
}
if (x <= 120 && y >= 270) {
System.out.println("sum1 has reached tha corner");
g.drawString("You lose!",115,35);
}
if (x == 120 && y >= 270){
velx = 0;
vely = 0;
}
if (x == 31.5 && y <= 200 && y >= 100){
velx = 0;
}
if (x == 132 && y <= 170 && y >= 100){
velx = 0;
}
if (x <= 190 && x >= 120 && y == 42){
velx = 0;
}
if (x <= 210 && x >= 171 && y == 192){
velx = 0;
}
}
public void actionPerformed(ActionEvent e) {
System.out.println("x "+x+"y "+y);
repaint();
x += velx;
y += vely;
if (x < 0 || x > 260)
{
velx = 0;
vely = 0;
}
if (y < 0 || y > 340)
{
velx = 0;
vely = 0;
}
}
public void up() {
vely = -1.5;
velx = 0;
}
public void down() {
vely = 1.5;
velx = 0;
}
public void left() {
velx = -1.5;
vely = 0;
}
public void right() {
velx = 1.5;
vely = 0;
}
public void keyPressed(KeyEvent e) {
int code = e.getKeyCode();
if (code == KeyEvent.VK_UP) {
up();
}
if (code == KeyEvent.VK_DOWN) {
down();
}
if (code == KeyEvent.VK_RIGHT) {
right();
}
if (code == KeyEvent.VK_LEFT) {
left();
}
}
public void keyTyped(KeyEvent e) {}
public void keyReleased(KeyEvent e) {}
}
obj2.java
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import javax.swing.JPanel;
public class obj2 extends JPanel {
public void paintComponent(Graphics g1)
{
super.paintComponent(g1);
Graphics2D g3 = (Graphics2D)g1;
Ellipse2D circle = new Ellipse2D.Double(50.0D, 50.0D, 40.0D, 40.0D);
g3.fill(circle);
}
public obj2(){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
So basically, I get no compile errors, but even though I created an instance of each class variable in the main method, and added them both to the JFrame, they cannot both execute at once. If I comment out obj2 from the main method, Infout will show up. If I comment out Infout from the main method, obj2 will show up. But not both at once. If I try both at once, only Infout shows up.
As you may have saw, I thought maybe it had something to do with multithreading so I added some code for multithreading that you may have noticed but I'm sure it's wrong since I only learned about multithreading like an hour ago.
May someone pleeeease help me figure this out? I've tried everything I know to solve it, but it just won't work :C.
I would absolutely LOVE example code of maybe a simple program you guys could quickly whip up showing me how this works. I would even love more if you could explain why/how it works the way it does. I like learning! :)
Thanks!
Ab
By default a JFrame uses a BorderLayout. When you use the add(...) method without a constraint the component will be added tot he CENTER or the BorderLayout. However, only one component (panel) can be displayed in the CENTER, so only the last panel added is visible.
Read the section from the Swing tutorial on How to Use the BorderLayout for more information and examples.
Generally when I see code like you have you can display the components on top of one another with code like:
panel1.add( panel2 );
frame.add( panel1 );
Or if you want the coponents side by side then you can change the layout manager of the content pane to use a FlowLayout. Again, read the tutorial. There are examples for the FlowLayout and other layout managers.
I'm trying to make the sprite speed-up gradually on button press and not to move constant speed only. Also set a max-speed limit. I hope you understand what i mean.
timer = new Timer(5, this);
timer.start();
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D)g;
g2d.drawImage(image, x, y, this); //x,y = position
Toolkit.getDefaultToolkit().sync();
g.dispose();
}
private class TAdapter extends KeyAdapter {
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT) {
dx = -1;
}
if (key == KeyEvent.VK_RIGHT) {
dx = 1;
}
if (key == KeyEvent.VK_UP) {
dy = -1;
}
if (key == KeyEvent.VK_DOWN) {
dy = 1;
}
}
}
public void actionPerformed(ActionEvent e) {
x += dx;
y += dy;
repaint();
}
There are several things (initially) wrong with your example code...
You are overriding the paint method. It is recommend that you override the paintComponent method instead. If you are overriding the paint method of a top level container, like JFrame, then it is recommended that you don't. Instead, use something like JPanel as the bases for your custom painting...
You are disposing of the Graphics context that is past to you. This is VERY dangerous, as this will prevent anything else from been painted. The Graphics context is a shared resources, everything that needs to be updated during this repaint cycle will using the same Graphics context.
You are using a KeyListener. KeyListener suffers from focus issues. This can easily be remedied through the use of the Key Binding API. Key bindings are also more flexible, as they separate the physical key from the action, allowing you to associate the action with different keys with little effort and/or reuse the underlying action (such as with buttons).
So. For your question. You need to know...
The current speed...
The minimum allowable speed...
The maximum allowable speed...
You will also want to maintain the current position of the object you are altering.
This example doesn't actually move the "player" so much as it moves the background. The background position is altered by the xDelta, which is the speed of change...
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class TestSpeed {
public static void main(String[] args) {
new TestSpeed();
}
public TestSpeed() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private BufferedImage background;
// The current position of the background
private int xPos = 0;
// The speed/delta that the xPos is changed...
private int xDelta = 0;
public TestPane() {
Timer timer = new Timer(40, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if (xPos < -(getWidth())) {
xPos = 0;
}
xPos -= xDelta;
repaint();
}
});
timer.setRepeats(true);
timer.setCoalesce(true);
timer.start();
InputMap im = getInputMap(WHEN_IN_FOCUSED_WINDOW);
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0), "slower");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), "faster");
ActionMap am = getActionMap();
am.put("slower", new AbstractAction() {
#Override
public void actionPerformed(ActionEvent e) {
setSpeed(-1);
}
});
am.put("faster", new AbstractAction() {
#Override
public void actionPerformed(ActionEvent e) {
setSpeed(1);
}
});
}
protected void setSpeed(int delta) {
xDelta += delta;
// Check the change in speed to ensure it's within the appropriate range
if (xDelta < 0) {
xDelta = 0;
} else if (xDelta > 9) {
xDelta = 9;
}
}
#Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
#Override
public void invalidate() {
background = null;
super.invalidate();
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int x = xPos;
g.setColor(Color.DARK_GRAY);
while (x < getWidth()) {
g.drawLine(x, 0, x, getHeight());
x += 15;
}
int width = getWidth();
int height = getHeight();
x = (width / 2) - 5;
int y = (height / 2) - 5;
g.setColor(Color.RED);
g.fillOval(x, y, 10, 10);
}
}
}
You need to define and store the values for max speed, actual speed and the speed increment.
The simplest way to define the speed increment, and should try it first, is to define a constant speed increment. Based on the provided code:
int maxspeed = 5;
int speed = 1;
int acceleration = 1;
timer = new Timer(5, this);
timer.start();
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D)g;
g2d.drawImage(image, x, y, this); //x,y = position
Toolkit.getDefaultToolkit().sync();
g.dispose();
}
private class TAdapter extends KeyAdapter {
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT) {
dx = -acceleration;
}
if (key == KeyEvent.VK_RIGHT) {
dx = acceleration;
}
if (key == KeyEvent.VK_UP) {
dy = -acceleration;
}
if (key == KeyEvent.VK_DOWN) {
dy = acceleration;
}
}
}
public void actionPerformed(ActionEvent e) {
if (speed < maxspeed) {
speed += acceleration;
}
x += dx * speed;
y += dy * speed;
repaint();
}
As I don't really know the context of the problem, or the goal to achieve, I didn't include any way to slow the sprite down again, once maxspeed is hit.
Some interval for speed gains may also be considered. With the above code, the updates to speed would be fast enough that you probably wouldn't notice them.
This is the screen that im trying to repaint but it is not repainting properly.
public class arenaScreenBuild extends JPanel{
int pX=200, pY=150;
public void updateScreen(){
repaint();
}
public void paintComponent(Graphics g) {
g.drawString("x:"+pX, 535, 525);
g.drawString("y:"+pY, 535, 545);
}
public void refreshXY(int x, int y){
pX=x;
pY=y;
System.out.println("Refreshed X&Y");
updateScreen();
}
}
This is the screen displaying the graphics. When run, every time i move(press the right arrow key), it displays "Refreshed X&Y" but even though it calls the updateScreen() method, the displayed items are not redrawn. The code, if it had worked, should display x:XVALUE, y:YVALUE after the "refreshed X&Y".
public class ArenaKeys extends KeyAdapter {
arenaScreenBuild arenaBG = new arenaScreenBuild();
int xPos = 0, playerFace = 4,xPPos = 200, yPPos = 150;
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
if (keyCode == e.VK_RIGHT) {
if (xPos <= 3250)
if (((xPos + xPPos) >= 825) && ((xPos + xPPos) <= 910)
&& (yPPos >= 170) && (yPPos <= 255)) {
} else if (((xPos + xPPos) >= 1325) && ((xPos + xPPos)<= 1410)
&& (yPPos >= 170) && (yPPos <= 255)) {
} else
xPos += 5;
}
arenaBG.refreshXY(xPPos+xPos,yPPos);
}
}
EDIT: *Turns out that it does work but what i was doing was adding a Drawpanel on top of another drawpanel and this code was for the one underneath so it wasn't letting the bottom code update, i solved this by merging together the two codes for both drawpanels.*
In keyPressed call EventQueue.invokeLater(new Runnable()), as keyPressed is called on the event handling thread, and the repainting must be postponed.
There is a AWT event queue where event are handled in one single thread. When an event is handled, like keyPressed the GUI is frozen; other events are not parallel handled.
So such events should not do something taking a long time, or changing the display.
The solution is to postpone the code one wants to execute.
This can be done with:
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
... the code ...
}
});
In java there is one single thread (process) that handles GUI events in an endless loop, like keyPressed, button click or (indirectly) paintComponent. For that exists a wait queue of events, called java.awt.EventQueue.
So these things are not done in parallel. That limitation makes coding a bit simpler, and mapped better on the operating systems, like the old MacOS.
So when keyPressed is called (on the event thread), a call to repaint will have no effect, as that must be handled on a repaintComponent later, on the same thread.
The solution is to call invokeLater which places an event with a Runnable on the event queue for later.
xPos=0 initially
and
if (xPos <= 3250) { }
else
xPos += 5;
That means xPos is never increased
This seems to work for me (it's an SSCCE created out of your code). Since it works and there's virtually no modifications to your code, the problem probably doesn't lay in the code you posted, but in some other code. Also, generally you'd want to use Key Bindings instead of a KeyListener for these sorts of things.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Temp extends JPanel{
int pX=200, pY=150;
Dimension preferredSize = new Dimension(500,300);
public Temp(){
addKeyListener(new KeyAdapter() {
int xPos = 0, playerFace = 4,xPPos = 200, yPPos = 150;
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
if (keyCode == e.VK_RIGHT) {
if (xPos <= 3250)
if (((xPos + xPPos) >= 825) && ((xPos + xPPos) <= 910)
&& (yPPos >= 170) && (yPPos <= 255)) {
} else if (((xPos + xPPos) >= 1325) && ((xPos + xPPos)<= 1410)
&& (yPPos >= 170) && (yPPos <= 255)) {
} else
xPos += 5;
}
refreshXY(xPPos+xPos,yPPos);
}
});
setFocusable(true);
requestFocus();
}
public Dimension getPreferredSize(){
return preferredSize;
}
public void updateScreen(){
repaint();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawString("x:"+pX, 0, 20);
g.drawString("y:"+pY, 0, 40);
}
public void refreshXY(int x, int y){
pX=x;
pY=y;
System.out.println("Refreshed X&Y");
updateScreen();
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new Temp());
frame.pack();
frame.setVisible(true);
}
}
I have written a small 2d scroller with the assistance of different snippets of code I have found on-line. The original package run as a JFrame application but I am trying to convert it into an applet. When I Run the program in Eclipse I do not receive any debugging errors just a blank applet viewer... I don't think I am missing anything from what I have read from different applet creation sources but maybe it is something very simple.
Frame class
package OurGame;
import java.awt.*;
import javax.swing.*;
public class Frame extends JApplet {
public Frame() {
JPanel frame = new JPanel();
frame.add(new Board());
// frame.setTitle("2D PLATFORMER");
// frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(700,365);
frame.setVisible(true);
//frame.setLocationRelativeTo(null);
//setContentPane(frame);
}
// public static void main(String[] args){
public void init() {
new Frame();
}
}
Ive commented out the containers that were only workable in Jframe.
Dude class
package OurGame;
import java.awt.*;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
public class Dude {
int x, dx, y,nx,nx2,left, dy;
Image still,jump,reverse;
ImageIcon s = new ImageIcon("redirect.png");
ImageIcon j= new ImageIcon("redirect.png");
ImageIcon l = new ImageIcon("redirect.png");
public Dude() {
x = 75;
left = 150;
nx = 0;
nx2= 685;
y = 172;
still = s.getImage();
}
public void move() {
if (dx != -1){
if (left + dx <= 150)
left+=dx;
else{
x = x + dx;
nx2= nx2+dx;
nx = nx + dx;
}}
else
{
if (left+dx >0)
left = left + dx;
}
}
public int getX() {
return x;
}
public int getnX() {
return nx;
}
public int getnX2() {
return nx2;
}
public int getdx() {
return dx;
}
public Image getImage() {
return still;
}
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT)
{ dx = -1;
still = l.getImage(); }
if (key == KeyEvent.VK_RIGHT)
{dx = 1;
still = s.getImage();
}
if (key == KeyEvent.VK_UP)
{dy = 1;
still = j.getImage();
} }
public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT)
dx = 0;
if (key == KeyEvent.VK_RIGHT)
dx = 0;
if (key == KeyEvent.VK_UP)
{dy = 0;
still = s.getImage();}
}
}
Board Class
package OurGame;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Board extends JPanel implements ActionListener, Runnable {
Dude p;
public Image img;
Timer time;
int v = 172;
Thread animator;
boolean a = false;
boolean done2 = false;
public Board() {
p = new Dude();
addKeyListener(new AL());
setFocusable(true);
ImageIcon i = new ImageIcon("redirect.jpg");
img = i.getImage();
time = new Timer(5, this);
time.start();
}
public void actionPerformed(ActionEvent e) {
p.move();
repaint();
}
public void paint(Graphics g) {
if (p.dy == 1 && done2 == false) {
done2 = true;
animator = new Thread(this);
animator.start();
}
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
if ((p.getX() - 590) % 2400 == 0)// p.getX() == 590 || p.getX() == 2990)
p.nx = 0;
if ((p.getX() - 1790) % 2400 == 0)// p.getX() == 1790 || p.getX() == 4190)
p.nx2 = 0;
g2d.drawImage(img, 685 - p.getnX2(), 0, null);
if (p.getX() > 590) {
g2d.drawImage(img, 685 - p.getnX(), 0, null);
}
g2d.drawImage(p.getImage(), p.left, v, null);
if (p.getdx() == -1) {
g2d.drawImage(img, 685 - p.getnX2(), 0, null);
g2d.drawImage(p.getImage(), p.left, v, null);
}
}
private class AL extends KeyAdapter {
public void keyReleased(KeyEvent e) {
p.keyReleased(e);
}
public void keyPressed(KeyEvent e) {
p.keyPressed(e);
}
}
boolean h = false;
boolean done = false;
public void cycle() {
if (h == false)
v--;
if (v == 125)
h = true;
if (h == true && v <= 172) {
v++;
if (v == 172) {
done = true;
}
}
}
public void run() {
long beforeTime, timeDiff, sleep;
beforeTime = System.currentTimeMillis();
while (done == false) {
cycle();
timeDiff = System.currentTimeMillis() - beforeTime;
sleep = 10 - timeDiff;
if (sleep < 0)
sleep = 2;
try {
Thread.sleep(sleep);
} catch (InterruptedException e) {
System.out.println("interrupted");
}
beforeTime = System.currentTimeMillis();
}
done = false;
h = false;
done2 = false;
}
}
I am a little stumbled after doing a fair amount of research. I am thinking Eclipse might not recognise that I have multiple class files but I kind of proved that theory wrong by writing a html page to display my applet that runs fine but is completely empty.
why is the applet viewer even in eclipse blank when running it..
At no point is anything added to the applet container. To add something to the applet would require overriding the applet init() method and calling add(new Board());. (That could also be done in the constructor, but it is more common to build an applet GUI within the init() method.)
Other Notes
paint(Graphics)
Since Board is a Swing class that is not a top-level container, custom painting should be done in the paintComponent(Graphics) method, rather than paint(Graphics).
Nomenclature
JPanel frame = new JPanel();
Wow! Poorly chosen attribute name. What do you call your JFrame instances, panel?
Application resources
ImageIcon s = new ImageIcon("redirect.png");
This will not work for an applet, and would not work for a deployed app. It is necessary to access images by URL. The Applet class has a specific method for loading images.
Remaining lines of constructor
JPanel frame = new JPanel();
frame.add(new Board());
frame.setSize(700,365);
frame.setVisible(true);
The first line of the constructor is not needed, the Board created in the next line can be added directly to the applet. That leaves two more lines not commented out.
frame.setSize(700,365);
The size of an applet should be set by the HTML.
frame.setVisible(true);
Anything added to a component that is visible will itself become visible. As such, this is also redundant.
Swing Timer
Since I pointed out so many faults in the code, just thought I should add that the animation seems to be done correctly - using a Swing Timer. :)