0

I have tried everything to get a background image, and it either just is a gray background, or displays the image with no player, or a player with no image. For some reason I can't draw over the background? Any help would be greatly appreciated! Thanks!

package main;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.geom.Ellipse2D;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

public class grow extends JPanel implements ActionListener, KeyListener{
double x = 0, y = 0, velx = 0, vely = 0;
Timer t = new Timer(5, this);

public grow(){
t.start();
addKeyListener(this);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
}

public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
 g2d.setRenderingHint(RenderingHints.KEY_RENDERING,      RenderingHints.VALUE_RENDER_QUALITY);
g2d.setPaint(Color.blue);
g2d.fill(new Ellipse2D.Double(x, y, 70, 70));


}

public void actionPerformed(ActionEvent e) {
repaint();
x += velx;
y += vely;

}

public void up(){
vely = -2;
velx = 0;
}
public void down(){
vely = 2;
velx = 0;
}
public void left(){
vely = 0;
velx = -2;
}

public void right(){
vely = 0;
velx = 2;
} 




 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 stop(){
velx = 0;
vely = 0;
}
public void keyReleased(KeyEvent e) {
int code = e.getKeyCode();
if(code == KeyEvent.VK_UP){
    stop();
}
if(code == KeyEvent.VK_DOWN){
    stop();
}

if(code == KeyEvent.VK_RIGHT){
    stop();
}

if(code == KeyEvent.VK_LEFT){
    stop(); 
    }

}

public void keyTyped(KeyEvent e) {

}

}

and here is my class that displays the jframe:

package main;

import java.awt.Color;
import java.awt.Component;

import javax.swing.JFrame;

public class playerandframe{

static grow g = new grow();
public static void main(String args[]){
    JFrame j = new JFrame("|The Wizards Of Lleon|");
    j.add(g);
    j.setSize(600,600);
    j.setVisible(true);
    j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

}
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • All I see is one `g2d.fill`, which I'm assuming is the background. I don't see an attempt at drawing two item. So where are are the background and the image you are trying to draw? – Paul Samsotha Jan 24 '14 at 05:40

1 Answers1

1
  1. Use key bindings instead of a KeyListener as you may run into focus problems with KeyListener. key bindings are more appropriate for key event with Swing apps. You can see an example here.
  2. Just draw the background and the image in the same paintComponent method. I currently only see one of those things being drawn.
  3. Use proper java naming convention. Class names should being with capital letters.
  4. @Override the getPreferredSize() of the JPanel class (grow) instead of setting the size of the frame, and use frame.pack(), instead of frame.setSize(). packing the frame will respect the preferred size you gave to the panel

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(600, 600);
    }
    
    .... and 
    frame.pack(); // in the main
    
Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720