UIBezierPath colour - uibezierpath

I followed this tutorial to create a drawing application. It all works really well at making smooth curves. The problem is that as I draw the line is black, and once I let go it makes the line the colour that I want. The tutorial does not go into line colour, but I have altered it. The only problem is that it's black until touchesEnded runs.
Here is my code:
This first section sets the colour to black.
- (id)initWithCoder:(NSCoder *)aDecoder
{
if (self = [super initWithCoder:aDecoder])
{
[self setMultipleTouchEnabled:YES];
path = [UIBezierPath bezierPath];
[path setLineWidth:10.0];
red = 0.0/255.0;
green = 0.0/255.0;
blue = 0.0/255.0;
brush = 10.0;
opacity = 0.8;
toolSelected = 1;
bgImage = 1;
}
return self;
}
Then on touchesEnded this code is ran:
- (void)drawBitmap
{
UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0.0);
if (!incrementalImage) // first time; paint background white
{
UIBezierPath *rectpath = [UIBezierPath bezierPathWithRect:self.bounds];
[[UIColor clearColor] setFill];
[rectpath fill];
}
[incrementalImage drawAtPoint:CGPointZero];
UIColor *colour = [UIColor colorWithRed:red green:green blue:blue alpha:opacity];
[colour setStroke];
[path stroke];
incrementalImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
I assume that something needs to be ran in the touchesMoved to colourise the bezier path, but I'm just really struggling with it right now.
Can anyone help?
Thanks

It was achieved in the end by adding these two lines to this section
- (void)drawRect:(CGRect)rect
{
[[UIColor redColor]setStroke]; // Added these
[[UIColor redColor]setFill]; // two lines
[incrementalImage drawInRect:rect];
[path stroke];
}

Related

How to set style color for a line in javafx?

How to set style color for a line in javafx?
public void setColor(String color) {
for (int i = 0; i < 9; i++){
//lines[i].setFill(Color.BLUE);
//lines[i].setStyle("-fx-Background-color: yellow;");
//lines[i].setStyle("-fx-color:"+ color);
//setStyle("-fx-Foreground-color:"+ color);
}
}
All 4 comments do nothing the lines not colored.
I would be happy if you could help me.
Use -fx-stroke for coloring lines (using CSS)
line.setStyle("-fx-stroke: red;");
Or call setStroke() for coloring lines (using Java API):
line.setStroke(Color.RED);
I suggest using a for loop to get the children of the "lines"array and then using "-fx-stroke:" as ItachiUchiha suggested but adding the color to the string.
Here is the code:
public void setColor(String color) {
for (Line line:lines){
line.setStyle("-fx-stroke:"+ color);
}
}
I hope this helps. If you have any question just ask.

turning the corners of CGRect round like with UIView

I have this function that basically adds a given text below a given image, I wish to make the corners of textRect round, can you help me understand how to use UIBezierPath in this code
-(UIImage*) overlapText:(NSString*) p_text inImage:(UIImage*) p_image atPoint:(CGPoint) p_point
{
p_point.y += p_image.size.height-5;
UIFont *font = [UIFont boldSystemFontOfSize:11];
UIGraphicsBeginImageContext(CGSizeMake(p_image.size.width+15,p_image.size.height+15));
CGFloat imageX = (p_image.size.width+10)/2 - (p_image.size.width/2);
[p_image drawInRect:CGRectMake(imageX,0,p_image.size.width,p_image.size.height)];
CGRect textRect = CGRectMake(0, p_point.y, p_image.size.width+15, p_image.size.height+10);
[[UIColor colorWithRed:(70/255.0) green:(70/255.0) blue:(70/255.0) alpha:1] set];
CGContextFillRect(UIGraphicsGetCurrentContext(), textRect);
[[UIColor whiteColor] set];
[p_text drawInRect:textRect withFont:font lineBreakMode:NSLineBreakByTruncatingTail alignment:NSTextAlignmentCenter];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
Your image context is the current context, so just construct the rounded rect with bezierPathWithRoundedRect:cornerRadius:, set your fill color, and tell the bezier path to fill. It's one line of code (well, two if you count setting the fill color):
[[UIColor colorWithRed:(70/255.0) green:(70/255.0) blue:(70/255.0) alpha:1] set];
[[UIBezierPath bezierPathWithRoundedRect:textRect cornerRadius:5] fill];
Feel free to play with the numbers here; for example, you might like a different corner radius. Experiment!

UINavigationItem multiple line prompt text

Can any body give me the solution for displaying UINavigationItem prompt text in 2 lines?
There is not a built-in way to do this. Below is a work-around that seems to work pretty well that I put together from stackOverflow post UINavigationItem with prompt and activity indicator
Here is a simulator screen shot of what it creates:
Note that since the text is a UILabel you can modify its color, font, or anything else too.
// I have this code in viewDidLoad
UIView *viewContainingPrompt;
UIBarButtonItem *promptButtonItem;
// Configuring the prompt title of the navigation bar so it is present but empty
[self.navigationItem setPrompt: #""];
// We will create a UIBarButtonItem that has a custom view (viewContainingPrompt).
// A subview of viewContainingPrompt will be a UILabel (headerLabel)
// We need to have this "intermediate" view to position the label at the right position
// (the UIBarButtonItem ignores the origin and height of its custom view)
viewContainingPrompt = [[UIView alloc] initWithFrame: CGRectMake(0, 0, 0, 85)];
viewContainingPrompt.autoresizingMask = UIViewAutoresizingFlexibleWidth;
// Choose a width that puts 10 points on either end...
CGFloat labelWidth = self.navigationController.navigationBar.bounds.size.width - 20.0;
// Note that the '-60' below is determined by the width of the back button
// If someone can figure out how to determine this width at runtime this code
// would be much more robust.
UILabel *headerLabel = [[UILabel alloc] initWithFrame: CGRectMake(-60,-8,labelWidth,36)];
headerLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
headerLabel.text = #"A quite long prompt string that will wrap to a second line to demonstrate multiline prompt.";
headerLabel.font = [UIFont systemFontOfSize: 14];
headerLabel.numberOfLines = 0; // Zero gives as many lines as will fit, could be 2
headerLabel.backgroundColor = [UIColor clearColor];
headerLabel.textColor = [UIColor colorWithRed: .1 green: .1 blue: .2 alpha: 0.8f];
headerLabel.shadowColor = [UIColor colorWithRed: 1 green: 1 blue: 1 alpha: 0.5f];
headerLabel.shadowOffset = CGSizeMake( 0, 1 );
headerLabel.textAlignment = UITextAlignmentCenter;
[viewContainingPrompt addSubview: headerLabel];
//[headerLabel release]; // Uncomment if not using ARC
promptButtonItem = [[UIBarButtonItem alloc] initWithCustomView: viewContainingPrompt];
self.navigationItem.leftBarButtonItem = promptButtonItem;
self.navigationItem.leftItemsSupplementBackButton = YES;
//[viewContainingPrompt release]; // Uncomment if not using ARC
//[promptButtonItem release]; // Uncomment if not using ARC
I would appreciate anyone's feedback on how to figure out the width of the back button during execution so that width did not have to be hard coded.
As it is I do not think there are any private APIs or other illegal code contained.

Curve Sprite Movement COCOS2D

I'm trying with no luck to move a sprite position in an simetric curve way with the touch drag. So far I use the following code with no luck
-(BOOL) ccTouchBegan:(UITouch*)touch withEvent:(UIEvent *)event
{
lastTouchLocation = [MultiLayer locationFromTouch:touch];
return YES;
}
-(void) ccTouchMoved:(UITouch*)touch withEvent:(UIEvent *)event
{
CGPoint currentTouchLocation = [MultiLayer locationFromTouch:touch];
moveTo = ccpSub(lastTouchLocation, currentTouchLocation);
lastTouchLocation = currentTouchLocation;
}
-(void) update:(ccTime)delta
{
CCSprite* sprite = [sprites objectAtIndex:1];
if (moveTo.x != 0){
float X = sprite.position.x + moveTo.x;
float Y = sprite.position.y + (pow(X,2));
sprite.position = CGPointMake(X, Y);
}
}
The curve way i'm trying to simulate is in the form y=x^2. Is this posible?
Thanks in advance!
Yes it's possible. You can do it manually as your are trying, but it's better to use a built-in solution. There is a CCJumpTo and CCJumpBy actions in cocos2d engine. They are created for parabolic moves. Use them

openGL texturing shows only red component of texture

I am trying to texture map an image to a single polygon. My image is being read correctly, but only the red plane of the image is being textured.
I am doing this within a QGLWidget
I have checked the image after it is read, and it's components are being read correctly--ie, I get valid values for the green and blue planes.
Here is the code
QImageReader *theReader = new QImageReader();
theReader->setFileName(imageFileName);
QImage theImageRead = theReader->read();
if(theImageRead.isNull())
{
validTile = NOT_VALID_IMAGE_FILE;
return;
}
else
{
int newW = 1;
int newH = 1;
while(newW < theImageRead.width())
{
newW *= 2;
}
while(newH < theImageRead.height())
{
newH *= 2;
}
theImageRead = theImageRead.scaled(newW, newH, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
// values checked in theImageRead are OK here
glGenTextures(1,&textureObject);
theTextureImage = QGLWidget::convertToGLFormat(theImageRead);
// values checked in theTextureImage are OK here
glBindTexture(GL_TEXTURE_2D, textureObject);
glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,newW, newH, 0, GL_RGBA, GL_UNSIGNED_BYTE,theTextureImage.bits() );
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glFlush();
validTile = VALID_TEXTURE;
return;
}
then I draw like this:
{
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,textureTiles[tN]->getTextureObject() );
glBegin(GL_QUADS);
glTexCoord2f(0.0,0.0);
glVertex2f(textureTiles[tN]->lowerLeft.x(), textureTiles[tN]->lowerLeft.y());
glTexCoord2f(1.0,0.0);
glVertex2f(textureTiles[tN]->lowerRight.x(), textureTiles[tN]->lowerRight.y());
glTexCoord2f(1.0,1.0);
glVertex2f(textureTiles[tN]->upperRight.x(), textureTiles[tN]->upperRight.y());
glTexCoord2f(0.0,1.0);
glVertex2f(textureTiles[tN]->upperLeft.x(), textureTiles[tN]->upperLeft.y());
glEnd();
glDisable(GL_TEXTURE_2D);
}
Does anybody see anything that would cause my texture to be interpreded as if it is values of (r,0,0,1)? (r,g,b,a)?
QT 4.7.1, Ubuntu 10.04, openGl 2.something or other
thanks in advance for any help
I've had a similar problem. I found that I had to "reset" the gl color to white and opaque before drawing a texturized quad, or the colors would get messed up. Like this:
...
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,textureTiles[tN]->getTextureObject() );
glColor4f(1.0, 1.0, 1.0, 1.0); // reset gl color
glBegin(GL_QUADS);
...
This is a very common problem. First, set your MIN/MAG filters to something, since the defaults use mipmaps, and since you didn't provide mipmaps, the texture is incomplete.
Sampling a incomplete texture usually gives white. A glColor call with the default texture environment will multiply the vertex color with the texture color. You probably have something like glColor(red), and red * white = red, so that's why you're seeing red.
To fix it, set the MIN/MAG filters to GL_LINEAR or GL_NEAREST.

Resources