Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Need some help/advice for my GUI code.

  1. #1
    Junior Member
    Join Date
    Sep 2010
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Need some help/advice for my GUI code.

    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);
        }
     
     
     
     
     
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need some help/advice for my GUI code.

    how to link each shape to the coresponding JButton
    Can you explain what you mean by the above?

    By "link" do you mean when you press the button, some code is executed?
    Use listeners for connecting button presses to methods.

  3. #3
    Junior Member
    Join Date
    Sep 2010
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need some help/advice for my GUI code.

    To put it simply, I click a button on the GUI and the corresponding shape appears. I'm using three shapes and my name in my code using Paint and Text.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need some help/advice for my GUI code.

    Several steps required:
    1) recognize the button click and call a method
    2) in the method called, do something that will cause the shape to appear. Call repaint()
    3) in the overridden paint() method draw the shape(s) and text determined by step 2

  5. The Following User Says Thank You to Norm For This Useful Post:

    J-Ark (September 4th, 2010)

Similar Threads

  1. Java Career Advice required
    By saads90 in forum The Cafe
    Replies: 1
    Last Post: April 26th, 2010, 08:59 AM
  2. Advice on Java personal msg system?
    By cyborgbill in forum Java Theory & Questions
    Replies: 1
    Last Post: March 30th, 2010, 03:00 AM
  3. Need some general and specific advice.
    By Morevan in forum Loops & Control Statements
    Replies: 2
    Last Post: January 3rd, 2010, 11:31 PM
  4. Programming advice
    By silverspoon34 in forum Java Theory & Questions
    Replies: 1
    Last Post: November 6th, 2009, 04:19 PM
  5. i need some advice ....
    By mdstrauss in forum Java Theory & Questions
    Replies: 8
    Last Post: July 24th, 2009, 02:29 PM