Page 1 of 6 MIS 120 Homework #6 This homework is intended to provide an opportunity to exercise skills in the following: • Swing development • GUI development • Event-driven programming and callbacks...

1 answer below »
Hi I need help on Java HW 6 which is GUI layouts and im not that familiar with layouts. Please help if possible, thanks. I have attached the HW 6 files as well.


Page 1 of 6 MIS 120 Homework #6 This homework is intended to provide an opportunity to exercise skills in the following: • Swing development • GUI development • Event-driven programming and callbacks For this assignment, we are writing a very simple program that implements an extremely simple component, as well as a button and a checkbox. When the mouse clicks on the component, which we call ClickPad, it leaves a dot where the mouse is. Moving the mouse around makes the old dot vanish, and is replaced by the new dot. The button will reset the ClickPad’s display if the checkbox has a check in it. If it does not have a check in it, then a warning message will let the user know that the reset button is not enabled. CLASS: ClickPad This is the class that much of the rest of the PadCore class is written around. It is a component, so it is a subclass of the JComponent class. It is going to have to keep track of some information: two integers (x and y), and a boolean (shouldDisplay). We will also be writing a MouseListener implementation/subclass, named ClickListener. CONSTRUCTOR: The constructor should set x and y to 0, and shouldDisplay to false, and set up our ClickListener. It should also set the preferred size (not maximum, or minimum, or flat-out stating the size. This is important!). You will need to use the (VERY SIMPLE!) Dimension class from AWT to do this. Set it to 250 x 250 pixels. To make this a little easier on yourself you will also need to add in a border, so add in a border with the following line of code: setBorder(BorderFactory.createTitledBorder("ClickPad")); Remember that, since this is a subclass of JComponent, it is both a ClickPad and a JComponent, so when making calls to JComponent you do not have to prefix it with anything. Example: adding the event listener, adding the preferred size. That said, you can prefix it with ‘this’ if you wish. INNER CLASS: ClickListener ClickListener is an inner class that is a subclass of MouseListener. We will only be using the mousePressed() method, which is triggered when the mouse button is pushed down. The remainder of the methods are to be left blank. Do not use the more “obvious” option of mouseClicked(), as it will not be triggered if your mouse moves the tiniest bit as you click the mouse, so it is not very reliable. When the mousePressed() event occurs, we set x and y to the x and y of where the mouse was clicked. We switch shouldDisplay to true, and then we call repaint(). Note that you should only have this line Page 2 of 6 of code be repaint(). If you use ‘this.repaint()’ will tell the compiler to look for a repaint() method on the MouseListener, and not the JComponent! Example: repaint(); METHOD: paintComponent() We are overriding paintComponent() from JComponent. It is a method that is called by the system on every JComponent whenever it needs to be redrawn. paintComponent() should never be called directly by your program Call repaint() instead, which works with Swing internally so that paintComponent() is (eventually) called. paintComponent() may not work properly (or at all) if called directly. paintComponent() does not return any value. Swing does a LOT of work behind the scenes to make paintComponent() work with everything else, and you cannot easily replicate that work without writing thousands of lines of code. paintComponent() takes one parameter, a Graphics object. Graphics objects are better known as graphics context objects. Graphics context objects are used for drawing and take into account a wide variety of factors, letting you draw using similar methods in a wide variety of ways. Most windowing systems have something similar for their own graphical systems. Note that this graphics context class comes from AWT, and not JavaFX, so documentation for JavaFX may be partially or entirely unusable for your purposes if you find it. You do not generate this object – the system generates it when you call paintComponent(). This is one reason you call repaint(), because repaint() makes sure that paintComponent() has a valid Graphics object to work with. paintComponent() will, first, check shouldDisplay. If shouldDisplay is false, then paintComponent() does nothing, and the picture remains blank. If shouldDisplay is true, we proceed to drawing. Use the Graphics object to set it to the drawing color of your choice. Make sure it is dark and easy to see, preferably something like black or blue. Then draw an oval or circle using the methods in the graphics context. Make sure it is wide enough – I recommend about 30 pixels for width and height. It should use the x and y instance variables to calculate where to put it. That should be sufficient to draw the dot on the screen. The graphics context has methods that can do all of these things. METHOD: reset() The reset() method takes no parameters and returns no value. It sets shouldDisplay to false, and x and y to 0. It then calls repaint(). This should clear the ClickPad. This will be called by the main class, PadCore. CLASS: PadCore PadCore is our main class for this program, and it sets up and manages our JFrame. It will need, as instance variables, one ClickPad, one JFrame, two JPanels, one JButton and one JCheckBox. INNER CLASS: ResetAction ResetAction implements ActionListener, and supplies the required method, actionPerformed. It checks the JCheckBox to see if it is checked. If it is checked, it calls the reset() method on the ClickPad Page 3 of 6 object, which should make the current dot (if any) disappear. If not, it uses JOptionPane to make a text box telling the user that they are not allowed to reset the ClickPad. CONSTRUCTOR: The constructor is where all of our setup work happens. Begin by instantiating ClickPad(), which should be very simple. Next, instantiate the button, which we will call resetButton, with the word “Reset” as the text label. Use the ‘new’ keyword to give it an instance of ResetAction for its action listener. Next, instantiate the JCheckBox, which we will call resetCheckBox, and give it the label “Can Reset.” Unlike some of our demo code, don’t bother using setSize() unless you are still developing ClickPad and have no other components. The layout managers should take care of this for you, if you have them. Next, we need to build the two panels. One will be named mainPanel, and the other lowerPanel. Instantiate both. Set lowerPanel to have a GridLayout (NOT GridBagLayout), with 1 row and 2 columns. Then add the following components, in this order: resetButton and resetCheckBox. mainPanel should have a BoxLayout, with a BoxLayout.PAGE_AXIS layout setting. Add the ClickPad object first, then lowerPanel. Now, instantiate the JFrame. Set the default close operation to exit. Set the title to say, “ClickPad Demo.” Run the pack() method on the JFrame. Add the mainPanel to the JFrame (note – do not use any layout managers with JFrame; that should be taken care of by the panels). Then exit the constructor. METHOD: go() go() has a very simple job – set the JFrame to be visible. That’s it. It returns no variables and needs no parameters. By this point the constructor has set up everything so all we have to do is throw the switch and let Swing take over. METHOD: main() main() for this class is like every other main() we have examined or written in this course. It has to do three things: declare a reference to a PadCore object, build it, and run the go() method. If you feel more comfortable with main() being in a separate class, you can put main() into its own class, PadMain, which contains main() and nothing else, and can do the same tasks. ARE YOU OVERWHELMED? If you feel like it, this shouldn’t be a surprise. However, this is not as hard as it seems. YOU CAN DO IT. Most, if not all, of the things in here should be demonstrated in one way or another in the demonstration programs. Most other things should be covered in your book, and is definitely covered Page 4 of 6 in the Swing documentation. You will have to do a little searching to find the answers in the Swing documentation for a few things, but do not be concerned, it’s there. The actual control flow and coding required is essentially presented to you in this prompt step by step. It should be able to take care of most of your questions with that, so if you stick with it, it will be easier. If you have questions, first look in the demo programs. Then look in the slides and the book. Then the JavaDocs. And if all that fails, do NOT hesitate to ask me. Attach your code so we can save some time, even if I don’t ask for it. I might not use it but you won’t have to wait for me to ask you to send it. Although if you ask on Friday at 10:00 PM and it is due 11:59 PM that night, you are probably going to be out of luck. You are provided with code to use as a basis for this program. Be certain to use it, as it will make things much easier on you, and provides a lot of guidance for how to proceed. So you are aware, all of this can be accomplished in significantly fewer than 200 lines of code. More along the lines of 150 lines, or less. If you are writing large amounts of code you are probably doing something wrong. RECOMMENDED DEVELOPMENT STRATEGY: 1. First build a JFrame and JPanel inside of PadCore, with the JPanel going into the JFrame.
Answered 1 days AfterMay 13, 2021

Answer To: Page 1 of 6 MIS 120 Homework #6 This homework is intended to provide an opportunity to exercise...

Shweta answered on May 15 2021
154 Votes
84076Solution/Hw6/.classpath

    
        
            
        
    
    
    
84076Solution/Hw6/.project

     Hw6
    
    
    
    
        
             org.eclipse.jdt.core.javabuilder
            
            
        
    
    
         org.eclipse.jdt.core.javanature
    
84
076Solution/Hw6/.settings/org.eclipse.jdt.core.prefs
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.release=disabled
org.eclipse.jdt.core.compiler.source=1.8
84076Solution/Hw6/bin/ClickPad$ClickMouse.class
synchronized class ClickPad$ClickMouse implements java.awt.event.MouseListener {
private void ClickPad$ClickMouse(ClickPad);
public void mouseClicked(java.awt.event.MouseEvent);
public void mousePressed(java.awt.event.MouseEvent);
public void mouseReleased(java.awt.event.MouseEvent);
public void mouseEntered(java.awt.event.MouseEvent);
public void mouseExited(java.awt.event.MouseEvent);
}
84076Solution/Hw6/bin/ClickPad.class
public synchronized class ClickPad extends javax.swing.JComponent {
public static final int DOT_WIDTH = 30;
public static final int DOT_HEIGHT = 30;
private int x;
private int y;
private boolean shouldDisplay;
public void ClickPad();
public void paintComponent(java.awt.Graphics);
public void reset();
}
84076Solution/Hw6/bin/PadCore$ResetTrigger.class
synchronized class PadCore$ResetTrigger implements java.awt.event.ActionListener {
private void PadCore$ResetTrigger(PadCore);
public void actionPerformed(java.awt.event.ActionEvent);
}
84076Solution/Hw6/bin/PadCore.class
public synchronized class PadCore {
private javax.swing.JPanel mainPanel;
private javax.swing.JPanel lowerPanel;
private ClickPad pad;
private...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here