Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-06-2007, 07:24 PM
Member
 
Join Date: Dec 2007
Location: TX
Posts: 1
MurderfaceX4 is on a distinguished road
I need help with my MouseMotionAdapter and MouseListener.
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.event.*;

public class Shooter extends Canvas implements MouseListener
{
public int width, height;
public BufferStrategy bs; // <--- lol, bs.
public final Image ship = Toolkit.getDefaultToolkit().createImage("Ship.bmp" );
public final Image ground = Toolkit.getDefaultToolkit().createImage("ground.bm p");
public final Cursor c = Toolkit.getDefaultToolkit().createCustomCursor(shi p, new Point(0, 0) , "cursor");
public Graphics g = bs.getDrawGraphics();
Thread t;
Frame f;

public boolean cached = false;

public Shooter()
{
Frame f = new Frame();
f.setCursor(c);
f.setLayout(new FlowLayout());
f.resize(900, 900);
f.addWindowListener(new WindowHandler());
f.setVisible(true);
try
{
repaint();
Thread.sleep( 1000 );
}
catch(Exception e) {System.out.println(e);}
}
public static void main(String[] args)
{

}

public void mouseClicked(MouseEvent arg0) {Toolkit.getDefaultToolkit().beep();}
public void mouseEntered(MouseEvent arg0) {}
public void mouseExited(MouseEvent arg0) {}
public void mousePressed(MouseEvent arg0) {}
public void mouseReleased(MouseEvent arg0) {}

}
class WindowHandler extends WindowAdapter
{
public void windowClosing(WindowEvent event)
{
System.exit(0);
}
}
class Ship
{
public int lives = 3;
public int speed = 3;
public boolean moving = false;
public boolean fire = true;

}
class Display extends Canvas
{
public Ship s = new Ship();
public static int score = 0;
public static int level = 1;

public final Image ship = Toolkit.getDefaultToolkit().createImage("Ship.bmp" );
public final Image ground = Toolkit.getDefaultToolkit().createImage("ground.bm p");

public static final int WIDTH = 900;
public static final int HEIGHT = 900;

public long gameTime;
public long numNums;
public Display()
{
Frame f = new Frame();
f.setSize(WIDTH, HEIGHT);
f.setLocation(50, 50);
f.setUndecorated(true);
f.add(this);
this.setFocusable(false);
f.addWindowListener(new WindowHandler());

}
public void drawScreen(Graphics g)
{
Graphics2D g2d = (Graphics2D) g;

g2d.drawImage(ship, -100, -100, this);
g2d.drawImage(ground, getWidth(), getHeight(), this);

g2d.setColor(Color.BLACK);
g2d.fillRect(0,0, getWidth(), getHeight());

g2d.setColor(Color.WHITE);
g2d.drawString("Score: " + score, 20, 20);
g2d.drawString("Lives: " + s.lives, 20, 30);
g2d.drawString("Level " + level, WIDTH - 60, 20);
}
public void update(Graphics g)
{
drawScreen(g);
}
}
class Mouse extends MouseAdapter
{
public void mouseClicked(MouseEvent me)
{
// i want to make it where you click the mouse and,
// a bullet will come out. I have the collision detecting already.
}
}

class Mouse2 extends MouseMotionAdapter
{
public void mouseMoved(MouseEvent me)
{
// i want to make the mouse the ship.
//like every time you move your mouse the ship moves.
}
}
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 12-07-2007, 05:13 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
When you compile and get something like this in the console
Code:
C:\jexp>javac shooterrx.java Note: shooterrx.java uses or overrides a deprecated API. Note: Recompile with -Xlint:deprecation for details.
you can do this to find out what is deprecated
Code:
C:\jexp>javac -Xlint:deprecation shooterrx.java shooterrx.java:27: warning: [deprecation] resize(int,int) in java.awt.Component has been deprecated f.resize(900, 900); ^ 1 warning
and then look up the method in the indicated api to find what to replace it with.
Code:
import java.awt.*; import java.awt.image.*; import java.awt.event.*; import javax.swing.event.MouseInputAdapter; public class ShooterRx extends Canvas { Image ground; Image ship; Thread t; public Ship s = new Ship(); public static int score = 0; public static int level = 1; public long gameTime; public long numNums; public boolean cached = false; public ShooterRx() { ship = loadImage("Ship.bmp"); //"images/dukeWaveRed.gif"); ground = loadImage("ground.bmp"); Cursor c = Toolkit.getDefaultToolkit(). createCustomCursor(ship, new Point(0, 0), "cursor"); setCursor(c); MouseHandler handler = new MouseHandler(); addMouseListener(handler); addMouseMotionListener(handler); } private Image loadImage(String path) { Image image = Toolkit.getDefaultToolkit().createImage(path); MediaTracker mt = new MediaTracker(this); mt.addImage(image, 0); try { mt.waitForID(0); } catch(InterruptedException e) { System.out.println("load interruption for: " + path); } return image; } public void paint(Graphics g) { drawScreen(g); } public void update(Graphics g) { // drawScreen(g); paint(g); } public void drawScreen(Graphics g) { Graphics2D g2d = (Graphics2D) g; g2d.setColor(Color.BLACK); g2d.fillRect(0,0, getWidth(), getHeight()); g2d.drawImage(ship, 200, 200, this); int x = 0; int y = getHeight() - ground.getHeight(this); g2d.drawImage(ground, x, y, this); g2d.setColor(Color.WHITE); g2d.drawString("Score: " + score, 20, 20); g2d.drawString("Lives: " + s.lives, 20, 30); g2d.drawString("Level " + level, getWidth() - 60, 20); } public static void main(String[] args) { ShooterRx app = new ShooterRx(); Frame f = new Frame(); // f.setLayout(new FlowLayout()); System.out.println("Frame default layout = " + f.getLayout().getClass().getName()); f.add(app); f.setSize(600, 600); f.addWindowListener(new WindowHandler()); f.setVisible(true); } private class MouseHandler extends MouseInputAdapter { public void mouseClicked(MouseEvent arg0) { // i want to make it where you click the mouse and, // a bullet will come out. I have the collision detecting already. Toolkit.getDefaultToolkit().beep(); } public void mouseMoved(MouseEvent me) { // i want to make the mouse the ship. //like every time you move your mouse the ship moves. // Should work if you make the ship the Cursor. } } } class WindowHandler extends WindowAdapter { public void windowClosing(WindowEvent event) { System.exit(0); } } class Ship { public int lives = 3; public int speed = 3; public boolean moving = false; public boolean fire = true; }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Null array when passed to MouseListener stevemcc New To Java 2 04-03-2008 12:42 AM


All times are GMT +3. The time now is 03:07 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org