0

I'm having some issues with my code. I want to fix the background image to stay there fixed, meanwhile other components can move. It's a simple a game where a circle avoid the squares but the istruction about the game will be added further.

Here's my code

import java.applet.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.geom.Line2D;
import java.net.URL;

import javax.swing.*;
public class Main extends Applet implements Runnable,KeyListener{

    Thread r;
    Image bg = null;
    int a,b;
    int x = 600;
    int y = 400;
    public void init(){
        setSize(600,600);
        try 
        { 
            MediaTracker tr = new MediaTracker (this);
            bg = getImage
            (new URL("file:D:/workspace/Game/bin/image.jpg")); //set image
            tr.addImage(bg, 0);
        } catch (Exception e) { System.out.println(e.toString());

        }
    }
    public void start(){
        if(r == null){
            r = new Thread(this);
            r.start();
        }
    }
    public void paint(Graphics g){
        g.drawImage(bg,0,0,this); 
        Graphics2D g2 = (Graphics2D) g;
        g2.setStroke(new BasicStroke(20));
        g2.draw(new Line2D.Float(0, 500, 600, 500));
        g.fillRect(x, y, 20, 20);
    }
    public void pp(Graphics g){

    }
    public void run() {
        Thread Th = Thread.currentThread();
        while(r == Th) {
            if(a < 600) {
                a = a+10;
                x = x-10;
                repaint();
            }else{
                repaint();
                a = 30;
                x = 600;
            }
            try {
                Thread.sleep(100);
                } catch(InterruptedException e) { }
            }
    }
    @Override
    public void keyPressed(KeyEvent arg0) {
        // TODO Auto-generated method stub

    }
    @Override
    public void keyReleased(KeyEvent arg0) {
        // TODO Auto-generated method stub

    }
    @Override
    public void keyTyped(KeyEvent arg0) {
        // TODO Auto-generated method stub

    }

}

if you run the code, you'll see that the line and the background will be loaded a lot of time...

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Karlken
  • 25
  • 3
  • 1) Why code an applet? If it is due to the teacher specifying it, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT components in favor of Swing. 3) *"`new URL("file:D:/workspace/Game/bin/image.jpg")`"* That will look for the image on the local file system of whatever user is running the applet, so .. – Andrew Thompson Apr 12 '17 at 22:05
  • .. it can work only for you. 4) *"`setSize(600,600);`"* That is set by the HTML that loads the applet. The applet code itself should not try to set it. 5) *"`public void paint(Graphics g){ g.drawImage(bg,0,0,this);`"* should be *"`public void paint(Graphics g){ super.paint(g); g.drawImage(bg,0,0,this);`"* – Andrew Thompson Apr 12 '17 at 22:05

0 Answers0