I know there are a lot of answers for this but they don't seem to work. I don't know whether I am somehow using a different version of Java or the code for applet's is different but here's my code:
Snippet from HUD class:
public void paint (Graphics g)
{
if(g instanceof Graphics2D)
{
Graphics2D g2d = (Graphics2D)g;
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g2d.drawString("This is gona be awesome", 200, 200);
}
Main Class:
import java.applet.Applet; (more, they just glitched with the code block)
public class Main extends Applet implements Runnable, KeyListener {
static ArrayList<Block> all_blocks = new ArrayList<Block>();
Player player;
HUD hud;
static int amount = 0;
public void init(){
setSize(700, 500);
addKeyListener(this);
}
public void start(){
player = new Player(100, 100);
HUD hud = new HUD();
for (int i = 0; i < 75; i++){
new Grass(i*30, 400);
}
new Spikes(250, 370);
Thread thread = new Thread(this);
thread.start();
}
@Override
public void run() {
while (true){
player.update();
repaint();
camera_scroll(this.amount);
try {
Thread.sleep(17);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void stop(){
}
public void camera_scroll(int amount){
int new_amount;
if (amount > 30){
new_amount = 30;
this.amount -= 30;
} else {
new_amount = amount;
this.amount = 0;
}
for (Block b: all_blocks){
b.x += new_amount;
b.update();
}
player.x += new_amount;
}
public void paint(Graphics g){
for (Block block: all_blocks){
block.paint(g);
}
hud.paint(g);
player.paint(g);
}
@Override
public void keyPressed(KeyEvent e) {
switch(e.getKeyCode()){
case KeyEvent.VK_LEFT:
case KeyEvent.VK_A:
player.moveLeft();
break;
case KeyEvent.VK_RIGHT:
case KeyEvent.VK_D:
player.moveRight();
break;
case KeyEvent.VK_UP:
case KeyEvent.VK_W:
player.moveUp();
break;
}
}
@Override
public void keyReleased(KeyEvent e) {
switch(e.getKeyCode()){
case KeyEvent.VK_LEFT:
case KeyEvent.VK_A:
player.stopLeft();
break;
case KeyEvent.VK_RIGHT:
case KeyEvent.VK_D:
player.stopRight();
break;
case KeyEvent.VK_UP:
case KeyEvent.VK_W:
player.stopUp();
break;
}
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
}
Drawing of the player and blocks work fine and their paint method is identical to HUD's but they draw images instead. Let me know if you need anything else. Thanks!