I'm trying to make an image move randomly but it's not working for some reason. The image appears in the panel but it's not moving. I'm using tow classes for this: a main class and a sub class. This is the code for my sub class.
public Image()
{
randomPosition();
try {
image = ImageIO.read(new File("image.png"));
} catch (IOException e) {}
}
public void paint(Graphics g)
{
g.drawImage(image, x, y, this);
Toolkit.getDefaultToolkit().sync();
g.dispose();
}
public void move()
{
x += dx;
y += dy;
if (x >= 550) {
x = 550;
randomDirection();
}
if (x <= 1) {
x = 1;
randomDirection();
}
if (y >= 350) {
y = 350;
randomDirection();
}
if (y <= 1) {
y = 1;
randomDirection();
}
}
public void randomDirection() {
double speed = 2.0;
double direction = Math.random()*2*Math.PI;
dx = (int) (speed * Math.cos(direction));
dy = (int) (speed * Math.sin(direction));
}
public void randomPosition()
{
x = LEFT_WALL + (int) (Math.random() * (RIGHT_WALL - LEFT_WALL));
y = UP_WALL + (int) (Math.random() * (DOWN_WALL - UP_WALL));
}
public void run()
{
long beforeTime, timeDiff, sleep;
beforeTime = System.currentTimeMillis();
while (true)
{
move();
repaint();
timeDiff = System.currentTimeMillis() - beforeTime;
sleep = DELAY - timeDiff;
if (sleep > 2)
{
sleep = 1;
}
try
{
Thread.sleep(sleep);
}
catch (InterruptedException e)
{
System.out.println("interrupted");
}
beforeTime = System.currentTimeMillis();
}
}
and this is my main class where I start the thread:
public void addImage(){
Image I = new Image();
x = panel.getGraphics();
I.paint(x);
Thread thr=new Thread(I);
thr.start();
}
paint() should be
Like this:
public void paintComponent(Graphics g) {
super.paintComponent(g);
...
}
Never call paint()/paintComponent() directly. Instead call repaint(), this adds it to the event queue. The graphics object is passed by swing.
I'm not sure g.dispose() is necessary, and might cause problems.
Check the panel your painting into is sized correctly - a quick way to debug this is panel.setBorder(BorderFactory.createLineBorder(Color.RED));
Consider using a javax.swing.Timer instead of Thread. This isn't the cause of problem, but would be cleaner.
Related
I am rendering a model on canvas with opengl, therefore using a while loop and I run it under a thread not to disturb the main thread. I also have a text Field where I want to write some text. The text field focuses to my action and takes input but when hovering through the canvas, the text field focuses but I am not able to write something on it. This may be because of the the while loop but I do not know how to overcome this. I want the ui to remain responsive.
Here is my code:
import java.awt.Canvas;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JTextField;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.PixelFormat;
public final class Loader extends JFrame {
public final int FPS = 120; // better cause of animations
public static Loader loader;
public Canvas canvas;
private Viewport viewport;
private JTextField textField;
private Loader() {
setTitle("Test");
setResizable(true);
setSize(320, 285);
setLocationRelativeTo(null);
getContentPane().setLayout(null);
setVisible(true);
canvas = new Canvas();
getContentPane().add(canvas);
canvas.setBounds(10, 24, 280, 163);
textField = new JTextField();
textField.setColumns(10);
textField.setBounds(91, 193, 116, 22);
getContentPane().add(textField);
}
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
// Initialize the frame
loader = new Loader();
new Thread(new Runnable() {
#Override
public void run() {
try {
// render the model
loader.render();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
private static void init(Canvas canvas) throws Exception {
Display.setParent(canvas);
try {
Display.create(new PixelFormat(8, 24, 8, 8));
} catch (Exception ex) {
Display.create(new PixelFormat(8, 24, 8, 0));
}
Display.makeCurrent();
Display.setVSyncEnabled(false);
Display.setSwapInterval(0);
}
private void render() throws Exception {
init(canvas);
loader.viewport = new Viewport(canvas);
long[] timerCache = new long[10];
long timerCur = 0L;
long timerDst = 0L;
long timerLast = 0L;
long timerRate = 1000000L * 1000L / FPS;
int timerCacheIndex = 0;
int timerCacheSize = 0;
boolean minSleep = Runtime.getRuntime().availableProcessors() <= 1;
long[] clockCache = new long[32];
int clockIndex = 0;
int fps = 0;
while (isVisible()) {
long clock = System.nanoTime();
long lastClock = clockCache[clockIndex];
clockCache[clockIndex] = clock;
if (++clockIndex == 32)
clockIndex = 0;
if (lastClock != 0L && clock > lastClock) {
fps = (int) (1000000L * 1000L * 32L / (clock - lastClock));
}
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
GL11.glClearColor(.5f, .6f, .9f, 1f);
loader.viewport.render();
if (minSleep)
try {
Thread.sleep(1L);
} catch (Exception ex) {
}
timerCache[timerCacheIndex++] = System.nanoTime();
if (timerCacheSize < timerCacheIndex)
timerCacheSize = timerCacheIndex;
if (timerCacheIndex == 10)
timerCacheIndex = 0;
long time = 0L;
for (int i = 0; i != timerCacheSize; time += timerCache[i++])
;
time /= (long) timerCacheSize;
if (timerCacheSize == 1)
timerLast = time;
timerCur += time - timerLast;
timerLast = time;
timerDst += timerRate;
if (timerDst > timerCur) {
long sleep = timerDst - timerCur;
try {
Thread.sleep(sleep / 1000000L, (int) (sleep % 1000000L));
} catch (Exception ex) {
}
timerCur = timerDst;
for (int i = 0; i != timerCacheSize; ++i)
timerCache[i] += sleep;
timerLast += sleep;
}
}
}
}
This gif may help know how it currently looks.
https://gyazo.com/e6950fd8dd01306c704857e94f214f93.gif
I don't see where is the mistake in here, I already use a thread for the render method.
Edit:
Viewport.java
public Viewport(Canvas canvas) throws LWJGLException {
this.canvas = canvas;
this.scale = 1.0F;
this.pitch = 180.0F;
this.yaw = 0.0F;
this.roll = 0.0F;
}
public void render() {
try {
Dimension size = canvas.getSize();
if (models != null && size.width > 0 && size.height > 0) {
if (width != size.width || height != size.height) {
width = size.width;
height = size.height;
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
float c = (float) Math.sqrt((double) (width * width) + (double) (height * height));
glOrtho(0.0F, (float) width, 0.0F, (float) height, -c, c);
glMatrixMode(GL_MODELVIEW);
}
if (Mouse.isButtonDown(0) && !Mouse.isButtonDown(1)) {
yaw -= (float) Mouse.getDX() / 2;
pitch -= (float) Mouse.getDY() / 2;
}
if (Mouse.isButtonDown(0) && Mouse.isButtonDown(1)) {
offset_z += (float) Mouse.getDY();
}
float wheel = (float) Mouse.getDWheel() / 1800.0F;
if (wheel > 1.0F)
wheel = 1.0F;
else if (wheel < -1.0F)
wheel = -1.0F;
scale += scale * wheel;
if (scale < 0.01F)
scale = 0.01F;
for (Model3D model : models) {
float x = (float) width / 2.0F;
float y = ((float) -((100.0F * (scale))) + offset_z) + (float) (height - model.height()) / 2.0F;
float z = 0.0F;
model.render(model, x, y, z, pitch, yaw, roll, scale, scale, scale);
}
Display.update();
}
} catch (Throwable t) {
}
}
Instead of:
new Thread(new Runnable() {
#Override
public void run() {
try {
// render the model
loader.render();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
maybe:
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
try {
// render the model
loader.render();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
It will run at AWT dispatching thread, maybe the issue will fade. Swing not being thread safe sometimes makes life hard.
Can you try adding requestFocus() to the end of your render() method of your Loader class and report back what happens?
I have an application where you place tiles. You can place tiles over tiles and I don't want that. I know that I need something like if the tile rectangle contains the mouse then don't place a tile over it. But this doesn't work. Look at this code:
for (int i = 0; i < b.toArray().length; i++) {
b.get(i).tick();
if (b.get(i).r.contains(Comp.mx, Comp.my)) {
canPlaceATile = false;
// System.out.println("yes");
}
else {
canPlaceATile = true;
//System.out.println("no");
}
if (b.get(i).remove) {
b.remove(i);
i--;
}
}
This is how I check if the mouse is inside the area of one of the tiles.
Block class:
public abstract class block {
public int x,id;
public int y;
protected Image img;
public boolean remove;
public int rotate;
public Rectangle r;
protected int bx, by;
public block() {
}
public abstract void tick();
public abstract void render(Graphics g);
public void createCollisionRect() {
r.setBounds(x - (int) play.camx, y - (int) play.camy, 20, 20);
}
}
an example of a tile:
public class wall extends block {
public wall(int x, int y, int rot) {
this.x = x;
this.y = y;
this.rotate = rot;
r = new Rectangle(x - (int) play.camx, y - (int) play.camy, 20, 20);
id = 0;
}
public void tick() {
createCollisionRect();
if (Comp.mr && r.contains(new Point((Comp.mx), (Comp.my)))) {
remove = true;
}
}
public void render(Graphics g) {
ImageIcon i62 = new ImageIcon("res/tiles/wall.png");
img = i62.getImage();
g.drawImage(img, x - (int) play.camx, y - (int) play.camy, null);
g.setColor(Color.red);
// g.drawRect(x -(int)play.camx, y - play.camy, 20,20);
}
}
I want to check if any of the rectangles contains the mouse and set canPlaceATile to false if the mouse is inside one of the rectangles. The code above doesn't work because when I print the console and I have my mouse over one of the tiles it says:
yes
no
yes
no
yes
no
yes
no
yes
no
yes
no
yes
no
yes
no
yes
no
yes
no
yes
no
yes
and this is when my mouse is on the rectangle without moving it at all. How can I fix this so that I cannot place tiles on top of tiles. Her is where I place tiles:
public void mousePressed(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON1) {
Comp.ml = true;
if (manager.isplay && play.dragging == false) {
if (play.canPlaceATile) {
if (play.selectedID == 0) {
play.b.add(new wall((Comp.mx / 20) * 20, (Comp.my / 20) * 20, play.selectedRot));
}
}
}
}
}
I am trying to get a ball to bounce up and down when you press the up arrow key. I can get the ball to move up but it will not stop moving up. I have written the code for the ball movement in my update method. I am trying to get the ball to stop at y coordinate 400, but it just passes it up.
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
public class Game extends Canvas implements Runnable {
int x,y;
public static int width = 300;
public static int height = width / 16 * 9;
public static int scale = 3;
private boolean running = false;
public Thread thread;
public JFrame frame;
public Keyboard key;
private BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
private int[] pixels = ((DataBufferInt)image.getRaster().getDataBuffer()).getData();
public Game() {
Dimension size = new Dimension(width * scale, height * scale);
setPreferredSize(size);
frame = new JFrame();
key = new Keyboard();
addKeyListener(key);
}
public synchronized void start() {
running = true;
thread = new Thread(this, "Display");
thread.start();
}
public synchronized void stop(){
running = false;
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
#Override
public void run() {
long lastTime = System.nanoTime();
final double nanoSeconds = 1000000000.0 / 60.0;
double delta = 0;
int frames = 0;
int gameUpdates = 0;
long timer = System.currentTimeMillis();
requestFocus();
while(running){
long now = System.nanoTime();
delta += (now - lastTime) / nanoSeconds;
lastTime = now;
//this ensures that delta only updates the screen 60 times each second
while (delta >= 1){
update();
gameUpdates++;
delta--;
}
render();
frames++;
//this if statement happens once a second
if (System.currentTimeMillis() - timer > 1000){
timer += 1000;
frame.setTitle("Bouncy Ball! ~ ~ ~ Updates per second: " + gameUpdates + ". Frames per second: " + frames + ".");
gameUpdates = 0;
frames = 0;
}
}
stop();
}
int yy;
public void update() {
key.update();
y = y + yy;
if(key.up){
yy = -5;
if(y == 400){
yy = 0;}
}
}
public void render() {
BufferStrategy bs = getBufferStrategy();
if (bs == null){
createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
g.drawImage(image,0,0,getWidth(),getHeight(),null);
g.setColor(Color.DARK_GRAY);
g.fillRect(0,0,getWidth(),getHeight());
g.setColor(Color.MAGENTA);
g.fillOval(300,y+435,50,50);
g.dispose();
bs.show();
}
public static void main(String[] args) {
Game game = new Game();
game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
game.frame.setResizable(false);
game.frame.setVisible(true);
game.frame.add(game);
game.frame.pack();
game.frame.setTitle("Bouncy Ball");
game.start();
}
}
This is my keylisener class
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class Keyboard implements KeyListener{
private boolean[] keys = new boolean[120];
public boolean up, down, left, right;
public void update(){
up = keys[KeyEvent.VK_UP];
down = keys[KeyEvent.VK_DOWN];
left = keys[KeyEvent.VK_LEFT];
right = keys[KeyEvent.VK_RIGHT];
}
#Override
public void keyPressed(KeyEvent e) {
keys[e.getKeyCode()] = true;
}
#Override
public void keyReleased(KeyEvent e) {
keys[e.getKeyCode()] = false;
}
#Override
public void keyTyped(KeyEvent e) {
}
}
Your issue is in the key.update() method. key.up will only be true while you're holding the up key, not just if you press the up key once. If, for now, you only care about a single button press, change it to:
public void update() {
if (keys[KeyEvent.VK_UP]) {
up = true;
}
}
Which will only update once, once you press up, and keys.up will stay true.
Also, y is decreasing, not increasing (you're subtracting 5 from it), so you want to change
== 400 to == 400, or even better, <= 400, in case you don't increment with whole numbers (as per the other answer).
As a general debugging idea: At that if statement, I printed out the value of y, and noticed it only printed out 3 or 4 values (corresponding to how long I was holding it down), which meant that the if statement wasn't being checked, or wasn't returning true (which was the case here).
Instead of:
if(key.up){
yy = -5;
if(y == 400){
yy = 0;}
}
try:
if(key.up){
yy = -5;
if(y >= 400){
yy = 0;}
}
If your ball is moving at 5px per update then it may pass 400px without actually
equaling 400px, this is why you check if it is greater than 400px.
I'm trying to write a code a bout a bouncing ball, but i'm stuck in how to make the ball bounced. The code seems correct, no wrong messages from eclipse and yet the ball doesn't move. Any help/hint is appreciate.
Here's my code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class BouncingBallTest extends JFrame {
private JButton jbtStart = new JButton("Start");
private JButton jbtStop = new JButton("Stop");
private Ball canvas = new Ball();
public BouncingBallTest() {
JPanel panel = new JPanel(); // Use the panel to group buttons
panel.add(jbtStart);
panel.add(jbtStop);
add(canvas, BorderLayout.CENTER); // Add canvas to centre
add(panel, BorderLayout.SOUTH); // Add panel to south
// register listener
jbtStart.addActionListener(new StartBouncingBallTest());
jbtStop.addActionListener(new StopBouncingBallTest());
}
// the main method
public static void main(String[] args) {
JFrame frame = new BouncingBallTest();
frame.setTitle("Bouncing Ball Test");
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(470, 300);
frame.setVisible(true);
}
class StartBouncingBallTest implements ActionListener { // inner class
#Override
public void actionPerformed(ActionEvent e) {
canvas.StartBouncingBallTest();
}
}
class StopBouncingBallTest implements ActionListener { // inner class
#Override
public void actionPerformed(ActionEvent e) {
canvas.StopBouncingBallTest();
}
}
class Ball extends JPanel {
private int radius = 10;
private int x;
private int y;
private int dx = 3;
private int dy = 3;
private Timer timer = new Timer(20, new TimerListener());
public void StartBouncingBallTest() {
if (x > 0 && y > 0) {
x += dx;
y += dy;
}
else if (x == 0) {
x -= dx;
y += dy;
}
else if (y + (2 * radius) > getHeight()) {
x += dx;
y -= dy;
}
else if (y == 0) {
x += dx;
y -= dy;
}
else if (x + (2 * radius) > getWidth()) {
x -= dx;
y += dy;
}
repaint();
timer.start();
}
public void StopBouncingBallTest() {
timer.stop();
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.GREEN);
g.fillOval(x, y, 2 * radius, 2 * radius);
}
}
class TimerListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
repaint();
}
}
}
Basically, nothing is moving the ball.
Each time the Swing Timer ticks, all you do is repaint.
You need to move the movement logic to the actionPerformed method of the ActionListener registered to the Timer
More like...
public void StartBouncingBallTest() {
timer.start();
}
//...
class TimerListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
if (x > 0 && y > 0) {
x += dx;
y += dy;
}
else if (x == 0) {
x -= dx;
y += dy;
}
else if (y + (2 * radius) > getHeight()) {
x += dx;
y -= dy;
}
else if (y == 0) {
x += dx;
y -= dy;
}
else if (x + (2 * radius) > getWidth()) {
x -= dx;
y += dy;
}
repaint();
}
}
This way, each time the Timer ticks, you are making update the position of the ball accordingly...
Updated with working example
I made two changes. I made your TimerListener an inner class of Ball, which allows it to access the variable and methods of Ball and modified your movement logic so it works
class Ball extends JPanel {
private int radius = 10;
private int x;
private int y;
private int dx = 3;
private int dy = 3;
private Timer timer = new Timer(20, new TimerListener());
public void StartBouncingBallTest() {
timer.start();
}
public void StopBouncingBallTest() {
timer.stop();
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.GREEN);
g.fillOval(x, y, 2 * radius, 2 * radius);
}
class TimerListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
if (x < 0 || x + (2 * radius) > getWidth()) {
dx *= -1;
}
if (y < 0 || y + (2 * radius) > getHeight()) {
dy *= -1;
}
x += dx;
y += dy;
repaint();
}
}
}
private Timer timer = new Timer(20, new TimerListener());
doesn't that new TimerListener() need to do something useful? shouldn't most of the code inside StartBouncingBallTest be happening every time the timer ticks, intsead of just once when the timer starts?
you never declared that the g.fillOval was the ball here do this...
g.drawOval((int)applex, (int)appley, (int)appleradius, appleradius);
public double applex = ArandomX; //replace the random with values..
public double appley = ArandomY; //same here
public int appleradius = 10;
this makes it move...
a.px += a.vx;
// check for boundaries
if (a.px >= this.getWidth() || a.px <= 0) {
// reverse vel and apply
a.vx = -a.vx;
a.px += -a.vx; // to prevent sticking
}
//a.vx += -gravity; // add this constant to accelerate things down
a.py += a.vy;
// check for boundaries
if (a.py >= this.getHeight() || a.py <= 0) {
// reverse vel and apply
a.vy = -a.vy;
a.py += a.vy; // to prevent sticking
}`
replace a.vx with your x or dx idk because your variables are unclear and yeah just replace a.vy with either y or dy and do the same for my px... it will work
I've been told by my friend that my code isn't very OO. How would I make this more OO and reusable?
I tried making a class called level, that printed the images in level, but noting printed, so that didn't work.
(code dump coming, I'm sorry)
public class Main extends Applet implements Runnable, KeyListener,
java.awt.event.MouseListener {
double x_pos = 300;
double y_pos = 200;
int radius = 20;
int appletsize_x = 640;
int appletsize_y = 440;
double speed = 3;
float speedModifier = 1f;
Image grass;
Image hero;
Image hero45;
Image hero90;
Image hero135;
Image hero180;
Image hero225;
Image hero270;
Image hero315;
Image sprite;
Image tree;
Image path;
private boolean leftPressed;
private boolean rightPressed;
private boolean downPressed;
private boolean upPressed;
private Image dbImage;
private Graphics dbg;
public void init() {
Dimension dim = getMaximumSize();
this.setSize(appletsize_x, appletsize_y);
MediaTracker mt = new MediaTracker(this);
tree = getImage(getCodeBase(), "tree_64.png");
grass = getImage(getCodeBase(), "grassTile.png");
path = getImage(getCodeBase(), "path.png");
hero = getImage(getCodeBase(), "hero.png");
hero45 = getImage(getCodeBase(), "hero45.png");
hero90 = getImage(getCodeBase(), "hero90.png");
hero135 = getImage(getCodeBase(), "hero135.png");
hero180 = getImage(getCodeBase(), "hero180.png");
hero225 = getImage(getCodeBase(), "hero225.png");
hero270 = getImage(getCodeBase(), "hero270.png");
hero315 = getImage(getCodeBase(), "hero315.png");
sprite = getImage(getCodeBase(), "hero.png");
mt.addImage(hero, 0);
mt.addImage(path, 0);
mt.addImage(tree, 0);
mt.addImage(grass, 0);
mt.addImage(hero45, 0);
mt.addImage(hero90, 0);
mt.addImage(hero135, 0);
mt.addImage(hero180, 0);
mt.addImage(hero225, 0);
mt.addImage(hero270, 0);
try {
mt.waitForID(0);
} catch (InterruptedException ie) {
}
}
public void start() {
this.addKeyListener(this);
this.addMouseListener(this);
Thread th = new Thread(this);
th.start();
}
public void stop() {
}
public void destroy() {
}
public void run() {
// lower ThreadPriority
this.requestFocusInWindow();
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
while (true) {
if (downPressed && leftPressed) {
double y_decr, x_incr;
y_decr = Math.sqrt(Math.pow(speed, 2)) / 1.5;
x_incr = Math.sqrt(Math.pow(speed, 2)) / 1.5;
y_pos += y_decr;
x_pos -= x_incr;
} else if (downPressed && rightPressed) {
y_pos += Math.sqrt(Math.pow(speed, 2)) / 1.5;
x_pos += Math.sqrt(Math.pow(speed, 2)) / 1.5;
} else if (upPressed && rightPressed) {
y_pos -= Math.sqrt(Math.pow(speed, 2)) / 1.5;
x_pos += Math.sqrt(Math.pow(speed, 2)) / 1.5;
} else if (upPressed && leftPressed) {
y_pos -= Math.sqrt(Math.pow(speed, 2)) / 1.5;
x_pos -= Math.sqrt(Math.pow(speed, 2)) / 1.5;
} else {
// Hitting (right)
if (x_pos > this.getSize().width - radius) {
// x_speed = -x_speed;
}
if (leftPressed == true) {
x_pos -= speed * speedModifier;
}
if (rightPressed == true) {
x_pos += speed * speedModifier;
}
if (upPressed == true) {
y_pos -= speed * speedModifier;
}
if (downPressed == true) {
y_pos += speed * speedModifier;
}
}
// Hitting right
if (x_pos > this.getSize().width - 32) {
x_pos = this.getSize().width - 32;
} // Hitting left
if (x_pos < 0) {
x_pos = 0;
}
// } // Hitting top
if (y_pos < 0) {
y_pos = 0;
}
// Hitting bottom
if (y_pos > this.getSize().height - 32) {
y_pos = this.getSize().height - 32;
}
repaint();
try {
// Stop thread for 1 milliseconds
Thread.sleep(20);
} catch (InterruptedException ex) {
// do nothing
}
// set ThreadPriority to maximum value
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
}
}
public void paint(Graphics g) {
int layout = 0;
int coll = 0;
while (coll <= 440) {
drawRows(layout, grass, g, coll);
coll = coll + 32;
}
drawCollums(0, path, g, 180);
g.drawImage(tree, 50, 40, this);
g.drawImage(tree, 300, 20, this);
g.drawImage(tree, 500, 300, this);
int x_posI = (int) x_pos;
int y_posI = (int) y_pos;
if (downPressed && leftPressed) {
this.sprite = hero225;
} else if (downPressed && rightPressed) {
this.sprite = hero135;
} else if (upPressed && rightPressed) {
this.sprite = hero45;
} else if (upPressed && leftPressed) {
this.sprite = hero315;
} else if (leftPressed == true) {
this.sprite = hero270;
} else if (rightPressed == true) {
this.sprite = hero90;
} else if (upPressed == true) {
this.sprite = hero;
} else if (downPressed == true) {
this.sprite = hero180;
}
// this.sprite will contain value set on last "movement"
g.drawImage(this.sprite, x_posI, y_posI, this);
}
public void update(Graphics g) {
if (dbImage == null) {
dbImage = createImage(this.getSize().width, this.getSize().height);
dbg = dbImage.getGraphics();
}
dbg.setColor(getBackground());
dbg.fillRect(0, 0, this.getSize().width, this.getSize().height);
dbg.setColor(getForeground());
paint(dbg);
g.drawImage(dbImage, 0, 0, this);
}
#Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
leftPressed = true;
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
rightPressed = true;
}
if (e.getKeyCode() == KeyEvent.VK_UP) {
upPressed = true;
}
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
downPressed = true;
}
}
#Override
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
leftPressed = false;
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
rightPressed = false;
}
if (e.getKeyCode() == KeyEvent.VK_UP) {
upPressed = false;
}
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
downPressed = false;
}
}
#Override
public void keyTyped(KeyEvent e) {
}
#Override
public void mouseClicked(MouseEvent e) {
System.out.println("HIT!");
// TODO Auto-generated method stub
}
#Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
public void drawRows(int x, Image image, Graphics g, int coll) {
while (x <= appletsize_x) {
g.drawImage(image, x, coll, this);
x += 32;
}
}
public void drawCollums(int y, Image image, Graphics g, int coll) {
while (y <= appletsize_x) {
g.drawImage(image, coll, y, this);
y += 32;
}
}
}
You should ask your friend what he or she meant. If I were to guess, I would say that your Applet class is doing a lot of things: it's the animation loop; it's the key handler; it's the mouse handler; it's the UI. Those could be broken out into separate objects that were designed to work together.
Another improvement might be to translate your four ...Pressed flags into an integer from 0 through 7, representing the eight possible directions. For instance:
0 1 2
7 3
6 5 4
(You can assign the direction numbers any way you like.) Then you could use the direction number to index into an array of images. (This has nothing to do with being object-oriented, but it would simplify your code.)
Well for one thing, all those images should be in an array. And they should contain a filename so you can load them in a nice loop. You have over 3 pages of copy&paste code, that's absolutely horrible... What if you want to change the way they get loaded slightly?
Then, Math.sqrt(Math.pow(speed, 2)) jumps out at me. That's the equivalent of Math.abs(speed), and about 1/200x as fast.
This next part is particularly funny too. I won't comment on whether or not it's needed (and it most likely isn't, it's a design flaw on your part), but.. ya..
// Stop thread for 1 milliseconds
Thread.sleep(20);
I'd suggest you read books and article about OOP and in this particular case you should take a look at the MVC pattern (MVC article in wikipedia). For a good OO conception the events (keyPressed(KeyEvent e) for example) shouldn't be treated in the same class as the actual "graphics building" class.
Build a new package for your application and group all your data structures inside. Create a class for Your Hero, create classes for trees and grass. See how they can share attributes and create a class hierarchy, maybe with an abstract class.
These are just a few cues, it could be long to give you an Object Oriented course here ;)
But follows this tracks and read.
Also, you should learn how to stop threads elegantly.
And you could use inner classes for your listeners, even use a MouseAdater.
Regards,
Stéphane
You might like to study this game that also moves a player around a grid using the keyboard.