Hi. I'm currently learning Java and I need to do a project that makes shapes and my name appear with the buttons on the GUI. I also need to make them all clear on the final button press. I think I almost have it, but I'm still not quite sure on how to link each shape to the coresponding JButton. I'm a little stumped.
import java.sql.*; import java.awt.geom.*; import java.awt.*; import javax.swing.*; //Layout import java.awt.BorderLayout; import java.awt.GridLayout; import java.awt.FlowLayout; import java.awt.Rectangle; //Forming the objects import javax.swing.JFrame; import java.awt.Font; import javax.swing.JButton; import javax.swing.JTextField; import javax.swing.JTextArea; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.geom.*; //Menu Objects import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JOptionPane; //Event handling import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.border.EtchedBorder; import javax.swing.border.TitledBorder; //Input and Output Objects import java.net.*; import java.io.InputStream; import java.io.IOException; import java.io.OutputStream; import java.io.PrintWriter; import java.util.Scanner; public class RashadMooreFinalProject extends JFrame { private ActionListener listener; private JTextField inputText; private JTextArea outputText; private JButton addCircle; private JButton addSquare; private JButton addTriangle; private JButton addme; private JButton addclearall; Graphics2D g2; private static final int FRAME_WIDTH = 600; private static final int FRAME_HEIGHT = 400; private static final String myName = "Rashad Moore"; static final String defaultURL = "http://www.rashadlavonmoore.com/"; public RashadMooreFinalProject() { //Input box that will be changed. JMenuBar menu = new JMenuBar(); setJMenuBar(menu); menu.add(createFileMenu()); class ChoiceListener implements ActionListener { public void actionPerformed(ActionEvent event) { } } class TextListener implements ActionListener { public void actionPerformed(ActionEvent event) { setNewText(); } } listener = new TextListener(); createControlPanel(); setSize(FRAME_WIDTH, FRAME_HEIGHT); } public static void main(String[] args) { JFrame frame = new RashadMooreFinalProject(); frame.setTitle("Rashad Moore Final"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } public JMenu createFileMenu() { JMenu tMenu = new JMenu("File"); tMenu.add(createExitItem()); return tMenu; } public JMenuItem createExitItem() { JMenuItem mItem = new JMenuItem("Close"); //Create a special listener condition for this entry. class MenuItemListener implements ActionListener { public void actionPerformed(ActionEvent event) { System.exit(0); } } ActionListener listener = new MenuItemListener(); mItem.addActionListener(listener); return mItem; } /** * Creating the HELP and ABOUT functionality. */ public JMenuItem createAboutItem() { JMenuItem mItem = new JMenuItem("About..."); //Make a listener class MenuItemListener implements ActionListener { public void actionPerformed(ActionEvent event) { } } ActionListener listener = new MenuItemListener(); mItem.addActionListener(listener); return mItem; } /** * Create the control panel of interactive objects. */ //Creating the buttons public JPanel createButton() { addCircle = new JButton("Draw Circle"); addCircle.addActionListener(listener); JPanel panel = new JPanel(); panel.add(addCircle); addSquare = new JButton("Draw Square"); addSquare.addActionListener(listener); panel.add(addSquare); addTriangle = new JButton("Draw Triangle"); addTriangle.addActionListener(listener); panel.add(addTriangle); addme = new JButton("Me"); addme.addActionListener(listener); panel.add(addme); addclearall = new JButton("Clear All"); addclearall.addActionListener(listener); panel.add(addclearall); return panel; } public void createControlPanel() { JPanel textButtonPanel = createButton(); JPanel controlPanel = new JPanel(); controlPanel.setLayout(new GridLayout(3,1)); controlPanel.add(textButtonPanel); add(controlPanel, BorderLayout.SOUTH); } public void paint(Graphics g) { super.paint( g ); //Drawing the Circle { g.drawOval(340, 75, 90, 90); } //Drawing the Square g.drawRect(200, 100, 100, 100); //Drawing the triangle g.drawLine(100, 100, 150, 100); g.drawLine(150, 100, 125, 150); g.drawLine(125, 150, 100, 100); //Writing my name Font myFont = new Font("Times New Roman", Font.BOLD | Font.ITALIC ,30); g.setFont(myFont); g.drawString("Rashad Lavon Moore", 55, 90); } //Creating the button public void setNewText() { inputText.setText("Me"); inputText.repaint(); } /** * Handler function when refresh button is clicked. Grabs HTML data from URL. */ public void setURLtext() throws IOException { //Set up the URL information URL url = new URL(inputText.getText()); URLConnection connection = url.openConnection(); //Check that HTTP connection is good HttpURLConnection http = (HttpURLConnection)connection; int response = http.getResponseCode(); if (response != HttpURLConnection.HTTP_OK) { return; } //Input stream InputStream is = connection.getInputStream(); Scanner input = new Scanner(is); outputText.setText(""); while (input.hasNextLine()) { String inLine = input.nextLine(); outputText.append(inLine); outputText.append("\n"); } } { JPanel textButtonPanel = createButton(); JPanel controlPanel = new JPanel(); controlPanel.setLayout(new GridLayout(3,1)); controlPanel.add(textButtonPanel); add(controlPanel, BorderLayout.SOUTH); } }