Results 1 to 20 of 23
Thread: Homework help please
- 03-17-2010, 03:16 AM #1
Member
- Join Date
- Mar 2010
- Posts
- 17
- Rep Power
- 0
Homework help please
hello all,
I am new to java and am having problems finding information on how to do this problem for my class here is the problem, and then I have also including the code I have so far. any help is appreciated thanks!
FRAME
Create a new method:
public Picture frame(int frameSize, Color frameColor)
in Picture.java. This method will create and return a new picture object. The new picture object
will contain the original picture inside a frame. The thickness of the frame is given by the parameter
frameSize. The color of the frame is given by the parameter frameColor. The new picture object
will be larger than the original because of the frame.
MIRROR
Create a new method:
public Picture mirror()
in Picture.java. This method will create and return a new picture object. The new picture object
will contain a mirrored version of the original picture. The mirroring will be done across a vertical axis,
so things that were on the left will now be on the right and things that were on the right will now be on
the left.
MIRRORFRAME
Create a new method:
public Picture mirrorFrame(int frameSize, Color frameColor)
in Picture.java. This method will create and return a new picture object. The new picture object
will contain a mirrored version of the original picture inside a frame. The thickness of the frame is
given by the parameter frameSize. The color of the frame is given by the parameter frameColor.
The mirroring will be the same as in the mirror method.
HERE IS THE CODE I HAVE WRITTEN SO FAR:
however when compiling i do receive an error that says:
1 error found:
File: C:\Users\Matt\Desktop\bookClasses\Picture.java [line: 92]
Error: C:\Users\Matt\Desktop\bookClasses\Picture.java:92: reached end of file while parsing
public Picture frame(int frameSize, Color frameColor)
{
public Picture mirror()
{
int width= this.getWidth();
int mirrorPoint = width / 2;
Pixel leftPixel = null;
Pixel rightPixel = null;
for (int y = 0; y < getHeight(); y++)
{
for (int x = 0; x < mirrorPoint; x++)
{
leftPixel = getPixel(x, y);
rightPixel = getPixel(width - 1 - x, y);
rightPixel.setColor(leftPixel.getColor());
}
}
}
THANKS IN ADVANCE =)
-
Can you post your entire Picture class using code tags? For details on how to do this, please see my signature below.
Much luck!
- 03-17-2010, 03:28 AM #3
Member
- Join Date
- Mar 2010
- Posts
- 17
- Rep Power
- 0
import java.awt.*;
import java.awt.font.*;
import java.awt.geom.*;
import java.awt.image.BufferedImage;
import java.text.*;
import java.util.*;
import java.util.List; // resolves problem with java.awt.List and java.util.List
/**
* A class that represents a picture. This class inherits from
* SimplePicture and allows the student to add functionality to
* the Picture class.
*
* Copyright Georgia Institute of Technology 2004-2005
* @author Barbara Ericson ericson@cc.gatech.edu
*/
public class Picture extends SimplePicture
{
///////////////////// constructors //////////////////////////////////
/**
* Constructor that takes no arguments
*/
public Picture ()
{
/* not needed but use it to show students the implicit call to super()
* child constructors always call a parent constructor
*/
super();
}
/**
* Constructor that takes a file name and creates the picture
* @param fileName the name of the file to create the picture from
*/
public Picture(String fileName)
{
// let the parent class handle this fileName
super(fileName);
}
/**
* Constructor that takes the width and height
* @param width the width of the desired picture
* @param height the height of the desired picture
*/
public Picture(int width, int height)
{
// let the parent class handle this width and height
super(width,height);
}
/**
* Constructor that takes a picture and creates a
* copy of that picture
*/
public Picture(Picture copyPicture)
{
// let the parent class do the copy
super(copyPicture);
}
/**
* Constructor that takes a buffered image
* @param image the buffered image to use
*/
public Picture(BufferedImage image)
{
super(image);
}
////////////////////// methods ///////////////////////////////////////
public Picture frame(int frameSize, Color frameColor)
{
public Picture mirror()
{
int width= this.getWidth();
int mirrorPoint = width / 2;
Pixel leftPixel = null;
Pixel rightPixel = null;
for (int y = 0; y < getHeight(); y++)
{
for (int x = 0; x < mirrorPoint; x++)
{
leftPixel = getPixel(x, y);
rightPixel = getPixel(width - 1 - x, y);
rightPixel.setColor(leftPixel.getColor());
}
}
}
// this is the end of class Picture, put all new methods before this
before the methods line, I have left untouched. hope this is what you were looking for.
-
Please edit the post above to add code tags (as suggested). Much luck.
edit: also, have you matched all your curly braces, including the main one for the class itself? Are you missing a closing brace "}"?Last edited by Fubarable; 03-17-2010 at 04:06 AM.
- 03-18-2010, 06:31 PM #5
Member
- Join Date
- Mar 2010
- Posts
- 17
- Rep Power
- 0
Java Code:import java.awt.*; import java.awt.font.*; import java.awt.geom.*; import java.awt.image.BufferedImage; import java.text.*; import java.util.*; import java.util.List; // resolves problem with java.awt.List and java.util.List /** * A class that represents a picture. This class inherits from * SimplePicture and allows the student to add functionality to * the Picture class. * * Copyright Georgia Institute of Technology 2004-2005 * @author Barbara Ericson [email]ericson@cc.gatech.edu[/email] */ public class Picture extends SimplePicture { ///////////////////// constructors ////////////////////////////////// /** * Constructor that takes no arguments */ public Picture () { /* not needed but use it to show students the implicit call to super() * child constructors always call a parent constructor */ super(); } /** * Constructor that takes a file name and creates the picture * @param fileName the name of the file to create the picture from */ public Picture(String fileName) { // let the parent class handle this fileName super(fileName); } /** * Constructor that takes the width and height * @param width the width of the desired picture * @param height the height of the desired picture */ public Picture(int width, int height) { // let the parent class handle this width and height super(width,height); } /** * Constructor that takes a picture and creates a * copy of that picture */ public Picture(Picture copyPicture) { // let the parent class do the copy super(copyPicture); } /** * Constructor that takes a buffered image * @param image the buffered image to use */ public Picture(BufferedImage image) { super(image); } ////////////////////// methods /////////////////////////////////////// public Picture frame(int frameSize, Color frameColor) { public Picture mirror() { int width= this.getWidth(); int mirrorPoint = width / 2; Pixel leftPixel = null; Pixel rightPixel = null; for (int y = 0; y < getHeight(); y++) { for (int x = 0; x < mirrorPoint; x++) { leftPixel = getPixel(x, y); rightPixel = getPixel(width - 1 - x, y); rightPixel.setColor(leftPixel.getColor()); } } }
// this is the end of class Picture, put all new methods before this
before the methods line, I have left untouched. hope this is what you were looking for.[/QUOTE]
- 03-18-2010, 07:27 PM #6
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
You pretty much have the right idea with your mirror() method, but when you take a pixel from the left and put it on the right, you need to also take the pixel that was on the right and put it on the left. This means you're going to need a tempPixel (tempColor?) variable to store it in.
Hope that helps. Your frame() method should be easy enough, and of course mirrorFrame() will simply call mirror() then frame().Java Code:Color tempColor = rightPixel.getColor(); // not sure about the Color type here -- check that rightPixel.setColor(leftPixel.getColor()); leftPixel.setColor(tempColor);
-Gary-
- 03-18-2010, 07:43 PM #7
Member
- Join Date
- Mar 2010
- Posts
- 17
- Rep Power
- 0
im getting a missing return statement errorJava Code:public Picture mirror() { int width = this.getWidth(); int mirrorPoint = width / 2; Pixel leftPixel = null; Pixel rightPixel = null; for (int y = 0; y < getHeight(); y++) { for (int x = 0; x < mirrorPoint; x++) { leftPixel = getPixel(x,y); rightPixel = getPixel(width - 1 - x, y); } } }}
or when that doesn't happen I am getting an incorrect parsing error. I checked all my brackets and they do coorelate, and I am having trouble starting the frame method, do I scaleUp the picture first or is there a way to create a frame using java a different iway. this is the method I am having the most trouble with. can anyone help describe how the loops might go, I can't figure out the way to create a frame.
- 03-18-2010, 08:15 PM #8
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
Well, in the last code you posted, you had this:
So, yeah, you were missing a closing brace on your frame() method. Unless you have fixed that, it will definitely cause you problems.Java Code:public Picture frame(int frameSize, Color frameColor) { public Picture mirror() { int width= this.getWidth(); ...
-Gary-
- 03-18-2010, 08:20 PM #9
Member
- Join Date
- Mar 2010
- Posts
- 17
- Rep Power
- 0
i fixed that, but can you help me start the frame method, do I need to scale up. and now I am getting an error, that says
1 error found:
File: C:\Users\Matt\Desktop\bookClasses\Picture.java [line: 132]
Error: C:\Users\Matt\Desktop\bookClasses\Picture.java:132 : reached end of file while parsing
this is the code I have now :
any advice on the Frame method, or how to get rid of my error.Java Code:public Picture frame() { { Picture targetPicture = new Picture(this.getWidth() * 2, this.getHeight() * 2); Pixel sourcePixel = null; Pixel targetPixel = null; int targetX = 0; int targetY = 0; for (int sourceX = 0; sourceX < this.getWidth(); sourceX++) { for (int sourceY = 0; sourceY < this.getHeight(); sourceY++) { sourcePixel = this.getPixel (sourceX, sourceY); for (int indexY = 0; indexY < 2; indexY++) { for (int indexX = 0; indexX < 2; indexX++) { targetX = sourceX * 2 + indexX; targetY = sourceY * 2 + indexY; targetPixel = targetPicture.getPixel(targetX,targetY); targetPixel.setColor(sourcePixel.getColor()); } } } } return targetPicture; } } public Picture mirror() { int width = this.getWidth(); int mirrorPoint = width / 2; Pixel leftPixel = null; Pixel rightPixel = null; for (int y = 0; y < getHeight(); y++) { for (int x = 0; x < mirrorPoint; x++) { leftPixel = getPixel(x,y); rightPixel = getPixel(width - 1 - x, y); Color tempColor = rightPixel.getColor(); rightPixel.setColor(leftPixel.getColor()); leftPixel.setColor(tempColor); } } return targetPicture; }
- 03-18-2010, 08:27 PM #10
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
What's on line 132? We don't have line numbers here, and your code has omitted the imports and whatever else may have been at the top.
-Gary-
- 03-18-2010, 08:31 PM #11
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,406
- Blog Entries
- 7
- Rep Power
- 17
- 03-18-2010, 08:32 PM #12
Member
- Join Date
- Mar 2010
- Posts
- 17
- Rep Power
- 0
with the error :Java Code:import java.awt.*; import java.awt.font.*; import java.awt.geom.*; import java.awt.image.BufferedImage; import java.text.*; import java.util.*; import java.util.List; // resolves problem with java.awt.List and java.util.List /** * A class that represents a picture. This class inherits from * SimplePicture and allows the student to add functionality to * the Picture class. * * Copyright Georgia Institute of Technology 2004-2005 * @author Barbara Ericson ericson@cc.gatech.edu */ public class Picture extends SimplePicture { ///////////////////// constructors ////////////////////////////////// /** * Constructor that takes no arguments */ public Picture () { /* not needed but use it to show students the implicit call to super() * child constructors always call a parent constructor */ super(); } /** * Constructor that takes a file name and creates the picture * @param fileName the name of the file to create the picture from */ public Picture(String fileName) { // let the parent class handle this fileName super(fileName); } /** * Constructor that takes the width and height * @param width the width of the desired picture * @param height the height of the desired picture */ public Picture(int width, int height) { // let the parent class handle this width and height super(width,height); } /** * Constructor that takes a picture and creates a * copy of that picture */ public Picture(Picture copyPicture) { // let the parent class do the copy super(copyPicture); } /** * Constructor that takes a buffered image * @param image the buffered image to use */ public Picture(BufferedImage image) { super(image); } ////////////////////// methods /////////////////////////////////////// public Picture frame() { { Picture targetPicture = new Picture(this.getWidth() * 2, this.getHeight() * 2); Pixel sourcePixel = null; Pixel targetPixel = null; int targetX = 0; int targetY = 0; for (int sourceX = 0; sourceX < this.getWidth(); sourceX++) { for (int sourceY = 0; sourceY < this.getHeight(); sourceY++) { sourcePixel = this.getPixel (sourceX, sourceY); for (int indexY = 0; indexY < 2; indexY++) { for (int indexX = 0; indexX < 2; indexX++) { targetX = sourceX * 2 + indexX; targetY = sourceY * 2 + indexY; targetPixel = targetPicture.getPixel(targetX,targetY); targetPixel.setColor(sourcePixel.getColor()); } } } } return targetPicture; } } public Picture mirror() { int width = this.getWidth(); int mirrorPoint = width / 2; Pixel leftPixel = null; Pixel rightPixel = null; Pixel targetPixel = null; for (int y = 0; y < getHeight(); y++) { for (int x = 0; x < mirrorPoint; x++) { leftPixel = getPixel(x,y); rightPixel = getPixel(width - 1 - x, y); Color tempColor = rightPixel.getColor(); rightPixel.setColor(leftPixel.getColor()); leftPixel.setColor(tempColor); } } return targetPicture; } } // this is the end of class Picture, put all new methods before this
1 error found:
File: C:\Users\Matt\Desktop\bookClasses\Picture.java [line: 132]
Error: C:\Users\Matt\Desktop\bookClasses\Picture.java:132 : cannot find symbol
symbol : variable targetPicture
location: class Picture
i am sorry i dont know how to do line numbers. this is really frustrating I keep getting different errors and i can't run it to see if I have mirror or frame
- 03-18-2010, 08:39 PM #13
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
First of all, I don't know the Picture or SimplePicture classes you're working with, so I'm just going based on the code and instructions you've shown us so far.
For your frame() method, you're given frameSize in pixels as a parameter. You'll create a new Picture, and its width will be 2 * frameSize + oldWidth, and its height will be 2 * frameSize + oldHeight. Now, I don't know if your Picture class provides any methods for slapping a whole image into a specified place on a new Picture object -- if it does provide them, use them. What you've shown us so far is getPixel(), setPixel(), getColor() and setColor(). If that's all you've got, then you need to go something like this:
That gets you to the bottom of the top frame. Now:Java Code:for frameSize rows for 0 to newWidth set pixel x, y to frameColor
Now you just have to do the bottom part of the frame, which is pretty much like the top:Java Code:for oldHeight rows for 0 to frameSize set pixel x, frameSize + y to frameColor for 0 to oldWidth get oldPixel x, y from oldPicture get oldColor from oldPixel set pixel frameSize + x, frameSize + y to oldColor for 0 to frameSize set pixel frameSize + oldWidth + x, frameSize + y to frameColor
Again, this is doing things a pixel at a time. It will work, but if you have methods available that allow you to do things in blocks, you should do that.Java Code:for frameSize rows for 0 to newWidth set pixel x, frameSize + oldHeight + y to frameColor
-Gary-
- 03-18-2010, 08:41 PM #14
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
By the way, you don't want to scale the Picture -- you just want to place it in the center of the new Picture.
-Gary-
- 03-18-2010, 08:42 PM #15
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,406
- Blog Entries
- 7
- Rep Power
- 17
- 03-18-2010, 08:55 PM #16
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
Jos makes an excellent point, and since you're generating a new Picture for the mirror() method, you don't need to do the swapping I described earlier. You just go all the way across each row of the old Picture, pulling pixels from the left and putting them on the right of the new Picture. Write your method so that you have an optional offset to x and y -- that way you can use it in mirrorFrame() as well. You don't have to do your frame() row by row like I described above. You can do the top, the left, the right, the bottom, and the Picture independently (and it's probably better -- simpler and more efficient -- than the row-by-row design I described above).
-Gary-
- 03-19-2010, 02:53 AM #17
Member
- Join Date
- Mar 2010
- Posts
- 17
- Rep Power
- 0
Thanks guys its helping a lot. I am down to the last stretch and am getting unknown errors and am missing the last part of the frame. is anyone on now to help?? I have another hour and forty minutes. here is my code with error.
5 errors found:Java Code:import java.awt.*; import java.awt.font.*; import java.awt.geom.*; import java.awt.image.BufferedImage; import java.text.*; import java.util.*; import java.util.List; // resolves problem with java.awt.List and java.util.List /** * A class that represents a picture. This class inherits from * SimplePicture and allows the student to add functionality to * the Picture class. * * Copyright Georgia Institute of Technology 2004-2005 * @author Barbara Ericson ericson@cc.gatech.edu */ public class Picture extends SimplePicture { ///////////////////// constructors ////////////////////////////////// /** * Constructor that takes no arguments */ public Picture () { /* not needed but use it to show students the implicit call to super() * child constructors always call a parent constructor */ super(); } /** * Constructor that takes a file name and creates the picture * @param fileName the name of the file to create the picture from */ public Picture(String fileName) { // let the parent class handle this fileName super(fileName); } /** * Constructor that takes the width and height * @param width the width of the desired picture * @param height the height of the desired picture */ public Picture(int width, int height) { // let the parent class handle this width and height super(width,height); } /** * Constructor that takes a picture and creates a * copy of that picture */ public Picture(Picture copyPicture) { // let the parent class do the copy super(copyPicture); } /** * Constructor that takes a buffered image * @param image the buffered image to use */ public Picture(BufferedImage image) { super(image); } ////////////////////// methods /////////////////////////////////////// public Picture frame(int frameSize, Color frameColor) { { Picture targetPicture = new Picture(this.getWidth() + frameSize *2, this.getHeight() + frameSize *2); Pixel sourcePixel = null; Pixel targetPixel = null; int targetX = 0; int targetY = 0; Pixel[]pixelArray = framepic.getPixels(); for (int sourceX = 0; sourceX < this.getWidth(); sourceX++) { for (int sourceY = 0; sourceY < this.getHeight(); sourceY++) { sourcePixel = this.getPixel (sourceX, sourceY); for (int indexY = 0; indexY < 2; indexY++) { for (int indexX = 0; indexX < 2; indexX++) { targetX = sourceX * 2 + indexX; targetY = sourceY * 2 + indexY; targetPixel = targetPicture.getPixel(targetX,targetY); targetPixel.setColor(sourcePixel.getColor()); } } } } return framePic; } [COLOR="Red"]public Picture mirror()[/COLOR][/COLOR] { int width = this.getWidth(); int mirrorPoint = width / 2; Pixel leftPixel = null; Pixel rightPixel = null; Pixel targetPixel = null; for (int y = 0; y < getHeight(); y++) { for (int x = 0; x < mirrorPoint; x++) { leftPixel = getPixel(x,y); rightPixel = getPixel(width - 1 - x, y); Color tempPixel = rightPixel.getColor(); rightPixel.setColor(leftPixel.getColor()); leftPixel.setColor(tempPixel); } } } return mirrorPic; } } [COLOR="red"]public Picture mirrorFrame(int frameSize, Color frameColor)[/COLOR] { frame(int frameSize, Color frameColor); [COLOR="red"]mirror();[/COLOR] [COLOR="red"] }[/COLOR] // this is the end of class Picture, put all new methods before this
File: C:\Users\Matt\Desktop\bookClasses\Picture.java [line: 117]
Error: C:\Users\Matt\Desktop\bookClasses\Picture.java:117 : illegal start of expression
File: C:\Users\Matt\Desktop\bookClasses\Picture.java [line: 117]
Error: C:\Users\Matt\Desktop\bookClasses\Picture.java:117 : ';' expected
File: C:\Users\Matt\Desktop\bookClasses\Picture.java [line: 141]
Error: C:\Users\Matt\Desktop\bookClasses\Picture.java:141 : class, interface, or enum expected
File: C:\Users\Matt\Desktop\bookClasses\Picture.java [line: 144]
Error: C:\Users\Matt\Desktop\bookClasses\Picture.java:144 : class, interface, or enum expected
File: C:\Users\Matt\Desktop\bookClasses\Picture.java [line: 145]
Error: C:\Users\Matt\Desktop\bookClasses\Picture.java:145 : class, interface, or enum expected
all the error points are in red. anything will help. I cant hand it in unless it atleast compiles i have been doing this for days
- 03-19-2010, 03:40 AM #18
Member
- Join Date
- Mar 2010
- Posts
- 17
- Rep Power
- 0
all i am stuck on now is what the return statement for the frame is
- 03-19-2010, 03:40 AM #19
Senior Member
- Join Date
- Mar 2010
- Posts
- 266
- Rep Power
- 4
Extra curly bracket before "return mirrorPic;"
- 03-19-2010, 03:41 AM #20
Senior Member
- Join Date
- Mar 2010
- Posts
- 266
- Rep Power
- 4
Similar Threads
-
Homework help
By rclausing in forum New To JavaReplies: 26Last Post: 11-24-2009, 06:06 AM -
Please Help with Homework
By theuser in forum Advanced JavaReplies: 2Last Post: 07-30-2009, 03:37 PM -
help with homework
By pinkdiamondgail in forum Advanced JavaReplies: 7Last Post: 04-07-2009, 01:34 AM -
Homework help...
By robrobrob in forum New To JavaReplies: 4Last Post: 10-17-2008, 04:24 AM -
Need help with homework.
By JavaNewbie0000 in forum New To JavaReplies: 2Last Post: 07-31-2008, 03:06 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks