Hi: I'm working on a mini-game ish sort of applet using Eclipse. I am trying to get a picture I have in my folder of a dot to show on my applet. and be able to move it freely. Since I have other parts of the game planned out, parts of my code may seem not to make any sense. But I am fairly new to java, so I really after trying for days still have yet to figure out what to do. I have no more errors showing when I compile, but it's just a plan screen when I run.

I have 2 main parts to the project:

first is "Bullet.java" which contains a class designed to create an Ellipse2D and some things it will need to do in the game.

import java.awt.*;
import java.awt.geom.*;

public class Bullet {
     private Image picture;
     private int xpos, ypos, height, width, speed, xdir, ydir;
     private Ellipse2D.Double c;
     public Bullet(Image p, int x, int y, int w, int h, int s, int xd)
     {
      picture = p;
      xpos = x;
      ypos = y;
      width = w;
      height = h;
      speed = s;
      xdir = xd;
      ydir = 0;
      c = new Ellipse2D.Double (xpos, ypos,  width,  height);
     }
    
     public void move()
     {
      
      xpos = xpos + speed*xdir;
      ypos = ypos + speed*ydir;
      c.setFrame(xpos, ypos,  width,  height);
     } 
     public int getX() { return xpos;}
     public int getY() { return ypos ;}
     public int getHeight() { return height; }
     public int getWidth() { return width ;}
     public int getXDir() { return xdir ;}
     public int getYDir() { return ydir ;}
     public void setPos(int a, int b) {xpos = a; ypos = b;}
     public void setDir(int x, int y)
     {
      xdir = x;
      ydir = y;
     }
     public boolean intersects(int x, int y, int w, int h)
     {
      return c.intersects(x, y, w, h);
     }
     public void displayBullet(Graphics2D g)
     {
      g.drawImage(picture, xpos, ypos, width, height, null);
     }
     public void paint(Graphics2D g)
     {
      displayBullet(g);
     }
}

Next is myApplet.java which contains the actual painting, KeyListener, etc for Bullet.java.

import java.applet.Applet;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.*;
import javax.swing.*;  
import java.awt.image.*;


public class myApplet extends Applet implements KeyListener
 {
    BufferedImage imageBuffer;
     Graphics2D  graphicsBuffer;
     private  Bullet s;
     private Timer t;
     Image p;

 public void init()
 {
  imageBuffer = (BufferedImage)createImage(getWidth(), getHeight());
  graphicsBuffer = (Graphics2D) imageBuffer.getGraphics();
  p= getImage(getCodeBase(), "dot.jpg");
  
  s=new Bullet(p, 20, 250, 50, 200, 20, 0);
  addKeyListener(this);
  setFocusable(true);

  for(int j=0;j<5;j++)
  {
   for(int k=0;k<16;k++)

    {   
        p= getImage(getCodeBase(), "dot.jpg");
    }
  }
  
 ActionListener z = new ActionListener()
 {
  public void actionPerformed(ActionEvent evt)
  {

   s.move();


           }
 };
paint(graphicsBuffer);

 t=new Timer(10, z);
 t.start();

}
 public void keyPressed(KeyEvent e)
{
 if (e.getKeyCode() == KeyEvent.VK_DOWN)
 {
 
  s.setDir(0,1);   //change direction of the paddle to positive
 }
 if (e.getKeyCode() == KeyEvent.VK_UP)
 {
  s.setDir(0,-1);
 }
 if (e.getKeyCode() == KeyEvent.VK_LEFT)
 {
  s.setDir(-1,0);
 }
 if(e.getKeyCode() ==KeyEvent.VK_RIGHT)
 {
  s.setDir(1,0);
 }
}
public void keyReleased(KeyEvent e)
{
 if (e.getKeyCode() == KeyEvent.VK_DOWN)
 {
  s.setDir(0,0);  
 }
 if (e.getKeyCode() == KeyEvent.VK_UP)
 {
  s.setDir(0,0);
 }
 if (e.getKeyCode() == KeyEvent.VK_LEFT)
 {
  s.setDir(0,0);
 }
 if(e.getKeyCode() ==KeyEvent.VK_RIGHT)
 {
  s.setDir(0,0);
 }
}
public void keyTyped(KeyEvent e) {}  

public int width = 3000;
public int height= 300;

public void paint(Graphics g)
{

 Graphics2D g2 = (Graphics2D) g;
 

  graphicsBuffer.setColor(Color.white);
 graphicsBuffer.fillRect(0, 0, getWidth(), getHeight());

 s.paint(graphicsBuffer);

g2.drawImage(imageBuffer, 0,0, getWidth(), getHeight(), this);
}

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
}

My Images are in the right folders for my project yet still, I can't get anything other then a blank screen. Your help is greatly appreciated.

Thanks

I put your two java files into a folder with an image "dot.jpg". I have successfully compiled the MyApplet.java. Only one deformed image is shown in both ways: appletviewer and a browser. Check your image file name, your image file extension and other technical problems. It works in my folder.

here is updated code. the problem still exists though.........

First is Bullet.java code

package put;

import java.awt.*;
import java.awt.geom.*;


public class Bullet {
     private Image picture;
     private int xpos, ypos, height, width, speed, xdir, ydir;
     private Ellipse2D.Double c;
     public Bullet(Image p, int x, int y, int w, int h, int s, int xd)
     {
      picture = p;
      xpos = x;
      ypos = y;
      width = w;
      height = h;
      speed = s;
      xdir = xd;
      ydir = 0;
      c = new Ellipse2D.Double (xpos, ypos,  width,  height);
     }
    
     public void move()
     {
      
      xpos = xpos + speed*xdir;
      ypos = ypos + speed*ydir;
      c.setFrame(xpos, ypos,  width,  height);
     } 
     public int getX() { return xpos;}
     public int getY() { return ypos ;}
     public int getHeight() { return height; }
     public int getWidth() { return width ;}
     public int getXDir() { return xdir ;}
     public int getYDir() { return ydir ;}
     public void setPos(int a, int b) {xpos = a; ypos = b;}
     public void setDir(int x, int y)
     {
      xdir = x;
      ydir = y;
     }
     public boolean intersects(int x, int y, int w, int h)
     {
      return c.intersects(x, y, w, h);
     }
     public void displayBullet(Graphics2D g)
     {
      g.drawImage(picture, xpos, ypos, width, height, null);
     }
     public void paint(Graphics2D g)
     {
      displayBullet(g);
      g.fill(c);
     }
}

Next is myApplet.java code

package put;

import java.applet.Applet;
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 java.awt.*;
import javax.swing.*;  
import java.awt.image.*;


public class myApplet extends Applet implements KeyListener
 {
    BufferedImage imageBuffer;
     Graphics2D  graphicsBuffer;
     private  Bullet s;
     private Timer t;
     Image p;

 public void init()
 {
  imageBuffer = (BufferedImage)createImage(getWidth(), getHeight());
  graphicsBuffer = (Graphics2D) imageBuffer.getGraphics();
  p= getImage(getCodeBase(), "sunset.jpg");
  
  s=new Bullet(p, 20, 250, 50, 200, 20, 0);
  addKeyListener(this);
  setFocusable(true);

  for(int j=0;j<5;j++)
  {
   for(int k=0;k<16;k++)

    {   
        p= getImage(getCodeBase(), "sunset.jpg");
    }
  }
  
 ActionListener z = new ActionListener()
 {
  public void actionPerformed(ActionEvent evt)
  {

   s.move();


           }
 };
paint(graphicsBuffer);

 t=new Timer(10, z);
 t.start();

}
 public void keyPressed(KeyEvent e)
{
 if (e.getKeyCode() == KeyEvent.VK_DOWN)
 {
 
  s.setDir(0,1);   //change direction of the paddle to positive
 }
 if (e.getKeyCode() == KeyEvent.VK_UP)
 {
  s.setDir(0,-1);
 }
 if (e.getKeyCode() == KeyEvent.VK_LEFT)
 {
  s.setDir(-1,0);
 }
 if(e.getKeyCode() ==KeyEvent.VK_RIGHT)
 {
  s.setDir(1,0);
 }
}
public void keyReleased(KeyEvent e)
{
 if (e.getKeyCode() == KeyEvent.VK_DOWN)
 {
  s.setDir(0,0);  
 }
 if (e.getKeyCode() == KeyEvent.VK_UP)
 {
  s.setDir(0,0);
 }
 if (e.getKeyCode() == KeyEvent.VK_LEFT)
 {
  s.setDir(0,0);
 }
 if(e.getKeyCode() ==KeyEvent.VK_RIGHT)
 {
  s.setDir(0,0);
 }
}
public void keyTyped(KeyEvent e) {}  

public int width = 3000;
public int height= 300;

public void paint(Graphics g)
{

 Graphics2D g2 = (Graphics2D) g;
 

 graphicsBuffer.setColor(Color.white);
 graphicsBuffer.fillRect(0, 0, getWidth(), getHeight());
 graphicsBuffer.setColor(Color.blue);
 s.paint(graphicsBuffer);
g2.drawImage(imageBuffer, 0,0, getWidth(), getHeight(), this);
}

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
}

the problem still exists though

Can you describe what the problem is?
When I run the code in AppleViewer, I get an blue oval vertically orientated in the lower left corner over the top of the image.

currently, i have a blank screen in applet viewer..

I have a closer look at your code and tested. Both the image file and the oval may show up although they are overlapped (at the same location). Attached please find the files I have modified. Hope the observation report may help you.
I found:
(1) in class Bullet line 49: g.drawImage(picture, xpos, ypos, width, height, null);
The ImageObserver was null so that the image never shows up
(2) I have changed the access modifiers (private) for the attributes of class Bullet with friendly so that they could be accessed in the class myApplet.
(3) Therefore, the method paint() of the class myApplet (line 101-112) can be enhanced so that the work of display() of the class Bullet can be done without calling s.paint(graphicsBuffer); (line 110)

public void paint(Graphics g)
{
 Graphics2D g2 = (Graphics2D) g;
 graphicsBuffer.setColor(Color.white);
 graphicsBuffer.fillRect(0, 0, getWidth(), getHeight());
 graphicsBuffer.drawImage(s.picture, s.xpos, s.ypos, s.width, s.height, this);
 graphicsBuffer.setColor(Color.red);// I have added this line of code so that the oval shows up 
 graphicsBuffer.fill(s.c);
 g2.drawImage(imageBuffer, 0,0, getWidth(), getHeight(), this);
}

(4) The height of Applet can not be too short. Otherwise the locations of both bullet and the oval beyond the boundary.The size of the applet at least as indicated as follows

<html>
<applet code = "myApplet.class" width = 500 height = 500>
</applet>
</html>

(5) I also found that the Ellipse2D.Bouble c (very slender vertically) and the bullet at the same location so that the oval c overlapps with the bullet image.

Try this:

p = new ImageIcon(getClass().getResource("sunset.jpg")).getImage();
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.