Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

One thing youll notice is a lot of null pointer exceptions as well as the code not working too well. Try dragging upwards and to

One thing youll notice is a lot of null pointer exceptions as well as the code not working too well. Try dragging upwards and to the left instead of downwards and to the right, you may notice that one feels more like its dragging then the other. Two things can happen here. Lets deal with the first one, which is a null pointer exception. This can happen if we tend to click in an area where an object is not present. If we ever do this and call getElementAt, getElementAt returns null. This means that if we try to call say setLocation on null, well get a null pointer exception raised. Java is nice in that it continues to try to run the program and recover afterwards, but youll notice this happen a lot. An easy way for us to stop generating the exceptions is to add an if statement after our getElementAt. We only want to set the location of toDrag if in fact, toDrag is not null. Otherwise, we are trying to set the location of a null object. Go ahead and add an if statement to only call setLocation on toDrag if toDrag is not null and then run the program again (Ctrl-F11). What youll notice now is that no more exceptions are being generated but the behavior is pretty much the same - we cant drag upwards and to the left. The reason for this is what happens with setLocation. Remember that here setLocation is being called on the upper-left hand corner of the object, which could make it so that once we call a drag event, the mouse cursor is already outside the bounds of the object, and so it is not on top of an object anymore.

One easy fix that we can use is to change our setLocation so that we set the location to be the center of the object. Thus rather than say setLocation (x, y) we need to subtract half of the shapes width and height from x and y, which are both defined as SHAPE_SIZE. Go ahead and make that change now and re-run the program, your program should now behave like this:

I can't get my objects to drag smoothly around the window without it stopping and I can no longer move the object.

Here is my code:

import acm.graphics.*; import acm.program.*; import acm.util.*; import java.awt.*; import java.awt.event.*;

public class Dragging extends GraphicsProgram { public static final int WINDOW_WIDTH = 800; public static final int WINDOW_HEIGHT = 600; public static final int SHAPE_SIZE = 100; private GObject toDrag; public void run() { GOval oval = new GOval(100, 100, SHAPE_SIZE, SHAPE_SIZE); oval.setColor(Color.blue); oval.setFilled(true); add(oval); GRect rect = new GRect(500, 500, SHAPE_SIZE, SHAPE_SIZE); rect.setColor(Color.green); rect.setFilled(true); add(rect); addMouseListeners(); } @Override public void mousePressed(MouseEvent e) { toDrag = getElementAt(e.getX(), e.getY()); } @Override public void mouseDragged(MouseEvent e) { if(toDrag != null) { toDrag.setLocation(SHAPE_SIZE/2, SHAPE_SIZE/2); } } @Override public void mouseReleased(MouseEvent e) { toDrag = null; } public void init() { setSize(WINDOW_WIDTH, WINDOW_HEIGHT); requestFocus(); } public static void main(String[] args) { new Dragging().start(); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Programming With Visual Basic .NET

Authors: Carsten Thomsen

2nd Edition

1590590325, 978-1590590324

More Books

Students also viewed these Databases questions

Question

Why is the System Build Process an iterative process?

Answered: 1 week ago