8 Replies - 354 Views - Last Post: 31 August 2012 - 11:19 AM Rate Topic: -----

#1 hajra_ch  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 31-August 12

unable to create rectangle when button pressed

Posted 31 August 2012 - 08:20 AM

This is the FrameDEmo class all i want it the rectangle to be created on the canvas when i click on the button REctangle.

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


public class FrameDemo  extends JFrame
{
    private JPanel buttonPanel;
    private JButton textBut;
    private JButton rectangleBut;
    private JButton deleteBut;
    private String s;
    
    private JPanel textPanel;
    private JTextArea textArea;
    private JPanel centerPanel;
    private  int width = 1280;
    private int height = 750;
    private JLabel label;
    private MyCanvas canvas;
    private drawRectangle rec;
    
    public FrameDemo(String title)
    {
        setSize(width,height);
        setTitle(title);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //left hand panel
        buttonPanel = new JPanel();
        buttonPanel.setLayout(new GridLayout(8,2));
        textBut = new JButton("Text");
        rectangleBut = new JButton("Rectangle");
        deleteBut = new JButton("Delete");
        
        buttonPanel.add(textBut);
        buttonPanel.add(rectangleBut);
        buttonPanel.add(deleteBut);
        
        //for canvas thats the center panel
        centerPanel = new JPanel();
        centerPanel.setLayout(new BorderLayout());
        canvas = new MyCanvas();
        centerPanel.add(canvas);
        label = new JLabel("");
        canvas.add(label);
        rec = new drawRectangle();
        
        //right panel
        textPanel = new JPanel();
        textPanel.setLayout(new BorderLayout());
  
        textArea = new JTextArea("Text Area",10,25);
        textPanel.add(textArea);
        
        
        //instance of ButtonWatcher and register this object listener with the button
        textBut.addActionListener(new ButtonWatcher());
        rectangleBut.addActionListener(new ButtonWatcher());
        deleteBut.addActionListener(new ButtonWatcher());
        
        
       //container 
        Container cp = getContentPane();
        cp.add(buttonPanel,BorderLayout.WEST);
        cp.add(centerPanel,BorderLayout.CENTER);
        cp.add(textPanel,BorderLayout.EAST);
        
    }
    
    
     
       
    //Define the listener class by implementng...
    
        private class ButtonWatcher implements ActionListener
        {
         public void actionPerformed(ActionEvent a)
         {
             Object buttonPressed = a.getSource();
             if(buttonPressed.equals(textBut))
             {
                 s=textArea.getText();
                 label.setText(s);
                 repaint();
                 
                 
                   
             }
             if(buttonPressed.equals(rectangleBut))
             {
              
                
                rec.drawRectangle(50, 50, 100, 150);
                canvas.add(rec);
                repaint();
                
                
                
             }
             if(buttonPressed.equals(deleteBut))
             {
             }
             
         }   
        }
        
   
    
}


REctangle class
import java.awt.*;
import javax.swing.*;
public class drawRectangle extends JPanel
    {
    private int x1,y1,x2,y2;
    
    public void drawRectangle(int x, int y, int width, int height)
    {
        
        x1=x;
        y1=y;
        x2 =width;
        y2=height;
        
    }
    
    public void paint(Graphics g)
    {
        
       //super.paint(g);
        g.drawRect(x1,y1,x2,y2);
       
       
    
        
        
        
    }


canvas class
mport java.awt.*;
import javax.swing.*;

public class MyCanvas extends JPanel
{
    
    public void paintComponent(Graphics g)
    {
        int width = getWidth();
        int height = getHeight();
        g.drawString( "This is a Canvas",(width/2),(height/2) );
        
       
       
        
        
        
    }
}



test class
public class FrameDemoTest {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        
        FrameDemo fd=new FrameDemo("title");
        fd.setVisible(true);
        
    }
}


Is This A Good Question/Topic? 0
  • +

Replies To: unable to create rectangle when button pressed

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2194
  • View blog
  • Posts: 5,222
  • Joined: 10-September 10

Re: unable to create rectangle when button pressed

Posted 31 August 2012 - 08:52 AM

You made this about as complicated as you could, but that's okay. Everyone comes to understand this rather complicated topic from different directions.

Why don't you just paint the rectangle on the canvas JPanel? That would be the more typical approach.
Was This Post Helpful? 0
  • +
  • -

#3 hajra_ch  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 31-August 12

Re: unable to create rectangle when button pressed

Posted 31 August 2012 - 08:56 AM

how do you do that?
Was This Post Helpful? 0
  • +
  • -

#4 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2194
  • View blog
  • Posts: 5,222
  • Joined: 10-September 10

Re: unable to create rectangle when button pressed

Posted 31 August 2012 - 09:12 AM

Without rewriting the whole thing for you, here are a few changes to get you moving in the right direction:

Modify the Canvas class to fill a rectangle or not depending on the value of the variable fillRect:
    class MyCanvas extends JPanel
    {
        public void paintComponent(Graphics g)
        {
            super.paintComponent( g );

            int width = getWidth();
            int height = getHeight();
            g.drawString( "This is a Canvas",(width/2),(height/2) );

            if ( drawRect )
            {
                g.fillRect( 50, 50, 100, 100 );
            }
        }
    }

Then modify the ButtonWatcher to set the variable appropriately. I took the liberty of using the delete button to remove the rectangle, but you may have had a different use for it in mind:
    private class ButtonWatcher implements ActionListener
    {
        public void actionPerformed(ActionEvent a)
        {
            Object buttonPressed = a.getSource();
            if(buttonPressed.equals(textBut))
            {
                s=textArea.getText();
                label.setText(s);
                repaint();
            }
            if(buttonPressed.equals(rectangleBut))
            {
                drawRect = true;
                canvas.repaint();
            }
            if(buttonPressed.equals(deleteBut))
            {
                drawRect = false;
                canvas.repaint();
            }
        }   
    }

You'll need to create the fillRect drawRect variable. Even after these changes, what you'll have could be further simplified. For starters, the drawRectangle class will be completely redundant.

This post has been edited by GregBrannon: 31 August 2012 - 09:33 AM

Was This Post Helpful? 0
  • +
  • -

#5 hajra_ch  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 31-August 12

Re: unable to create rectangle when button pressed

Posted 31 August 2012 - 09:28 AM

the fact that u added the drawRect..... am i eman to add
private boolean drawRect; 
in the myvanvas class or the other 1?
Was This Post Helpful? 0
  • +
  • -

#6 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2194
  • View blog
  • Posts: 5,222
  • Joined: 10-September 10

Re: unable to create rectangle when button pressed

Posted 31 August 2012 - 09:45 AM

Ahhh, yes. drawRect is the boolean variable I meant you'd have to create. I updated my last post. I had fillRect() on the brain because I was thinking you may want to use that method to draw a solid box rather than the outline you'll get with drawRect().

As for where to put the boolean variable drawRect, create it wherever you like with the necessary access modifier it needs to be visible to the program parts that need it.

This would be a good time to learn about Java packages.
Was This Post Helpful? 0
  • +
  • -

#7 hajra_ch  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 31-August 12

Re: unable to create rectangle when button pressed

Posted 31 August 2012 - 10:14 AM

im unable to do it i wrote in the FrameDemo class
public boolean getdrawRect()
    {
        return drawRect;
    }
    
    public void  setdrawRect(boolean s)
    {
        drawRect = s;
    }



if(buttonPressed.equals(rectangleBut))
             {
              
                 setdrawRect(true);
                 canvas.repaint();
              
             }


i dont understand what to do in the canvas class now how do i call this getter method

im unable to do it i wrote in the FrameDemo class
public boolean getdrawRect()
    {
        return drawRect;
    }
    
    public void  setdrawRect(boolean s)
    {
        drawRect = s;
    }



if(buttonPressed.equals(rectangleBut))
             {
              
                 setdrawRect(true);
                 canvas.repaint();
              
             }


i dont understand what to do in the canvas class now how do i call this getter method
Was This Post Helpful? 0
  • +
  • -

#8 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2194
  • View blog
  • Posts: 5,222
  • Joined: 10-September 10

Re: unable to create rectangle when button pressed

Posted 31 August 2012 - 10:32 AM

I've adjusted for your decision to go the getter/setter route, and that's a perfectly fine route to pick. It's getting a bit complicated to push you in the right direction, so I just posted a possible solution that works below. Let me know if you have any questions:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;


public class FrameDemo  extends JFrame
{
    private JPanel buttonPanel;
    private JButton textBut;
    private JButton rectangleBut;
    private JButton deleteBut;
    private String s;

    private JPanel textPanel;
    private JTextArea textArea;
    private JPanel centerPanel;
    private  int width = 1280;
    private int height = 750;
    private JLabel label;
    private MyCanvas canvas;
    private boolean drawRect;
    
    public FrameDemo(String title)
    {
        drawRect = false;
        setSize(width,height);
        setTitle(title);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //left hand panel
        buttonPanel = new JPanel();
        buttonPanel.setLayout(new GridLayout(8,2));
        textBut = new JButton("Text");
        rectangleBut = new JButton("Rectangle");
        deleteBut = new JButton("Delete");

        buttonPanel.add(textBut);
        buttonPanel.add(rectangleBut);
        buttonPanel.add(deleteBut);

        //for canvas thats the center panel
        centerPanel = new JPanel();
        centerPanel.setLayout(new BorderLayout());
        canvas = new MyCanvas( this );
        centerPanel.add(canvas);
        label = new JLabel("");
        canvas.add(label);

        //right panel
        textPanel = new JPanel();
        textPanel.setLayout(new BorderLayout());

        textArea = new JTextArea("Text Area",10,25);
        textPanel.add(textArea);


        //instance of ButtonWatcher and register this object listener with the button
        textBut.addActionListener(new ButtonWatcher());
        rectangleBut.addActionListener(new ButtonWatcher());
        deleteBut.addActionListener(new ButtonWatcher());


        //container 
        Container cp = getContentPane();
        cp.add(buttonPanel,BorderLayout.WEST);
        cp.add(centerPanel,BorderLayout.CENTER);
        cp.add(textPanel,BorderLayout.EAST);
    }
    
    public boolean getDrawRect()
    {
        return drawRect;
    }
    
    public void setDrawRect( boolean drawRect )
    {
        this.drawRect = drawRect;
    }

    //Define the listener class by implementng...

    private class ButtonWatcher implements ActionListener
    {
        public void actionPerformed(ActionEvent a)
        {
            Object buttonPressed = a.getSource();
            if(buttonPressed.equals(textBut))
            {
                s=textArea.getText();
                label.setText(s);
                repaint();
            }
            if(buttonPressed.equals(rectangleBut))
            {
                drawRect = true;
                canvas.repaint();
            }
            if(buttonPressed.equals(deleteBut))
            {
                drawRect = false;
                canvas.repaint();
            }
        }   
    }

    public static void main(String[] args) {
        // TODO code application logic here

        FrameDemo fd=new FrameDemo("title");
        fd.setVisible(true);
    }
}

class MyCanvas extends JPanel
{
    FrameDemo fd;
    
    public MyCanvas( FrameDemo fd )
    {
        this.fd = fd;
    }
    
    public void paintComponent(Graphics g)
    {
        super.paintComponent( g );

        int width = getWidth();
        int height = getHeight();
        g.drawString( "This is a Canvas",(width/2),(height/2) );

        if ( fd.getDrawRect() )
        {
            g.fillRect( 50, 50, 100, 100 );
        }
    }
}

I've shown how to add the "magic" to the Canvas class you asked about. Making Canvas aware of the FrameDemo instance allows it to use the FrameDemo class getter for drawRect.

This post has been edited by GregBrannon: 31 August 2012 - 10:36 AM

Was This Post Helpful? 0
  • +
  • -

#9 hajra_ch  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 31-August 12

Re: unable to create rectangle when button pressed

Posted 31 August 2012 - 11:19 AM

thank you so much...finally i can get working on delete button now...hope dont need help in that! thank again
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1