Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please help me with application that uses a GUI (graphical user interface) to allow the user to input a path on an image, save paths

Please help me with application that uses a GUI (graphical user interface) to allow the user to input a path on an image, save paths to a file, and load paths from a file.

This is what it should look like:

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed
Requirements - Path File Format The Path files will start with an integer that specifies how many points, n, there are in the path. The next n pairs of integers specify the x and y coordinates of a point in the path (in order first to last). Values are space separated, but there is no punctuation or other text in the file. Here is an example: 4 0 150 100 100 150 125 200 170 This would be a very short path with four points (three segments): . One line segment from (0, 150) to (100, 100) . One line segment from (100, 100) to (150, 125) . One line segment from (150, 125) to (200, 170)Requirements - Path Class The Path class must be in a package named path. (Note the lowercase package name.) The autograder will test the following public functions in your Path class: (Put contracts above each function.) 0 A default constructor that builds an empty Path (no points). 0 A constructor that takes a Scanner as a parameter. This constructor assumes the Scanner is already open and scanning a path le, and scans in the count and the points to build a path. (This loads the path into the object.) Note: It takes a Scanner. For testing/debugging, you can send in different Scanenrs (that scan from the console if you want to type the count and points) or a String (if you want your test a specic String of text). My tests might do something similar. (That's the advantage of taking a Scanner instead of a lename.) 0 An "int getPointCountO" function that returns the number of points in the path. 0 An \"int getX(int n)" function that returns the n'I x coordinate in the path, with n starting at 0 for the rst x coordinate. (This makes it easy to use ArrayLists.) 0 An "int getY(int n)" function that returns the nth y coordinate in the path, with n starting at O for the rst y coordinate. 0 A "void add(int x, int y) function that adds the specied (x, y) coordinate to the end of the path. 0 A \"toStringi) function" that returns a String representation of the path in the file format above. The String should have a count, then the x and y coordinates (just like shown above). Build a string in a local variable, add to it as needed, then return it. Note: The numbers are required to be space-separated, but line breaks are not important. They do aid readability and if you'd like them in your string, you may add them. Use "\ " for line breaks (and they count as spaces for space-separated text). The above list is the required functions. I strongly recommend writing a public draw function "void draw (Graphics g)" that draws the path to the graphics object (using line segments). It will aid you in creating your solution. (Remember, there is a step-by-step guide below.) 1. Create the Path class (The package is path, the classname is Path.) Add a field to hold the points. An ArrayList of Point objects, The type is ArrayList. (Use java.util.ArrayList and java.awt.Point.) Point objects represent simple (x,y) points and allow you public access to their x and y fields. The ArrayList has useful functions for this project: add, get, size. Put in stubs for the functions specified above. Write the functions specified above. Even the scanning Constructor is fairly simple as long as you're careful about the scanning. (You can use .nextlnt() twice in the body of the loop, but never do a .nextlnt() in the loop condition.) Remember: Both constructors will need to create a new ArrayList() object. The second constructor then adds points to it. Once your Path class works, move on. 2. Create the Application class, get the GUI thread working for you. Create a new class path.PathEditor with a main method. (The package is path, the class name is PathEditor.) Implement the Runnable interface. Add "implements Runnable" to the class header. Add a public void run() instance method. Put a statement in the run method that prints something to the screen. This line of code asks the GUI thread to execute the run method, later. Test it. If it's working, you'll see your message printed from the run method. This means that the GUI thread is successfully executing your code. The run method is where you'll build your GUI. Note: Your run method runs inside of a new PathEditor (that you created in main). This run method is where initialization of your PathEditor object should take place. (Do not create a constructor for the PathEditor class we need the GUI thread to execute all GUI code.) 3. Create and display a JFrame. In the run method you'll need to create a frame, set itjs close policy, pack it, and show it. This is only four statements. Test it. Your frame should appear, but it will be very tiny. 4. Add a JPanel to the content area of your JFrame. A JPanel is where our drawing and mouse events will occur. Right now your PathEditor application has a static main method, and an instance run method. Change your class header and make your PathEditor extend JPanel. Great! 'this' is now a JPanel (in addition to being our application class). In the run method, before you 'pack' the JFrame, set the size of 'this' JPanel. You should set the minimum and preferred sizes. Use method calls like this: this.setMinimumSize(new Dimension(600, 600)); (There are examples of this method call in the LifeSimulation constructor.) Next, 'this' JPanel needs to be put in the JFrame. Finally, you'll want a 'paint' method. Add one, and draw something very, very simple. Test it. If it works, your JFrame should now be larger (because you set 'this' JPanel's size and added it to the content area) and your simple drawing should appear (because you added a paint method). If your drawing doesn't appear, you might not be correctly overriding JPanel's paint method. Double-check the spelling, visibility, and types of your paint method and put a printIn in it for testing. |5. Listen to mouse events Make this class implement MouseListener. The 'implements' part of the class header can implement multiple interfaces. Use a comma separated list. Add the MouseListener methods. (Eclipse can help with this ~ hover over the error in your class header and add unimplemented methods.) There should be five of them. Put a println statement in the mouseReleased method. I recommend printing the coordinates of the event e. (Use e.getX() and e.getY().) In the run method, listen to our own mouse events: this.addMouseListener(this); Test it. Mouse releases should cause your println to print out something. 6. Add a menu bar In your run method before you pack the frame, create the menu. Create a JMenuBar, JMenu, and JMenu item objects. See the example from Wednesday in Week #9 for my code for this. This should only be a few lines of code. Add the JMenultems to your JMenu, add the JMenu to the JMenuBar, and set the frames JMenuBar to your JMenuBar. Note: My JMenuBar and JMenu are held in local variables. I keep my JMenultems in fields so I can use their references later. Test it. Your JMenuBar should appear and you should be able to select each JMenultem. They don't work yet because our PathEditor is not yet listening to the JMenultems.7. Listen to the JMenultems Adjust your class so that it also implements the ActionListener interface. Add the needed method. Put a println statement in your actionPerformed method for testing. Listen to each JMenultem. (See the example for an example of how to do this ~~ it's very near where I create the JMenultems.) Test it. You should see your grintln message when you activate the menu items. Next, Add conditionals to take different actions for each menu item. For now, this can be as simple as printing messages. Work on this part until each menu item prints a different message. 8. Load an draw a background image Locate an image (jpg, gif, or png) that has a path on it, preferably about 600x600. Put it in the top level of your project (like we did for text files). Create a field to hold the loaded image: Bufferedlmage backdrop; In your run method, load the image. You can use code like this: backdrop = javaximageio.lmagelO.read(new File("path_1.png")); } catch (lOException e) { ll Do something sensible here. In your paint method, draw the image rst. (It will cover any previous drawing, so it needs to be first.) Use: g.drawlmage(backdrop, 0, 0, null); [Test it. The image should appear. (Debugging: Make sure the image is non~null or it did not load and cannot draw. Make sure you're not covering the backdrop with another filled shape. Check the lename.) 9. Finish the application Almost all the parts are in place, you just need to connect them. You'll need a field to hold a Path object. You'll need to create a Path object (in some sensible location for creating things once). You'll need to make your paint method draw the path. You'll need to add code for adding points to the Path and repainting (in some sensible location for this). Don't forget to repaint() every time you change the path (add, clear, load, etc.). OthenNise, changes won't show up on screen. You'll need to add code for loading and saving paths. the FileChooser steps on your own. You already have code in the Path class that simplifies this work. Note: I chose to use the .txt file extension for my paths to make them easy to edit. An example path is here: path_2.txt \f

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

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions