1 Replies - 216 Views - Last Post: 01 August 2013 - 02:46 PM Rate Topic: -----

#1 zerimar97  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 24-April 13

Side scroller image problem

Posted 01 August 2013 - 02:29 PM

I'm making a side scroller, and I have an issue about one of the images i'm using. Here's my code:
Frame Classe:
package Game;

import javax.swing.*;

public class Frame {
	public static void main(String[] args) {
		JFrame frame = new JFrame("swag");
		
		frame.add(new Board());
		
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		//frame.setSize(780, 285);
		frame.setSize(1200, 365);
		frame.setVisible(true);
		frame.setLocationRelativeTo(null);
		}
}



character class:
package Game;

import java.awt.*;
import java.awt.event.KeyEvent;

import javax.swing.ImageIcon;

public class Character {
int x, dx, y , dy;

Image still;

public Character(){
	ImageIcon i = new ImageIcon("/Users/crprincipal2/Desktop/Jared/Rayright.jpg");
	still = i.getImage();
	x = 100;
	y = 10;
			
	}

	public void move() {
		x = x +dx;
	}
	public void jump() {
		y = y +dy;
	}
	public int getX() {
		return x;
	}
	public int getY() {
		return y;
	}
	public Image getImage() {
		return still;
	}
	
		public void keyPressed(KeyEvent e) {
			int key = e.getKeyCode();
			
			if (key == KeyEvent.VK_LEFT)
			dx = -1;
			
			if(key == KeyEvent.VK_RIGHT)
			dx = 1;
			
			if (key == KeyEvent.VK_UP)
			dy = 1;
			
			//if(key == KeyEvent.VK_RIGHT && VK_Shift)
			//dx = 10;
				
		}
		
		public void keyRelesed(KeyEvent e) {
			int key = e.getKeyCode();
			
			if (key == KeyEvent.VK_LEFT)
			dx = 0;
			
			if(key == KeyEvent.VK_RIGHT)
			dx = 0;
			
			if (key == KeyEvent.VK_UP)
			dy = 0;
		}
}



and my board class:
package Game;

import java.awt.*;

import javax.swing.*;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class Board extends JPanel implements ActionListener {

	Character p;
	Image img;
	Timer time;
	
	public Board() {
		p = new Character();
		addKeyListener(new AL());
		setFocusable(true);
		ImageIcon i = new ImageIcon("/Users/crprincipal2/Downloads/farviewscreen.png");
		img = i.getImage();
		time = new Timer(5, this);
		time.start();
	}

	public void actionPerformed(ActionEvent e) {
		p.move();
		repaint();		
	}
	
	public void paint(Graphics g) {
		super.paint(g);
		Graphics2D g2d = (Graphics2D) g;
		
		g2d.drawImage(img, 0, 0, null);
		g2d.drawImage(p.getImage(), p.getX(), p.getY(), null);
	}
	private class AL extends KeyAdapter {
		public void keyReleased(KeyEvent e) {
			p.keyRelesed(e);
		}
		public void keyPressed(KeyEvent e) {
			p.keyPressed(e);
		}
	}
}


and the current image i'm using is attached. This is still a beta question but behind my image there is a white background. I don't know how to get rid of it. To see it you need to change ImageIcon i = new ImageIcon("/Users/crprincipal2/Desktop/Jared/Rayright.jpg"); this in the character class to where ever you saved the image. But you probably already know that.

thanks in advance.

Attached image(s)

  • Attached Image


Is This A Good Question/Topic? 0
  • +

Replies To: Side scroller image problem

#2 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2684
  • View blog
  • Posts: 11,341
  • Joined: 20-September 08

Re: Side scroller image problem

Posted 01 August 2013 - 02:46 PM

Quote

This is still a beta question but behind my image there is a white background.
But that's the way you created it ..? If you want no white background, you should create it with a transparent one, which means you need png or gif
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1