MetKiller Joe
April 10th, 2009, 10:09 AM
http://img2.imageshack.us/img2/1876/littlephysicsapp.jpg
I have the area where you see those dots double-buffered. Is there anyway I can get that area to respond to mouse events? I did try to add the .addMouseListener to the image and graphics object, nothing. I tried listening to the area on the applet below the image, nothing. This is in awt.
Code:
package bouncy;
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Arrays;
public class Bouncy extends Applet implements MouseListener, MouseMotionListener{
Button newAtom;
Button reset;
Button Awesome;
boolean awesome = false;
int n = 0;
Button DragBox;
boolean dragBox = false;
TextField pxSecX;
TextField pxSecY;
TextField pxSec2X;
TextField pxSec2Y;
TextField size;
Checkbox xVector = new Checkbox("X Vector", true);
Checkbox yVector = new Checkbox("Y Vector", true);
Checkbox summedVector = new Checkbox("Summed Vector", true);
Checkbox Coordinates = new Checkbox("Coordinates", true);
Image backBuffer;
Graphics drawBuffer;
Atom[] atom;
int bW = 500;
int bH = 500;
int mX, mY;
int xAnchor = 10;
boolean boolX;
int yAnchor = 10;
boolean boolY;
int sumAnchor = (int)Math.sqrt(Math.pow(xAnchor, 2) + Math.pow(yAnchor, 2));
boolean boolSum;
int radius;
int radiusAnchor;
double velocity;
double accel;
boolean outBox = false;
boolean animate = false;
Point box = new Point(225, 10);
@Override
public void init() {
this.setSize(800, 600);
atom = new Atom[0];
backBuffer = createImage(bW - 1, bH - 1);
drawBuffer = backBuffer.getGraphics();
newAtom = new Button();
reset = new Button();
Awesome = new Button();
DragBox = new Button();
pxSecX = new TextField();
pxSecY = new TextField();
pxSec2X = new TextField();
pxSec2Y = new TextField();
size = new TextField();
add(newAtom);
newAtom.addMouseListener(this);
add(reset);
reset.addMouseListener(this);
add(DragBox);
DragBox.addMouseListener(this);
DragBox.addMouseMotionListener(this);
add(Awesome);
Awesome.addMouseListener(this);
add(pxSecX);
pxSecX.addMouseListener(this);
add(pxSecY);
pxSecY.addMouseListener(this);
add(pxSec2X);
pxSec2X.addMouseListener(this);
add(pxSec2Y);
pxSec2Y.addMouseListener(this);
add(size);
size.addMouseListener(this);
add(xVector);
xVector.addMouseListener(this);
add(yVector);
yVector.addMouseListener(this);
add(summedVector);
summedVector.addMouseListener(this);
add(Coordinates);
Coordinates.addMouseListener(this);
this.addMouseListener(this);
repaint();
}
@Override
public void update(Graphics g){
paint(g);
}
@Override
public void paint(Graphics g){
newAtom.setLocation(10, 10);
newAtom.setSize(65, 25);
newAtom.setLabel("Spawn");
reset.setLocation(10, 35);
reset.setSize(65, 25);
reset.setLabel("Reset");
Awesome.setLocation(10, 350);
Awesome.setSize(65, 25);
Awesome.setLabel("Awesome");
DragBox.setLocation(new Point((int)(box.getX() - 65), (int)box.getY()));
DragBox.setSize(65, 25);
DragBox.setLabel("Drag Box");
DragBox.hide();
DragBox.disable();
g.drawString("px/s (X)", 80, 75);
g.drawString("px/s (Y)", 80, 100);
g.drawString("px/s^2 (X)", 80, 125);
g.drawString("px/s^2 (Y)", 80, 150);
g.drawString("Radius", 80, 175);
pxSecX.setLocation(10, 60);
pxSecX.setSize(65, 25);
pxSecY.setLocation(10, 85);
pxSecY.setSize(65, 25);
pxSec2X.setLocation(10, 110);
pxSec2X.setSize(65, 25);
pxSec2Y.setLocation(10, 135);
pxSec2Y.setSize(65, 25);
size.setLocation(10, 160);
size.setSize(65, 25);
xVector.setLocation(10, 185);
yVector.setLocation(10, 210);
summedVector.setLocation(10, 235);
Coordinates.setLocation(10, 260);
if(dragBox) g.clearRect(185, 10, this.getWidth() - 185, this.getHeight() - 10);
g.drawRect((int)(box.getX()), (int)(box.getY()), bW, bH);
if(animate){
for(int frames = 0; frames < 200; frames++){
animation();
try
{Thread.sleep(16);}
catch(Exception e)
{System.err.println(e.getMessage());}
if(dragBox) g.clearRect(185, 10, this.getWidth() - 185, this.getHeight() - 10);
g.drawImage(backBuffer,(int)(box.getX() + 1), (int)(box.getY() + 1), this);
if(!awesome) drawBuffer.clearRect(0, 0, backBuffer.getWidth(this), backBuffer.getHeight(this));
}
}
}
public void animation() {
for(int atoms = 0; atoms < atom.length; atoms++){
drawBuffer.fillOval((int)atom[atoms].realX(), (int)atom[atoms].realY(), (int)atom[atoms].getSize(), (int)atom[atoms].getSize());
if(!awesome && Coordinates.getState()){
drawBuffer.drawString("X: " + (int)atom[atoms].getX(), (int)atom[atoms].getX() + (int)atom[atoms].getSize() + 1, (int)atom[atoms].getY());
drawBuffer.drawString("Y: " + (int)atom[atoms].getY(), (int)atom[atoms].getX() + (int)atom[atoms].getSize() + 1, (int)atom[atoms].getY() + 10);
}
//sum of velocity vectors
if(summedVector.getState()){
drawBuffer.setColor(Color.RED);
drawBuffer.drawLine((int)atom[atoms].getX(), (int)atom[atoms].getY(),
(int)(atom[atoms].getX() + atom[atoms].getXSpeed()),
(int)(atom[atoms].getY() + atom[atoms].getYSpeed()));
}
if(xVector.getState()){
//X vector
drawBuffer.setColor(Color.GREEN);
drawBuffer.drawLine((int)atom[atoms].getX(), (int)atom[atoms].getY(),
(int)(atom[atoms].getX() + atom[atoms].getXSpeed()),
(int)(atom[atoms].getY()));
}
if(yVector.getState()){
//Y vector
drawBuffer.setColor(Color.BLUE);
drawBuffer.drawLine((int)atom[atoms].getX(), (int)atom[atoms].getY(),
(int)atom[atoms].getX(),
(int)(atom[atoms].getY() + atom[atoms].getYSpeed()));
}
drawBuffer.setColor(Color.BLACK);
atom[atoms].detectCollision(atom);
atom[atoms].xAccel();
atom[atoms].yAccel();
atom[atoms].xMove();
atom[atoms].yMove();
}
}
public void mouseMoved(MouseEvent m){
}
public void mousePressed(MouseEvent m){
if(m.getSource() == newAtom){
animate = true;
if(!((pxSecX.getText().equals("") && pxSecY.getText().equals("")) &&
(pxSec2X.getText().equals("") && pxSec2Y.getText().equals("")) &&
(size.getText().equals("")))){
ArrayList al = new ArrayList(Arrays.asList(atom));
al.add(new Atom(new Point(150, 150),
Double.parseDouble(size.getText()),
Double.parseDouble(pxSecX.getText()),
Double.parseDouble(pxSecY.getText()),
Double.parseDouble(pxSec2X.getText()),
Double.parseDouble(pxSec2Y.getText()),
(int)(box.getX() + 1), (int)(box.getY() + 1), bW - 1, bH - 1));
atom = (Atom[])al.toArray(new Atom[al.size()]);
repaint();
}
}
if(m.getSource() == reset){
animate = true;
atom = new Atom[0];
drawBuffer.clearRect(0, 0, backBuffer.getWidth(this), backBuffer.getHeight(this));
repaint();
}
if(m.getSource() == Awesome){
if(!awesome){
awesome = true;
n++;
}
if(awesome && n != 1) awesome = false;
n = 0;
System.out.println(awesome);
}
if(m.getSource() == DragBox){
mX = m.getX();
mY = m.getY();
dragBox = true;
}
}
public void mouseDragged(MouseEvent m){
if(m.getSource() == DragBox && dragBox){
box.setLocation((int)(box.getX() + (m.getX() - mX)), (int)(box.getY() + (m.getY() - mY)));
mX = m.getX();
mY = m.getY();
repaint();
}
}
public void mouseReleased(MouseEvent m){
if(dragBox) dragBox = false;
}
public void mouseClicked(MouseEvent m){
}
public void mouseExited(MouseEvent m){
}
public void mouseEntered(MouseEvent m){
}
}
I have the area where you see those dots double-buffered. Is there anyway I can get that area to respond to mouse events? I did try to add the .addMouseListener to the image and graphics object, nothing. I tried listening to the area on the applet below the image, nothing. This is in awt.
Code:
package bouncy;
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Arrays;
public class Bouncy extends Applet implements MouseListener, MouseMotionListener{
Button newAtom;
Button reset;
Button Awesome;
boolean awesome = false;
int n = 0;
Button DragBox;
boolean dragBox = false;
TextField pxSecX;
TextField pxSecY;
TextField pxSec2X;
TextField pxSec2Y;
TextField size;
Checkbox xVector = new Checkbox("X Vector", true);
Checkbox yVector = new Checkbox("Y Vector", true);
Checkbox summedVector = new Checkbox("Summed Vector", true);
Checkbox Coordinates = new Checkbox("Coordinates", true);
Image backBuffer;
Graphics drawBuffer;
Atom[] atom;
int bW = 500;
int bH = 500;
int mX, mY;
int xAnchor = 10;
boolean boolX;
int yAnchor = 10;
boolean boolY;
int sumAnchor = (int)Math.sqrt(Math.pow(xAnchor, 2) + Math.pow(yAnchor, 2));
boolean boolSum;
int radius;
int radiusAnchor;
double velocity;
double accel;
boolean outBox = false;
boolean animate = false;
Point box = new Point(225, 10);
@Override
public void init() {
this.setSize(800, 600);
atom = new Atom[0];
backBuffer = createImage(bW - 1, bH - 1);
drawBuffer = backBuffer.getGraphics();
newAtom = new Button();
reset = new Button();
Awesome = new Button();
DragBox = new Button();
pxSecX = new TextField();
pxSecY = new TextField();
pxSec2X = new TextField();
pxSec2Y = new TextField();
size = new TextField();
add(newAtom);
newAtom.addMouseListener(this);
add(reset);
reset.addMouseListener(this);
add(DragBox);
DragBox.addMouseListener(this);
DragBox.addMouseMotionListener(this);
add(Awesome);
Awesome.addMouseListener(this);
add(pxSecX);
pxSecX.addMouseListener(this);
add(pxSecY);
pxSecY.addMouseListener(this);
add(pxSec2X);
pxSec2X.addMouseListener(this);
add(pxSec2Y);
pxSec2Y.addMouseListener(this);
add(size);
size.addMouseListener(this);
add(xVector);
xVector.addMouseListener(this);
add(yVector);
yVector.addMouseListener(this);
add(summedVector);
summedVector.addMouseListener(this);
add(Coordinates);
Coordinates.addMouseListener(this);
this.addMouseListener(this);
repaint();
}
@Override
public void update(Graphics g){
paint(g);
}
@Override
public void paint(Graphics g){
newAtom.setLocation(10, 10);
newAtom.setSize(65, 25);
newAtom.setLabel("Spawn");
reset.setLocation(10, 35);
reset.setSize(65, 25);
reset.setLabel("Reset");
Awesome.setLocation(10, 350);
Awesome.setSize(65, 25);
Awesome.setLabel("Awesome");
DragBox.setLocation(new Point((int)(box.getX() - 65), (int)box.getY()));
DragBox.setSize(65, 25);
DragBox.setLabel("Drag Box");
DragBox.hide();
DragBox.disable();
g.drawString("px/s (X)", 80, 75);
g.drawString("px/s (Y)", 80, 100);
g.drawString("px/s^2 (X)", 80, 125);
g.drawString("px/s^2 (Y)", 80, 150);
g.drawString("Radius", 80, 175);
pxSecX.setLocation(10, 60);
pxSecX.setSize(65, 25);
pxSecY.setLocation(10, 85);
pxSecY.setSize(65, 25);
pxSec2X.setLocation(10, 110);
pxSec2X.setSize(65, 25);
pxSec2Y.setLocation(10, 135);
pxSec2Y.setSize(65, 25);
size.setLocation(10, 160);
size.setSize(65, 25);
xVector.setLocation(10, 185);
yVector.setLocation(10, 210);
summedVector.setLocation(10, 235);
Coordinates.setLocation(10, 260);
if(dragBox) g.clearRect(185, 10, this.getWidth() - 185, this.getHeight() - 10);
g.drawRect((int)(box.getX()), (int)(box.getY()), bW, bH);
if(animate){
for(int frames = 0; frames < 200; frames++){
animation();
try
{Thread.sleep(16);}
catch(Exception e)
{System.err.println(e.getMessage());}
if(dragBox) g.clearRect(185, 10, this.getWidth() - 185, this.getHeight() - 10);
g.drawImage(backBuffer,(int)(box.getX() + 1), (int)(box.getY() + 1), this);
if(!awesome) drawBuffer.clearRect(0, 0, backBuffer.getWidth(this), backBuffer.getHeight(this));
}
}
}
public void animation() {
for(int atoms = 0; atoms < atom.length; atoms++){
drawBuffer.fillOval((int)atom[atoms].realX(), (int)atom[atoms].realY(), (int)atom[atoms].getSize(), (int)atom[atoms].getSize());
if(!awesome && Coordinates.getState()){
drawBuffer.drawString("X: " + (int)atom[atoms].getX(), (int)atom[atoms].getX() + (int)atom[atoms].getSize() + 1, (int)atom[atoms].getY());
drawBuffer.drawString("Y: " + (int)atom[atoms].getY(), (int)atom[atoms].getX() + (int)atom[atoms].getSize() + 1, (int)atom[atoms].getY() + 10);
}
//sum of velocity vectors
if(summedVector.getState()){
drawBuffer.setColor(Color.RED);
drawBuffer.drawLine((int)atom[atoms].getX(), (int)atom[atoms].getY(),
(int)(atom[atoms].getX() + atom[atoms].getXSpeed()),
(int)(atom[atoms].getY() + atom[atoms].getYSpeed()));
}
if(xVector.getState()){
//X vector
drawBuffer.setColor(Color.GREEN);
drawBuffer.drawLine((int)atom[atoms].getX(), (int)atom[atoms].getY(),
(int)(atom[atoms].getX() + atom[atoms].getXSpeed()),
(int)(atom[atoms].getY()));
}
if(yVector.getState()){
//Y vector
drawBuffer.setColor(Color.BLUE);
drawBuffer.drawLine((int)atom[atoms].getX(), (int)atom[atoms].getY(),
(int)atom[atoms].getX(),
(int)(atom[atoms].getY() + atom[atoms].getYSpeed()));
}
drawBuffer.setColor(Color.BLACK);
atom[atoms].detectCollision(atom);
atom[atoms].xAccel();
atom[atoms].yAccel();
atom[atoms].xMove();
atom[atoms].yMove();
}
}
public void mouseMoved(MouseEvent m){
}
public void mousePressed(MouseEvent m){
if(m.getSource() == newAtom){
animate = true;
if(!((pxSecX.getText().equals("") && pxSecY.getText().equals("")) &&
(pxSec2X.getText().equals("") && pxSec2Y.getText().equals("")) &&
(size.getText().equals("")))){
ArrayList al = new ArrayList(Arrays.asList(atom));
al.add(new Atom(new Point(150, 150),
Double.parseDouble(size.getText()),
Double.parseDouble(pxSecX.getText()),
Double.parseDouble(pxSecY.getText()),
Double.parseDouble(pxSec2X.getText()),
Double.parseDouble(pxSec2Y.getText()),
(int)(box.getX() + 1), (int)(box.getY() + 1), bW - 1, bH - 1));
atom = (Atom[])al.toArray(new Atom[al.size()]);
repaint();
}
}
if(m.getSource() == reset){
animate = true;
atom = new Atom[0];
drawBuffer.clearRect(0, 0, backBuffer.getWidth(this), backBuffer.getHeight(this));
repaint();
}
if(m.getSource() == Awesome){
if(!awesome){
awesome = true;
n++;
}
if(awesome && n != 1) awesome = false;
n = 0;
System.out.println(awesome);
}
if(m.getSource() == DragBox){
mX = m.getX();
mY = m.getY();
dragBox = true;
}
}
public void mouseDragged(MouseEvent m){
if(m.getSource() == DragBox && dragBox){
box.setLocation((int)(box.getX() + (m.getX() - mX)), (int)(box.getY() + (m.getY() - mY)));
mX = m.getX();
mY = m.getY();
repaint();
}
}
public void mouseReleased(MouseEvent m){
if(dragBox) dragBox = false;
}
public void mouseClicked(MouseEvent m){
}
public void mouseExited(MouseEvent m){
}
public void mouseEntered(MouseEvent m){
}
}