i started by making this:
package test;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.Line2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.ListIterator;
import java.util.concurrent.ConcurrentHashMap;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
/** shoot a bal from 200, 200 in any direction. the system wil print one of eight quadrent which can be used for
* animation purpose - example: player click quadrent one then display western moving animation. plug in the
* center of the scren for a top down view two dimensional game and reverse the negative / positive symbol tied
* to a background. */
public class direction extends JFrame implements MouseListener{
/**
*
*/
private static final long serialVersionUID = 1L;
Integer x, y, reckey;
Double mx, my;
Boolean once;
Line2D line, line2, line3, line4, line5, line6, line7;
Rectangle rectangle[];
ConcurrentHashMap<Integer, movingObject> molist;
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stubfloat f = 102.236569f;
new direction();
//System.out.print(5.0 / 2.0);
}
public class movingObject{
Rectangle rec;
Double spe, quad, x2, y2;
public movingObject(double speed, int quadrent) {
spe = new Double(speed);
quad = new Double(quadrent);
x2 = new Double(200);
y2 = new Double(200);
rec = new Rectangle(x2.intValue(), y2.intValue(), 8, 8);
}
}
public direction() throws IOException {
reckey = new Integer(0);
molist = new ConcurrentHashMap<Integer, movingObject>();
line = new Line2D.Double(100, 100, 200, 122.5);
once = false;
setSize(700, 500);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addMouseListener(this);
x = getWidth() / 2;
y = getHeight() / 2;
Double one = line.getX2() - line.getX1();
Double two = line.getY2() - line.getY1();
//System.out.print(two / one);
}
public void paint(Graphics g) {
super.paint(g);
if (once.equals(false)) {
once = true;
// System.out.println(((45) / 100.0));
}
Iterator<movingObject> iterator = molist.values().iterator();
Integer removekey = new Integer(0);
while (iterator.hasNext()) {
movingObject mo = iterator.next();
g.drawRect(mo.rec.x, mo.rec.y, (int)mo.rec.getWidth(), (int)mo.rec.getHeight());
//System.out.println(mo.quad);
if (mo.quad == 1) {
mo.x2--;
mo.y2 = mo.y2 - mo.spe;
mo.rec.x = mo.x2.intValue();
mo.rec.y = mo.y2.intValue();
}
if (mo.quad == 2) {
mo.y2--;
mo.x2 = mo.x2 - mo.spe;
mo.rec.x = mo.x2.intValue();
mo.rec.y = mo.y2.intValue();
}
if (mo.quad == 3) {
mo.y2--;
mo.x2 = mo.x2 + mo.spe;
mo.rec.x = mo.x2.intValue();
mo.rec.y = mo.y2.intValue();
}
if (mo.quad == 4) {
mo.x2++;
mo.y2 = mo.y2 - mo.spe;
mo.rec.x = mo.x2.intValue();
mo.rec.y = mo.y2.intValue();
}
if (mo.quad == 5) {
mo.x2++;
mo.y2 = mo.y2 + mo.spe;
mo.rec.x = mo.x2.intValue();
mo.rec.y = mo.y2.intValue();
}
if (mo.quad == 6) {
mo.y2++;
mo.x2 = mo.x2 + mo.spe;
mo.rec.x = mo.x2.intValue();
mo.rec.y = mo.y2.intValue();
}
if (mo.quad == 7) {
mo.y2++;
mo.x2 = mo.x2 - mo.spe;
mo.rec.x = mo.x2.intValue();
mo.rec.y = mo.y2.intValue();
}
if (mo.quad == 8) {
mo.x2--;
mo.y2 = mo.y2 + mo.spe;
mo.rec.x = mo.x2.intValue();
mo.rec.y = mo.y2.intValue();
}
if (mo.rec.x <= 0) {
iterator.remove();
}
}
//g = getGraphics();
//g.drawImage(bufferedimage, x, y, this);
g.drawLine(200, 200, 100, (int)155);
g.drawLine(200, (int)200, (int)155, 100);
g.drawLine(200, 200, (int)245, 100);
g.drawLine(200, 200, (int)300, (int)155);
g.drawLine(200, 200, (int)300, (int)245);
g.drawLine(200, 200, (int)245, 300);
g.drawLine(200, 200, (int)155, 300);
g.drawLine(200, 200, (int)100, (int)245);
//g.draw(line);
repaint();
}
@Override
public void mouseClicked(MouseEvent e) {
if (e.getButton() == 3) {
/*
System.out.println("molist size: " + molist.size());
System.out.println("molist key: " + reckey);
*/
mx = e.getLocationOnScreen().getX();
my = e.getLocationOnScreen().getY();
double local = 200 - mx;
double local2 = 200 - my;
if (local > 0 && local2 > 0) {
if (local >= local2) {
molist.put(reckey, new movingObject(local2 / local, 1));
reckey++;
//System.out.print(local2 / local);
}
if (local < local2) {
molist.put(reckey, new movingObject(local / local2, 2));
reckey++;
//System.out.print(local / local2);
}
//System.out.println(local / local2);
if (local / local2 >= 2.5) {
System.out.println("quadrent one");
}
if (local / local2 < 2.5) {
if (local / local2 < .45) {
System.out.println("quadrent three");
}
if (local / local2 > .45) {
System.out.println("quadrent two");
}
}
}
if (local < 0 && local2 > 0) {
local = mx - 200; // local is mouseclicked x - 200
if (local <= local2) { // local2 is 200 - mouseclicked y
// shoots from 200, 200
molist.put(reckey, new movingObject(local / local2, 3));
reckey++;
//System.out.print(local2 / local);
}
if (local > local2) {
molist.put(reckey, new movingObject(local2 / local, 4));
reckey++;
}
//System.out.println(local / local2);
if (local / local2 <= .45) {
System.out.println("quadrent three");
}
if (local / local2 > .45) {
if (local / local2 <= 2.5) {
System.out.println("quadrent four");
}
if (local / local2 > 2.5) {
System.out.println("quadrent five");
}
}
}
if (local < 0 && local2 < 0) {
local = mx - 200;
local2 = my - 200;
if (local > local2) {
molist.put(reckey, new movingObject(local2 / local, 5));
reckey++;
}
if (local <= local2) {
molist.put(reckey, new movingObject(local / local2, 6));
reckey++;
}
if (local / local2 > 2) {
System.out.println("quadrent five");
}
if (local / local2 <= 2) {
if (local / local2 >= .45) {
System.out.println("quadrent six");
}
if (local / local2 < .45) {
System.out.println("quadrent seven");
}
}
//System.out.println(local / local2);
//System.out.println("botom left working");
}
if (local > 0 && local2 < 0) {
local2 = my - 200;
if (local < local2) {
molist.put(reckey, new movingObject(local / local2, 7));
reckey++;
}
if (local >= local2) {
molist.put(reckey, new movingObject(local2 / local, 8));
reckey++;
}
if (local / local2 > 2) {
System.out.println("quadrent one");
}
if (local / local2 <= 2) {
if (local / local2 < .45) {
System.out.println("quadrent seven");
}
if (local / local2 > .45) {
System.out.println("quadrent eight");
}
}
//System.out.println(local / local2);
//System.out.println("botom left working");
}
//e.getLocationOnScreen().getX() <= x + bufferedimage.getWidth() / 2 && e.getLocationOnScreen().getY() <= y + bufferedimage.getHeight() / 2 &&
//if (my / mx ) {
//System.out.print("test");
//}
}
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
}
for the background / player / enemy movement but then i wanted to use paintComponent instead of paint but the shooting was all buggy i think that might be the problem