0

I am studying Java via the Stanford open courses in addition to other books.

Currently I am working on an assignment called Breakout.

I understand the concept of addMouseListeners() and addKeyListeners to the canvas.

However, I am having trouble conceptually (and on code level) understanding how to invoke a method call on an existing object that I have created on the canvas. My main issue is conceptual -- why can I not change the state of an existing object using mouse and key listeners when I have declared the object as a private instance variable as well.

Here is the most basic code to explain where I am having trouble. (Please note that I have imported the JSwing library as well -- however I have not used it thus far in the Stanford course or other resources that I have been using. Hence, I do not have an understanding of it). Any and all help is appreciated.

/*
 * File: Example.java
 * Name: Jasjeet
 */

import acm.graphics.*;
import acm.program.*;
import acm.util.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Example extends GraphicsProgram {  
    public void run() {
        int width = getWidth();             // width of window in pixels
        int height = getHeight();            // height of window in pixels

        int x = (width/2) - 36;              // x coordinates for GRect
        int y = (height/2) - 36;              // y coordinates for GRect


GRect outer = new GRect (x, y, 72, 72);              //Adding a new GRect
        add(outer);
        outer.setColor(Color.red);
        outer.setFillColor(Color.red);
        outer.setFilled(true);

addKeyListeners();                                // added Key Listeners to the canvas
addMouseListeners();                           // added Mouse Listeners to the   canvas
}

/* Trying to invoke a mouse click evnt below but it does not do anything to the GRect*/

public void mouseClicked(MouseEvent e) {  
    while (true) {
    if (e.getX() > 0 && e.getY()<50) {
    outer.move(1, 0);
    pause(50);
 }
 else {
    outer.move(-1, 0);
    pause(50);
 }
}

private GRect outer;             // Declared the GRect outer as a private instance var

}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • 3
    im not quite sure about acm library, but if you are using swing as you tagged , with `while(true)` you are freezing the gui, cause all gui stuff runs in the Event Dispatch Thread. – nachokk Sep 03 '13 at 15:42
  • 1
    If you _really_ need `acm.*`, you may need Java 1.5, as suggested [here](http://stackoverflow.com/a/12130829/230513). – trashgod Sep 03 '13 at 16:03
  • @trashgod -- the acm library was a part of the course instruction. They direct learners to use the acm. I will look at the link you have provided. Thanks. – Jasjeet Singh Sep 03 '13 at 19:53
  • @nachokk -- I am not familiar with the workings of Event Dispatch Thread. Will have to study it. Can you give a brief overview please? – Jasjeet Singh Sep 03 '13 at 19:54
  • @JasjeetSingh: I had to revert to 1.5 to get it to work; for Event Dispatch Thread reference, see [*Initial Threads*](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html). – trashgod Sep 03 '13 at 20:10
  • @trashgod -- thanks a lot ! I will go through the java tutorials for the various threads. I would rather jump ahead of the class and learn about the workings of threads and the swing class. Do you think I have any reasons to stick to the acm library? – Jasjeet Singh Sep 03 '13 at 20:48
  • @JasjeetSingh: I'd say consult you instructor, who may have chosen the library for other reasons. – trashgod Sep 04 '13 at 00:16

0 Answers0