Question : xcode: iPhone: How do I port this Java into Objective C

If I create a new xcode project to
1) draw an image at x,y
2) Move that image to wherever I click

I'd have a JFrame something like this..

int x,y;
constructor() {
setSize(400,100);
x=50;
y=50
image =  toolkit.getimage etc.
add mouse listener
}
void update() {
draw image at x,y;
}
void mousePressed(...) {
x = mouse / tap x
y = mouse / tap y
update();
}

What would that look like in Xcode?
What new xcode project type should I use?

Answer : xcode: iPhone: How do I port this Java into Objective C

It will look very different in Objective C.

Start by creating a new view based application in XCode, all of the work will be done in the UIViewController subclass that is generated as part of this project type.

In the viewDidLoad method you will need to init your image and image view set the initial positions and add the image view to it's superview.

You will also need to override the touch methods to pickup touch events, as this is a simple requirement just overriding the touchBegan method should be enough to get the location of the touch point.

Your view controller should look something like the attached code.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
@interface TestViewController : UIViewController {
	UIImageView		*imageView;
}

@property (readwrite, retain) UIImageView *imageView;

@end


@implementation TestViewController
@synthesize imageView;


- (void)viewDidLoad {
    [super viewDidLoad];

	UIImage *tmpImage = [UIImage imageNamed:@"block.png"];
	UIImageView *tmpImageView = [[UIImageView alloc] initWithImage:tmpImage];
	CGRect initialFrame = CGRectMake(0, 0, tmpImage.size.width, tmpImage.size.height);
	tmpImageView.frame = initialFrame;
	tmpImageView.center = CGPointMake(	self.view.frame.size.width / 2.0, 
										self.view.frame.size.height / 2.0);

	self.imageView = tmpImageView;
	[self.view addSubview:self.imageView];
	[tmpImageView release];
	tmpImageView = nil;

}


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
	if ([touches count] == 1) {
		// Single touch event, we will process here
		UITouch *touch = [touches anyObject]; // Single object in the set so we can trust this
		CGPoint location = [touch locationInView:self.view];

		[UIView beginAnimations:@"MyAnimation" context:nil];  // only needed if you want to animate the move
		[UIView setAnimationDuration:0.25f];                  // only needed if you want to animate the move
		self.imageView.center = location;
		[UIView commitAnimations];                            // only needed if you want to animate the move
		
	} else {
		// Not applicable to us pass to next responder
		[[self nextResponder] touchesBegan:touches withEvent:event];
	}

}
Random Solutions  
 
programming4us programming4us