5 Replies - 12442 Views - Last Post: 05 September 2010 - 10:28 AM Rate Topic: -----

#1 mr. leight  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 03-September 10

Using JButtons to Draw Shapes

Posted 03 September 2010 - 05:51 PM

Hi, I'm new to the forum and I am trying to learn JAVA. I have to do a program that creates shapes by pressing buttons on a GUI. I am pretty frustrated and could use some advice. I have to make the shapes and my name appear according to the buttons on the GUI. I also have to make a button that erases all of them.

import java.awt.*;
import javax.swing.*;
import java.awt.geom.*;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;



import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;



   


public class RashadMooreFinal extends JPanel {

    private ActionListener  listener;
	 private ActionListener  textlistener;
    private JButton         refButton;
	 private JTextField      inputText;
    private JButton         nameButton;
	
	private boolean drawCircle = false;
	private boolean drawSquare = false;
	private boolean drawTriangle = false;
	
	 private static final int    FRAME_WIDTH = 500;
    private static final int    FRAME_HEIGHT = 300;
	

	JButton addTriangle;
	JButton addSquare;
	JButton addCircle;
	JButton addme;
	JButton addClear;
	Rectangle rectangle;
	Graphics2D g2;
	


	
		
JPanel panel = new JPanel();	

      
  
		public void paint(Graphics g)
	{
		super.paint( g );
		
		
		// Drawing the Circle
		
		g.drawOval(340, 75, 90, 90);
		addCircle.addActionListener(new ButtonListener());
  
		
		
		//Drawing the Square
		g.drawRect(200, 100, 100, 100);
		

		
		//Drawing the triangle
	 g.setColor(Color.GREEN);	
    g.drawLine(100, 100, 150, 100);
    g.drawLine(150, 100, 125, 150);
    g.drawLine(125, 150, 100, 100);
	
	 //Writing my name
	 

	 
	 g.setColor(Color.BLACK);
    Font myFont = new Font("Times New Roman", Font.BOLD | Font.ITALIC ,30);
    g.setFont(myFont);
	 g.drawString("Rashad Lavon Moore", 55, 55);
	

		
	}
	

	
	
		public RashadMooreFinal()
	{
	   JFrame frame = new JFrame("Rashad Lavon Moore");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
		
		addCircle = new JButton("Draw Circle");
		addCircle.addActionListener(new ButtonListener());
		
		addSquare = new JButton("Draw Square");
		addSquare.addActionListener(new ButtonListener());
		
		addTriangle = new JButton("Draw Triangle");
		addTriangle.addActionListener(new ButtonListener());
		
		addme = new JButton("Me");
		addme.addActionListener(new ButtonListener());
		
		
		addClear = new JButton("Clear All");
		addClear.addActionListener(new ButtonListener());
		
		panel.setLayout(new FlowLayout());
		panel.add(addTriangle);
		panel.add(addSquare);
		panel.add(addCircle);
		panel.add(addme);
		panel.add(addClear);

		frame.getContentPane().add(panel);
		frame.setVisible(true);
		frame.setLocationRelativeTo(null);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(600, 500);
		
		
	}
	
		class ButtonListener extends JPanel implements ActionListener
		{
			public void actionPerformed(ActionEvent e)
			{
				
				{
					drawCircle = true;
				}
				

				{
					drawSquare = true;
				}
				
				
				{
					
				}
				
			}
		}
		
		public static void main(String[] args)
		{
			RashadMooreFinal RashadMooreFinal = new RashadMooreFinal();
			
			
			
		
		}
		
   

}


If anyone can help me and give me some pointers, I would appreciate it.

This post has been edited by macosxnerd101: 03 September 2010 - 05:53 PM
Reason for edit:: Title renamed to be more descriptive. Please respect rule #4 of this forum.


Is This A Good Question/Topic? 0
  • +

Replies To: Using JButtons to Draw Shapes

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10771
  • View blog
  • Posts: 40,113
  • Joined: 27-December 08

Re: Using JButtons to Draw Shapes

Posted 03 September 2010 - 06:03 PM

This very much deals with the State pattern. Basically, we are identifying the State of the program based on JButton clicks. Some of the states could include drawing your name, drawing a circle, etc. So it might be a good idea to make some constants to represent the various options, and an attribute to represent the state.

Now as a JPanel is responsible for drawing, it makes sense to extend it and have it manage the states internally. So if the NAME JButton is clicked, set the state of the JPanel to NAME. And use such logic. Then after the state is set, invoke repaint().

//I'm assuming your constants and state variable
//will be of type int
public void setState(int state){
    this.state = state;
    repaint();
}



Then in your paint() method, you can simply check the state and paint accordingly.

Also, you may want to separate your JPanels with the JButtons and the one that does the painting. You can use something like BorderLayout to add them to the same JFrame in an organized manner.

Does this help you see the basic logic of what I am talking about?
public class PaintPanel extends JPanel{

   public static final int STATE_CONST_ZERO = 0;

   private int state;

   public void paint(Graphics g){
        super.paint(g);
 
        if(state == STATE_CONST_ZERO){/*code*/}
        else if(state == /*some other const*/){/*code*/}
   }
 
   public void setState(int state){
       this.state = state;
   }

}


Was This Post Helpful? 1
  • +
  • -

#3 mr. leight  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 03-September 10

Re: Using JButtons to Draw Shapes

Posted 03 September 2010 - 06:07 PM

So using states will help me connect the buttons to each shape.....

I'll try it. Thanks for the quick response!
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10771
  • View blog
  • Posts: 40,113
  • Joined: 27-December 08

Re: Using JButtons to Draw Shapes

Posted 03 September 2010 - 06:08 PM

Yes. It is all about identifying which state the panel should be in. :)
Was This Post Helpful? 0
  • +
  • -

#5 mr. leight  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 03-September 10

Re: Using JButtons to Draw Shapes

Posted 05 September 2010 - 09:27 AM

Hello, I am having some problems trying to link my paint/text object to my GUI buttons. Can anyone help out?

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 ButtonListener implements ActionListener
        {
            public void actionPerformed(ActionEvent event)
            {
                setObject1();
            }
				
				{
				    setObject2();
					 }
			   {
				    setObject3();

					 }
					 {
					 setObject4();
					 }
			
				{
				    setObject5();
					 }
        }


        listener = new ButtonListener();

        createControlPanel();
		  setObject1();
		  setObject2();
		  setObject3();
		  setObject4();
		  setObject5();

        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("Clear All");

        //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.
     */
    /**
     * Create the control panel of interactive objects.
	  */
   
  	 
	     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.setColor(Color.RED);
		g.drawOval(340, 75, 90, 90);
		g.fillOval(340, 75, 90, 90);

		

    
		
		//Drawing the Square
		g.setColor(Color.BLUE);
		g.drawRect(200, 100, 100, 100);
		g.fillRect(200, 100, 100, 100);
		
		

		
		//Drawing the triangle
    g.setColor(Color.ORANGE);
    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.setColor(Color.BLACK);
	 g.drawString("Rashad Lavon Moore", 55, 90);
	}
	
	  //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);
        panel.setBorder(new TitledBorder(new EtchedBorder(), "Options"));
        return panel;
    }


	  //Creating the button
	  
	    public void setObject1()
		 
		 {
	      int shape = 0;
			int name = 0;
			
         if (addCircle.isSelected())
        {
		  	
            
        }
		  }
		  
		 public void setObject2()
		 {
		  
		   if (addSquare.isSelected())
			{
			
			}
			}
		 public void setObject3()
		 {
		  
		   if (addTriangle.isSelected())
			{
			
			}
			}
			 public void setObject4()
		 {
		  
		   if (addme.isSelected())
			{
			
			}
			}
			 public void setObject5()
		 {
		  
		   if (addclearall.isSelected())
			{
			
			}
			}



		 
   

    {
        JPanel textButtonPanel  = createButton();

        JPanel controlPanel = new JPanel();
        controlPanel.setLayout(new GridLayout(3,1));

        controlPanel.add(textButtonPanel);
        add(controlPanel, BorderLayout.SOUTH);
    }
	 }

	 
	 

Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10771
  • View blog
  • Posts: 40,113
  • Joined: 27-December 08

Re: Using JButtons to Draw Shapes

Posted 05 September 2010 - 10:28 AM

Moved to Java and merged with your other topic. Note that Java != Javascript.

The first thing I'm going to say is to clean up your code some. For example, in the below segment, you have curly braces all over the place. See the comment I added to note where the method ends.
  public void actionPerformed(ActionEvent event)
091	            {
092	                setObject1();
093	            } //END actionPerformed()
094	                 
095	                {
096	                    setObject2();
097	                     }
098	               {
099	                    setObject3();
100	 
101	                     }
102	                     {
103	                     setObject4();
104	                     }
105	             
106	                {
107	                    setObject5();
108	                     }
109	        }



I'm guessing you want to test which button is pressed. To do so, you can get the source of the ActionEvent and test it:
public void actionPerformed(ActionEvent e){
    if(e.getSource() == button1){
       //code
    }

    else if(e.getSource() == button2){
       //code
    }
}



Remember what I said in my previous post about the JPanel subclass here.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1