Page 1 of 2 12 LastLast
Results 1 to 20 of 31
  1. #1
    primalpop's Avatar
    primalpop is offline Member
    Join Date
    Nov 2009
    Posts
    9
    Rep Power
    0

    Default Swing components not working

    I am trying to implement an interface which responds to mouseclick and with buttons Circle, Rectangle and Quit. On pressing Circle a Circle is drawn on the frame and so on. I cant get this code to work. Your help would be much appreciated.

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.event.ActionListener;
    import java.util.EventListener;
    import javax.swing.*;
    
    interface ActionListener extends EventListener{
    	public void actionPerformed(ActionEvent ae);
    }
    
    public class india extends JFrame implements ActionListener {
    	JButton b1, b2, b3;
    	JLabel lbl;
    	String isCircle="N", isRectangle="N";
    	public india(String strname){
    		super(strname);
    		getContentPane().setLayout(new FlowLayout());
    		JPanel p = new JPanel();
    		b1 = new JButton("Rectangle");
    		b2 = new JButton("Circle");
    		b3 = new JButton("Quit");
    		p.setLayout(new FlowLayout());
    		p.add(b1);
    		p.add(b2);
    		p.add(b3);
    		getContentPane().add(p);
    		b1.addActionListener(this);
    		b2.addActionListener(this);
    		b3.addActionListener(this);
    	}
    	public void actionPerformed(ActionEvent ae){
    		String s = ae.getActionCommand();
    		if("Rectangle".equals(s)){
    			isRectangle = "Y";
    			repaint();
    		}
    		if("Circle".equals(s)){
    			isCircle = "Y";
    			repaint();
    			}
    		if("Quit".equals(s)){
    			System.exit(0);
    		}
    	}
    	public void paint(Graphics g){
    		if(isRectangle == "Y"){
    			g.drawRect(10, 10, 50, 50);
    		}
    		if(isCircle == "Y"){
    			g.drawOval(10, 10, 50, 50);
    		}
    	}
    	
    
    	public static void main(String args[]){
    		india t = new india("Paint");
    		t.setSize(300, 300);
    		t.setVisible(true);
    	}
    
    }
    Last edited by primalpop; 11-03-2009 at 01:43 AM.

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    Use the "Code" button, not the "Quote" button when posting code.

    Never override the paint() method of a JFrame.

    Custom paint should be done by overriding the paintComponent() method of JPanel or JComponent. Read the section from the Swing tutorial on Custom Painting for more information.

  3. #3
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    6

    Default

    Does this code compile without error? Does actionPerformed ever get called?
    The reason I ask is that the code defines its own version of ActionListener:
    Java Code:
    interface ActionListener extends EventListener{
        public void actionPerformed(ActionEvent ae);
    }
    This interface is distinct from the one in java.awt.event.
    So it seems to me the addListener lines should get compile errors
    and actionPerformed should never be called.

    BTW: The code uses Strings where boolean values are appropriate.
    But this should not affect functionality.

  4. #4
    arulmozs is offline Member
    Join Date
    Oct 2009
    Posts
    7
    Rep Power
    0

    Default pls check code

    u correct code



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

    /*interface ActionListener extends EventListener {

    public void actionPerformed(ActionEvent ae);
    }*/

    public class india extends JFrame implements ActionListener {

    JButton b1, b2, b3;
    JLabel lbl;
    String isCircle = "N", isRectangle = "N";

    public india(String strname) {
    super(strname);
    getContentPane().setLayout(new FlowLayout());
    JPanel p = new JPanel();
    b1 = new JButton("Rectangle");
    b2 = new JButton("Circle");
    b3 = new JButton("Quit");
    p.setLayout(new FlowLayout());
    p.add(b1);
    p.add(b2);
    p.add(b3);
    getContentPane().add(p);
    b1.addActionListener( this);
    b2.addActionListener(this);
    b3.addActionListener(this);
    }

    public void actionPerformed(ActionEvent ae) {
    String s = ae.getActionCommand();
    if ("Rectangle".equals(s)) {
    isRectangle = "Y";
    repaint();
    }
    if ("Circle".equals(s)) {
    isCircle = "Y";
    repaint();
    }
    if ("Quit".equals(s)) {
    System.exit(0);
    }
    }

    public void paint(Graphics g) {
    if (isRectangle == "Y") {
    g.drawRect(10, 10, 50, 50);
    }
    if (isCircle == "Y") {
    g.drawOval(10, 10, 50, 50);
    }
    }

    public static void main(String args[]) {
    india t = new india("Paint");
    t.setSize(300, 300);
    t.setVisible(true);
    }
    }

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by arulmozs View Post
    u correct code
    Is that supposed to be "correct" code for the original poster? It ignores camickr's recommendations and has too many errors to mention.

    To the original poster, please disregard arulmozs' post. If you still have problems, please post your code (with code tags) and ask any questions you may have. Best of luck.

  6. #6
    primalpop's Avatar
    primalpop is offline Member
    Join Date
    Nov 2009
    Posts
    9
    Rep Power
    0

    Default

    Thank you for all the replies.

    Quote Originally Posted by zweibieren View Post
    Does this code compile without error? Does actionPerformed ever get called?
    The reason I ask is that the code defines its own version of ActionListener:
    Java Code:
    interface ActionListener extends EventListener{
        public void actionPerformed(ActionEvent ae);
    }
    This interface is distinct from the one in java.awt.event.
    So it seems to me the addListener lines should get compile errors
    and actionPerformed should never be called.

    BTW: The code uses Strings where boolean values are appropriate.
    But this should not affect functionality.

    As you mentioned, these lines are not getting compiled. How can I implement ActionListener interface from java.awt.event?

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I would for one delete your interface "class":
    Java Code:
    interface ActionListener extends EventListener{
    	public void actionPerformed(ActionEvent ae);
    }
    get rid of it as it's only messing you up.

  8. #8
    primalpop's Avatar
    primalpop is offline Member
    Join Date
    Nov 2009
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    I would for one delete your interface "class":
    get rid of it as it's only messing you up.
    Thank you. It worked

    Now the problem is when I press Rectangle or Circle nothing gets drawn on the Frame. Heres the edited code

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    
    public class india extends JFrame implements ActionListener  {
    	JButton b1, b2, b3;
    	JLabel lbl;
    	String isCircle="N", isRectangle="N";
    	public india(String strname){
    		super(strname);
    		getContentPane().setLayout(new FlowLayout());
    		JPanel p = new JPanel();
    		b1 = new JButton("Rectangle");
    		b2 = new JButton("Circle");
    		b3 = new JButton("Quit");
    		p.setLayout(new FlowLayout());
    		p.add(b1);
    		p.add(b2);
    		p.add(b3);
    		getContentPane().add(p);
    		b1.addActionListener(this);
    		b2.addActionListener(this);
    		b3.addActionListener(this);
    	}
    	public void actionPerformed(ActionEvent ae){
    		String s = ae.getActionCommand();
    		if("Rectangle".equals(s)){
    			isRectangle = "Y";
    			repaint();
    		}
    		if("Circle".equals(s)){
    			isCircle = "Y";
    			repaint();
    			}
    		if("Quit".equals(s)){
    			System.exit(-1);
    		}
    	}
    	public void paintComponent(Graphics g){ 
    		if(isRectangle == "Y"){
    			 g.setColor(Color.RED);
    			 g.drawRect(10, 10, 50, 70);
    		}
    		if(isCircle == "Y"){
    			 g.setColor(Color.RED);
    			 g.drawOval(10, 10, 30, 30);
    		}
    	}
    	
    
    	public static void main(String args[]){
    		india t = new india("Paint");
    		t.setSize(300, 300);
    		t.setVisible(true);
    	}
    
    }

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You're still trying to draw in a JFrame, and this shouldn't be done (I think that camickr has already stated this). Shoot, JFrame doesn't even have a paintComponent method to override, so your method is simply ignored. Instead, draw in a class that extends JPanel (or other JComponent) and override IT's paintComponent method.

    Also, don't try to compare Strings with ==. Use the equals method.

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Also, as noted above, you're still using Strings (for example, "Y" or "N") where you should be using boolean variables (for example true and false). The suggestion was a good one, and strongly urge you to either make the change or tell us what about the suggestion you don't understand or don't agree with.

  11. #11
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    Wat all changes should I make to implement the JPanel?
    I gave you a link to the Swing tutorial in my first reply.

    You can lead a horse to water but you can't force it to drink!

  12. #12
    primalpop's Avatar
    primalpop is offline Member
    Join Date
    Nov 2009
    Posts
    9
    Rep Power
    0

    Default

    Many things kept me busy last week. Today I tried the java program again.
    The assignment was to create a program such that rectangle class and oval circle inherits from the shape class. And square and circle from rectangle and oval respectively.

    The pgm compiled and showed the frame with buttons and text fields. But when I click any of the buttons it gets stuck altogether. Any help would be much appreciated.

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    
    public class shape extends JFrame implements ActionListener{
    		JButton b1, b2, b3;
    		JTextField t1, t2, t3, t4, t5, t6, t7, t8;
    		Boolean isCircle, isRectangle;
    		int a,b,c,d,e,f,j,h;
    		String tx1, tx2, tx3, tx4, tx5, tx6, tx7, tx8;
    		public shape(){
    			getContentPane().setLayout(new FlowLayout());
    			JPanel p = new JPanel();
    			b1 = new JButton("Rectangle");
    			b2 = new JButton("Circle");
    			b3 = new JButton("Exit");
    			t1 = new JTextField("100", 5);
    			t2 = new JTextField("50", 5);
    			t3 = new JTextField("50", 5);
    			t4 = new JTextField("50", 5);
    			t5 = new JTextField("50", 5);
    			t6 = new JTextField("50", 5);
    			t7 = new JTextField("50", 5);
    			t8 = new JTextField("50", 5);
    			p.setLayout(new FlowLayout());
    			p.add(b1);
    			p.add(t1);
    			p.add(t2);
    			p.add(t3);
    			p.add(t4);
    			p.add(b2);
    			p.add(t5);
    			p.add(t6);
    			p.add(t7);
    			p.add(t8);
    			getContentPane().add(p);
    			b1.addActionListener(this);
    			b2.addActionListener(this);
    			b3.addActionListener(this);
    			t1.addActionListener(this);
    			t2.addActionListener(this);
    		}
    		public void actionPerformed(ActionEvent ae){
    			String s = ae.getActionCommand();
    			String tx1 = t1.getText();
    			String tx2 = t2.getText();
    			String tx3  =t3.getText();
    			String tx4 = t4.getText();
    			String tx5 = t5.getText();
    			String tx6 = t6.getText();
    			String tx7 = t7.getText();
    			String tx8 = t8.getText();
    			a = Integer.parseInt(tx1);
    			b = Integer.parseInt(tx2);
    			c = Integer.parseInt(tx3);
    			d = Integer.parseInt(tx4);
    			e = Integer.parseInt(tx5);
    			f = Integer.parseInt(tx6);
    			j = Integer.parseInt(tx7);
    			h = Integer.parseInt(tx8);
    			if("Rectangle".equals(s)){
    				rectangle r = new rectangle();
    			}
    			else if("Circle".equals(s)){
    				oval o = new oval();
    				}
    			if("Quit".equals(s)){
    				System.exit(-1);
    			}
    		}
    		public static void main(String args[]){
    			JFrame frame = new JFrame("Paint");
    			shape t = new shape();
    			t.setSize(1000, 1000);
    			t.setVisible(true);
    		}
    		
    }
    
    class rectangle extends shape{
    	public rectangle(){
    		if(a==b){
    			square s = new square();
    		}
    		else {
    		repaint();
    		}
    	}
    	public void paint(Graphics g){
    		g.drawRect(c, d, a, b);
    	}
    }
    
    class square extends rectangle{
    	public square(){
    		repaint();
    	}
    	public void paint(Graphics g){
    		g.drawRect(c, d, a, b);
    	}
    }
    
    class oval extends shape{
    	public oval(){
    		if(e == f){
    			circle q = new circle();
    		}
    		else{
    			repaint();
    		}
    	}
    	public void paint(Graphics g){
    		g.drawOval(j, h, e, f);
    		}
    }
    
    class circle extends oval{
    	public circle(){
    		repaint();
    	}
    	public void paint(Graphics g){
    		g.drawOval(j, h, e, f);
    	}
    }

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You are inadvertently causing recursion to occur, to wit you are causing code to be called over and over again without stop.

    Note that when you construct rectangle the rectangle constructor creates a square, but square extends rectangle, so this calls the rectangle constructor again which creates another square which again calls the rectangle constructor again which... ad infinitum until you run out of memory.

    The fix: don't do this.

  14. #14
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Also, where is the JPanel where you are supposed to be doing your custom painting? As recommended by camickr, you really should read the custom painting tutorial. here's a better link: custom painting in swing

  15. #15
    primalpop's Avatar
    primalpop is offline Member
    Join Date
    Nov 2009
    Posts
    9
    Rep Power
    0

    Default

    Thank you.

    So I have to define a function and not use a constructor of the class if I am inheriting I suppose.

    The drawing of figures worked perfectly wen I did this pgm with no inheritance. So is there any need to use JPanel?
    Last edited by primalpop; 11-15-2009 at 03:31 AM.

  16. #16
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    There are other serious issues with your inheritance. For one your main class shape extends JFrame and then you use this as the parent object for all of your other shapes. Why would you want these classes to inherit from JFrame -- it doesn't make any sense to do this. You want Shape (note that class names should be capitalized) to be a simple class that encapsulates a shape's behaviors and do nothing else. Your JFrame should not be tied into this at all.

  17. #17
    primalpop's Avatar
    primalpop is offline Member
    Join Date
    Nov 2009
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    You want Shape (note that class names should be capitalized) to be a simple class that encapsulates a shape's behaviors and do nothing else. Your JFrame should not be tied into this at all.
    I didnt get this.

    I want to write the responses to action events and code for buttons and text fields in shape. So shouldn't I extend JFrame onto base class shape?

  18. #18
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by primalpop View Post
    I didnt get this.

    I want to write the responses to action events and code for buttons and text fields in shape. So shouldn't I extend JFrame onto base class shape?
    No, you really really don't want to do this.

    Your Shape class (again, the first letter of a class should be capitalized) should just have methods that are shared by all objects that subclass Shape. Period.

    Your events, buttons, and all that should go in your GUI class, call it ShapeGUI if you desire.

    You may wish to post your exact assignment instructions to help us better understand your needs and instructor-demanded limitations.

    Much luck!

  19. #19
    aaroncarpet's Avatar
    aaroncarpet is offline Senior Member
    Join Date
    Nov 2009
    Location
    California
    Posts
    147
    Rep Power
    0

    Default

    First of all you need an awt Canvas to paint stuff you should change your y and n to true and false, and for that matter you could check for only the first character if you use a String buffer R C Q. and if you are new to swing you should get rid of the flow layout and use a gridlayout and a combination of panels

    flow layout is the default layout and you don't even have to ask for it
    Last edited by aaroncarpet; 11-15-2009 at 05:47 AM. Reason: other thoughts

  20. #20
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by aaroncarpet View Post
    First of all you need an awt Canvas to paint stuff you should change your y and n to true and false, and for that matter you could check for only the first character if you use a String buffer R C Q. and if you are new to swing you should get rid of the flow layout and use a gridlayout and a combination of panels
    I strongly disagree with this. Stick with Swing here and use a JPanel. Do not revert to Canvas or even worse try to mix AWT components such as Canvas and Swing components such as JFrames. Also, I would think that a BorderLayout would better suit the OP's purposes with the BorderLayout.NORTH JPanel holding the controls (JButtons and JTextFields) and the BorderLayout.CENTER holding the drawing JPanel.

    aaroncarpet, thanks for trying to be helpful, but please be careful with your advice here. If you are not very familiar with Swing graphics when answering a Swing question, let the original poster know this, else they'll believe you blindly.
    Last edited by Fubarable; 11-15-2009 at 05:54 AM.

Page 1 of 2 12 LastLast

Similar Threads

  1. Problem In Swing Components
    By SANDY_INDIA in forum AWT / Swing
    Replies: 1
    Last Post: 07-19-2008, 10:23 PM
  2. Swing Components Placing
    By ne2000 in forum Eclipse
    Replies: 2
    Last Post: 06-23-2008, 08:00 AM
  3. How to print Swing components
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-22-2008, 11:04 PM
  4. Tab order on swing components
    By ashvin@projectdemo.biz in forum AWT / Swing
    Replies: 1
    Last Post: 05-31-2008, 10:06 AM
  5. HTML on Swing Components
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-27-2007, 09:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •