Question
import java.util.*; import java.awt.*; public class CoordinatesMain{ public static void main(String[] args){ DisplayWindow d = new DisplayWindow(Coordinates); PositionPanel p = new PositionPanel(Color.red); //TODO 1 //Remove
import java.util.*; import java.awt.*;
public class CoordinatesMain{
public static void main(String[] args){ DisplayWindow d = new DisplayWindow("Coordinates"); PositionPanel p = new PositionPanel(Color.red); //TODO 1 //Remove Scanner statements Scanner s = new Scanner(System.in); System.out.println("Enter an X coodinate:"); int x = s.nextInt(); System.out.println("Enter a Y coodinate:"); int y = s.nextInt(); p.setPt(x,y); d.addPanel(p); d.showFrame(); } }
import java.awt.*; import javax.swing.*;
//TODO 4a //1. Import java.awt.event.*; //2. Make PositionPanel the ActionListener
public class PositionPanel extends JPanel{
private int xPos, yPos; private Color c; //TODO 2 Add buttons, labels, and text fields public PositionPanel(Color c){ setPreferredSize(new Dimension(500,500)); this.c = c; //TODO 3 Add components to panel //TODO 5 Connect buttons to action listener } public void setPt(int a, int b){ xPos =a; yPos = b; }
public void paintComponent(Graphics g){ super.paintComponent(g); //clears surface g.setColor(Color.red); g.fillOval(xPos,yPos,6,6); g.setColor(Color.black); String s = "("+xPos+","+yPos+")"; g.drawString(s,xPos,yPos); } //ToDo 6 complete the actionPerformed method /** public void actionPerformed(ActionEvent e){
} */ }//end class
import java.awt.*; import javax.swing.*;
public class DisplayWindow extends JFrame{
private Container c;
public DisplayWindow(String frameTitle){
super(frameTitle);
c = this.getContentPane(); }
public void addPanel(JPanel p){ c.add(p); }
public void showFrame(){ this.pack(); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }
}
Part 2.1 Walk through an existing application that plots points in a 2D space.
In this lab, we will modify an existing application that plots points in a 2D space. The program gets two integer coordinates from the console and displays them on a grey background.
Open the CoordinatesMain java file and compile it. Run the code. You will see prompts for X and Y coodinates. Once you enter these, for example, 200 and 200,
you will see the GUI appear with a point at those coordinates.
Walk through of the code:
The CoordinatesMain class creates a DisplayWindow instance, then a PositionPanel instance. These are the main GUI elements of this application.
Then, a Scanner is used to get an X and Y coordinates from the user, which are then set into the panel, which is then added to the window.
Finally, the windows showFrame method is called. The basic classes used to produce this GUI are JFrame and JPanel. The JFrame extends a java.awt.Frame, which is required for all GUIs.
Part 2.1 Modify an existing application that plots points in a 2D space.
We will modify the code so that the user can input the coordinates directly to the GUI as in the figure below.
//TODO 1 Remove the Scanner related statements
Remove the code from CoordinatesMain that has to do with getting the coordinates from the user using the Scanner.
Hint: Keep only the following code:
DisplayWindow d = new DisplayWindow("Coordinates"); PositionPanel p = new PositionPanel(Color.red);
.. .
d.addPanel(p);
d.showFrame();
Compile the class.
//TODO 2 Add components to the PositionPanel
Open the PositionPanel java file. Now, add the following components as variables to
PositionPanel:
Two JButtons- a Quit button and an "Update Coordinate" button, named quitBtn, updateBtn.
Two JLabels- "horizontal position" and vertical position", named xPosLbl, yPosLbl,
two JTextField objects of width 5 named xPosTxt, yPosTxt.
Hint: Use the new constructor to create the button, label, and text field objects.
See how we add the three-letter postfix (Btn, Lbl, Txt) to the variable names that communicates what type of component they are. Proper naming is important as GUIs typically have many component
//TODO 3 Add components to panel
Next, in the PositionPanel constructor, write statements that add all of the above components (button, label, and text field objects) to the panel.
Hint: Call the this.add method.
Compile the class.
Run the program from the CoordinatesMain class. You should see the GUI pop up with all of your components, and notice the point is displayed at 0,0- the upper left corner of the panel. Also, note that the buttons can be clicked, but they do nothing.
//TODO 4 Make PositionPanel the ActionListener
We will now make the PositionPanel a listener for GUI component events. Add the following to the
PositionPanel:
Import java.awt.event.*;
The implements ActionListener words after the extends JPanel words.
//TODO 5 Connect the buttons to the ActionListener i.e. the PositionPanel
In the PositionPanels constructor, call both of the buttons addActionListener methods and pass in a reference to the panel, which is the keyword this.
Hint: The buttons are the Quit button and the Update Coordinate button.
These calls give the buttons a reference to the panel, so that when they are clicked they can call the panels actionPerformed method.
//TODO 6 Complete the actionPerformed method
In PositionPanel remove the comment /** */ for the actionPerformed method
Lastly, we will write the code that makes the program respond to the button click events.
Write an if-else statement in the actionPerformed method:
In the if part, call the getSource method on the ActionEvent e and compare that call to the quit button. The body of the call has System.exit(0); which kills the program.
In the else part check if the source is the update button. In the body of the you need to get the text from the text field objects, parse them as ints and assign them to the member variables for xPos and yPos.
Hint: Use the getText method and Integer class parseInt method to convert the String from the text fields into ints.
After the assignments have been made, make the call to repaint(). This will call the RepaintManager, a class we do not see, and eventually the PositionPanels paintComponent method will be called.
Compile and test the code from the CoordinatesMain class and enter the coordinates as in the figure below.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started