Jump to content

^^ paint() & repaint() ?!^^

- - - - -

  • Please log in to reply
4 replies to this topic

#1
<Java>

<Java>

    Newbie

  • Members
  • PipPip
  • 12 posts



Hello guys!
How's you doing?!!

I want some help in Java program that will probply make me crazy soon1:mad:

the question ask us to build a GUI that will make the user choose a shape : either Rectangle or Oval to draw
&
ask him to input the value of the x ,y , hieght & width

It should be like this image:

Attached File  U.png   45.73K   29 downloads


&

This is my code:

/*

 * To change this template, choose Tools | Templates

 * and open the template in the editor.

 */


package newappfortma;


import javax.swing.*;

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;


/**

 *

 * @author Sudent

 */

public class ShapesFrame extends JFrame{

     JButton  buttonDraw= new JButton("Draw");

     JRadioButton rbO= new JRadioButton("Oval",true);

     JRadioButton rbR= new JRadioButton("Rectangle",false);

    private Graphics gr;

    public JTextField textf[] =new JTextField[4];

   private int x,y,hight,width;

 

    public ShapesFrame(String title)

    {

      setSize(300,400);

      setTitle(title);

      ButtonGroup bgroup = new ButtonGroup();

       bgroup.add(rbR);

       bgroup.add(rbO);


      JPanel pan1=new JPanel();

      pan1.setLayout(new FlowLayout());

      pan1.add(rbR);

      pan1.add(rbO);

 

      JPanel pan2= new JPanel();

        for(int i=0;i<4;i++)

      {

       textf[i]=new JTextField(3);

      pan2.add(textf[i]);

     

      }

       Container con = getContentPane();

       con.setLayout(new FlowLayout());

       con.add(pan1);

       con.add(pan2);

       con.add(buttonDraw);

   

       buttonDraw.addActionListener(new MyAction());

    }

    @Override

    public void paint (Graphics s)

    {

        super.paint(s);

      if(rbR.isSelected())

      {

          s.drawRect(x, y, hight, width);

    }else{

         s.drawOval(x, y, hight, width);

         //

    }

    }

  


    public class MyAction implements ActionListener{

  public void actionPerformed(ActionEvent e){

      x=Integer.valueOf(textf[0].getText());

      y=Integer.valueOf(textf[1].getText());

      hight=Integer.valueOf(textf[2].getText());

      width=Integer.valueOf(textf[3].getText());

        repaint();

        }

}

    public static void main(String[]args){

      ShapesFrame sf= new ShapesFrame("Drawing test");

       sf.setVisible(true);


    }

}

  




The problem I think in paint() & repiant()


What shall I do to draw the graphic that might be selected once the "Draw" button is pressed?!!

By the way , I'm using netBeans 6.9

Thanks indeed ^^



Edited by <Java>, 11 December 2011 - 10:54 AM.


#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Did you fix this in your edit yesterday?

3 things to note tho:

1)
private Graphics gr;
This variable is not used.


2)
    if(rbR.isSelected())
    {
       s.drawRect(x, y, hight, width);
     }else{
       s.drawOval(x, y, hight, width);
     }
drawRect and drawOval are (x,y, width, height), so you should swap width & height around.


3)
"hight" is a typo, "height" is correct english :P

#3
<Java>

<Java>

    Newbie

  • Members
  • PipPip
  • 12 posts

Quote

Did you fix this in your edit yesterday?
Did I ?! :P

ok..
yeah ,I did.:closedeyes:

Quote

1)
Code:


private Graphics gr;

This variable is not used.

u r right, my mistake, sorry..

Quote

"hight" is a typo, "height" is correct english :P


:P

I know but writing "hight" is more easier than "height"..:lol:
but
doesn't matter while I have a variable with the same spilling & letter case .


 x=Integer.valueOf(textf[0].getText());

      y=Integer.valueOf(textf[1].getText());

      hight=Integer.valueOf(textf[2].getText());

      width=Integer.valueOf(textf[3].getText());




The code is finally work >>Thanks indeed..

& this is my final code.>>>If u r interesting :rules:


/*

 * To change this template, choose Tools | Templates

 * and open the template in the editor.

 */


package newappfortma;


import javax.swing.*;

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;


/**

 *

 * @author Student

 */

public class ShapesFrame extends JFrame{

     JButton  buttonDraw= new JButton("Draw");

     JRadioButton rbO= new JRadioButton("Oval",true);

     JRadioButton rbR= new JRadioButton("Rectangle",false);

    private Graphics gr;

    public JTextField textf[] =new JTextField[4];

   private int x,y,hight,width;

 

    public ShapesFrame(String title)

    {

      setSize(300,400);

      setTitle(title);

      ButtonGroup bgroup = new ButtonGroup();

       bgroup.add(rbR);

       bgroup.add(rbO);


      JPanel pan1=new JPanel();

      pan1.setLayout(new FlowLayout());

      pan1.add(rbR);

      pan1.add(rbO);

 

      JPanel pan2= new JPanel();

        for(int i=0;i<4;i++)

      {

       textf[i]=new JTextField(3);

      pan2.add(textf[i]);

     

      }

       Container con = getContentPane();

       con.setLayout(new FlowLayout());

       con.add(pan1);

       con.add(pan2);

       con.add(buttonDraw);

       buttonDraw.addActionListener(new MyAction());

 

    }

    @Override

    public void paint (Graphics s)

    {

        super.paint(s);

        if(rbR.isSelected()==true)

      {

          s.drawRect(x, y, hight, width);

    }else{

         s.drawOval(x, y, hight, width);

        

    }

      x=Integer.valueOf(textf[0].getText());

      y=Integer.valueOf(textf[1].getText());

      hight=Integer.valueOf(textf[2].getText());

      width=Integer.valueOf(textf[3].getText());

    }

  


    public class MyAction implements ActionListener{

  public void actionPerformed(ActionEvent e){

   

      repaint();

        }

}

    public static void main(String[]args){

      ShapesFrame sf= new ShapesFrame("Drawing test");

       sf.setVisible(true);//You have to enlrage the window to see the result


    }

}

  




Just enlarge the window to see the result :)

Thank you ^^



#4
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
You still have
s.drawRect(x, y, hight, width);
And
s.drawOval(x, y, hight, width);

Instead of
s.drawRect(x, y, width, hight);
and
s.drawOval(x, y, width, hight);

Because you do
hight = Integer.valueOf(textf[2].getText());
width = Integer.valueOf(textf[3].getText());
You expect the height in textbox 2, and the width in textbox 3. But you're drawing it the other way around.

----------------------------------------

Also, upon starting the program you get an error because of:
x = Integer.valueOf(textf[0].getText());
y = Integer.valueOf(textf[1].getText());
hight = Integer.valueOf(textf[2].getText());
width = Integer.valueOf(textf[3].getText());
Because at the beginning your text boxes contain no text, and Integer.valueOf() will fail.

Solution is to either put those 4 lines back in the actionPerformed

OR put a number in the textboxes from the start so Integer.valueOf does not fail :
new JTextField("0", 3);


#5
<Java>

<Java>

    Newbie

  • Members
  • PipPip
  • 12 posts

Rogor ....
;)

Thanks indeed






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users