Not sure why this graphics screen is not updating - java

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);
}
}

Related

change the direction of the ball as the key is pressed

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...

Java Multiple Objects JFrame

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.

Grey screen in Netbeans

so i using netbeans, and i'm starting to get into coding games... and i've done this so far with no errors, however when i run it just a grey box with my title "zachs game appears and thats it.... please help if you know the problem 1 -thank you
package swing9;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
public class JavaApplication2 extends JFrame implements Runnable {
int x, y, xDirection, yDirection;
Font font = new Font("Arial", Font.BOLD | Font.ITALIC, 30);
public void run() {
try {
while (true) {
move();
Thread.sleep(5);
}
} catch (Exception e) {
System.out.println("Error");
}
}
public void move() {
x += xDirection;
y += yDirection;
if (x <= 0)
x = 0;
if (x >= 300)
x = 300;
if (y <= 50)
y = 50;
if (y <= 300)
y = 300;
}
public void seyXDir(int xdir) {
xDirection = xdir;
}
public void setYDirection(int ydir) {
yDirection = ydir;
}
public class AL extends KeyAdapter {
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
if (keyCode == e.VK_LEFT) {
int setXDirection = -1;
}
if (keyCode == e.VK_RIGHT) {
int setXDirection = +1;
}
if (keyCode == e.VK_UP) {
int setYDirection = -1;
}
if (keyCode == e.VK_DOWN) {
int setYDirection = +1;
}
}
}
public void keyReleased(KeyEvent e) {
int keyCode = e.getKeyCode();
if (keyCode == e.VK_LEFT) {
int setXDirection = 0;
}
if (keyCode == e.VK_RIGHT) {
int setXDirecetion = 0;
}
if (keyCode == e.VK_UP) {
int setYDirectiom = 0;
}
if (keyCode == e.VK_DOWN) {
int setYDirecction = 0;
}
}
public JavaApplication2() {
addKeyListener((KeyListener) new JavaApplication2.AL());
setTitle("Zachs Game");
setSize(300, 300);
setResizable(false);
setVisible(true);
setBackground(Color.blue);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
x = 150;
y = 150;
}
public void paintComponent(Graphics g) {
g.setColor(Color.red);
g.drawString("Play", 40, 40);
g.setFont(font);
g.setColor(Color.red);
g.fillOval(x, y, 15, 15);
repaint();
}
public static void main(String[] args) {
new JavaApplication2();
// threads
Thread t1 = new Thread();
t1.start();
}
}
JFrame or any of its super classes do not implement the paintComponent method so is never invoked. Check this yourself by adding the #Override annotation.
Move this method to a new class that extends JComponent and invoke super.paintComponent(g) as the first statement.
Don't call repaint from within paintComponent, this create an infinite loop and degrades performance. Swing Timers were designed to interact more easily with swing components. Use these over than raw Threads for periodic updates.
Aside: JFrame is not focusable by default so KeyEvents which require focus will not be triggered without making the window focusable. Use Key Bindings instead.

How to create random monsters

I am making a game like Space Impact. In my game the rockets are working correctly, but I can't create monsters randomly. Monsters are on the screen but they aren't moving so they aren't active
How can I do this?
public class Dnm extends JFrame implements Runnable {
Graphics dbg;
Image dbImage;
Image Pik1;
Image Boss;
static ImageIcon active;
int x, y, xDirection, yDirection, BossX, BossY;
public void run() {
try {
while (true) {
move();
boss();
Thread.sleep(10);
}
} catch (Exception e) {
System.out.println("Uh-oh, something went wrong!.");
}
}
private void move() {
x += xDirection;
y += yDirection;
}
private void boss() {
while (BossX == 0) {
BossX = getX() - 1;
}
Random rastgele = new Random(getY());
BossY = rastgele.nextInt();
}
public void setXDirection(int xdir) {
xDirection = xdir;
}
public void setYDirection(int ydir) {
yDirection = ydir;
}
// KEY COMMANDS //
public class AL extends KeyAdapter {
#Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
if (keyCode == e.VK_LEFT) {
setXDirection(-1);
}
if (keyCode == e.VK_RIGHT) {
setXDirection(+1);
}
if (keyCode == e.VK_UP) {
setYDirection(-1);
}
if (keyCode == e.VK_DOWN) {
setYDirection(+1);
}
}
#Override
public void keyReleased(KeyEvent e) {
int keyCode = e.getKeyCode();
if (keyCode == e.VK_LEFT) {
setXDirection(0);
}
if (keyCode == e.VK_RIGHT) {
setXDirection(0);
}
if (keyCode == e.VK_UP) {
setYDirection(0);
}
if (keyCode == e.VK_DOWN) {
setYDirection(0);
}
}
#Override
public void keyTyped(KeyEvent e) {
}
}
// CONSTRUCTOR //
public Dnm() {
//Image Import
ImageIcon still = new ImageIcon("img/rocket.png");
Pik1 = still.getImage();
addKeyListener(new AL());
setTitle("Dnm");
setSize(500, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(true);
setVisible(true);
x = 15;
y = 15;
}
public void paint(Graphics g) {
dbImage = createImage(getWidth(), getHeight());
dbg = dbImage.getGraphics();
paintComponent(dbg);
g.drawImage(dbImage, 0, 0, this);
}
public void paintComponent(Graphics g) {
ImageIcon background = new ImageIcon("img/space.jpeg");
Image background1 = background.getImage();
ImageIcon still1 = new ImageIcon("img/Alien.png");
Boss = still1.getImage();
g.setColor(Color.red);
g.drawImage(background1, 0, 0, this);
g.drawImage(Boss, BossX, BossY, this);
g.drawImage(Pik1, x, y, this);
g.setColor(Color.blue);
repaint();
}
public static void main(String[] args) {
Dnm game = new Dnm();
Thread t1 = new Thread(game);
t1.start();
}
}
Thank You.
With your code being incomplete, it is hard to say for sure, but this line looks suspicious:
while (BossX == 0) {
BossX = getX() - 1;
}
This code is mostly likely going to be executed once, right? If BossX is initially zero, the code executes where you change its value. On the next update, BossX is not zero (presumably), so the code doesn't execute. Since BossX is the X coordinate where you are drawing the boss, then it will not move. Instead it just gets redrawn over and over in the same spot.
To get the monster to move randomly, increment or decrement both the X and Y coordinate values randomly during each game loop. Add a second random variable to cause them to move at random speeds in the random direction (by adjusting the coordinate value by a value greater than one).
As NPE commented, you should step through the code in a debugger and find out precisely what is going on.
I am fairly a beginner at programming, but here is my best attempt:
I would create a method that is called every x seconds that generates a random number using Math.Random(). I would say
int number = .5;
if(number > Math.Random())
GenerateMonster();
Your GenerateMonster method would then create a new object with the default values for monster. In order for the monster's created to act a certain way, you need to inherit those functions wherever you defined how you want the monster to act. I don't see the code for these functions so I am assuming you just didn't list them. Hope this at least helps. Good Luck!

Running Game in a Java Applet

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. :)

Categories

Resources