Page 2 of 3 FirstFirst 123 LastLast
Results 21 to 40 of 53
Like Tree2Likes

Thread: Paint and actionlistener!!!!

  1. #21
    Join Date
    Jan 2012
    Location
    hungary
    Posts
    27
    Rep Power
    0

    Default Re: Paint and actionlistener!!!!

    are you saying that i should pass in all the matrices?? im sorry, the last part was actually the outcome of speculation. do i have to create a new class just for painting the figure?? im really puzzled. i spent 3 days on this single application, its due tomorrow and i feel exhausted.


    also, i dont need to call cool's methods in JTrianglePanel. all i need to do, is somehow pass a paint method into the ActionListener. i still dont have an an explicit understanding on how to fix this problem.


    anyway this is my new code for JTrianglePanel (i havent touched anything in Cool)
    Java Code:
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Polygon;
    import java.awt.*;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.*;
    
    
    
    public class JTrianglePanel extends JPanel{
    	
    	
        
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    	
    	
    	public JTrianglePanel(){
    		Cool c = new Cool();
    		
    		
    		JPanel f = new JPanel();
    		
    		f.setSize(380, 360);
    		f.setVisible(true);
    		f.paint(g);
    		
    		
    		
    		
    		
    		
    		
    		
    	}
    	
    	
    	
    	
    	
    	
    	
    	
    	public void paint(Graphics g){
            
    		super.paint(g);
        	   
    		int [] xcoordinates = { matrix1, matrix2, matrix3};
    		int [] ycoordinates = { matrix4, matrix5, matrix6};
    		Polygon pol = new Polygon(xcoordinates, ycoordinates, 3);
            g.drawPolygon(pol);
    	
    		//matrices are still in red
    	
           }
    	
    	
    	
    	
    	
    	}
    Last edited by rowrowrowyourboat; 01-22-2012 at 07:14 PM.

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

    Default Re: Paint and actionlistener!!!!

    Quote Originally Posted by rowrowrowyourboat View Post
    are you saying that i should pass in all the matrices??
    No I wasn't saying that, but you may need to do that as well. I was saying that if you want one class, the JTrianglePanel, to call another class's methods, the Cool class, then the first class will need a reference to the second class. That's it. But on thinking through, yeah, that's what you really want to do: pass the important data into JTrianglePanel and let it use this data to do its own painting.

    im sorry, the last part was actually the outcome of speculation. do i have to create a new class just for painting the figure?? im really puzzled. i spent 3 days on this single application, its due tomorrow and i feel exhausted.
    I think that you'll need to create one class that extends JPanel and that you draw in, and JTrianglePanel probably should be this class. You will again want it to extend JPanel and will want to draw in its paintComponent override, not its paint override. You can then display this JPanel in a JOptionPane if desired, or create a JDialog to put it in.

    also, i dont need to call cool's methods in JTrianglePanel. all i need to do, is somehow pass a paint method into the ActionListener.
    That's the last thing you want to do. You will not want to call paint directly ever. Better to pass the data into JTrianglePanel and let the JTrianglePanel use that data to paint with.


    I've been going through your code but have found it full of many errors, almost where you may be better off restarting. For instance you appear to be trying to parse JTextFields text to ints in the constructor before the user has had any chance to add text, but not only that, you've got non-numeric text in the JTextField, so the parse will fail, but not only that, you do this within a try/catch block, and completely ignore the catch. This will make your code compilable but completely non-runnable.

    I think you should fix your errors first or restart. You should read the pertinent chapters in the Swing tutorial as well to gain a better understanding of how to do graphics.

  3. #23
    Join Date
    Jan 2012
    Location
    hungary
    Posts
    27
    Rep Power
    0

    Default Re: Paint and actionlistener!!!!

    hmm..earlier on, i replaced the new instance of the parsing call with a string implementation..here's what i did


    Java Code:
    String a = x1.getText();
    num1 = Integer.parseInt(a);
    would the input be parsed in this method?

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

    Default Re: Paint and actionlistener!!!!

    Quote Originally Posted by rowrowrowyourboat View Post
    hmm..earlier on, i replaced the new instance of the parsing call with a string implementation..here's what i did


    Java Code:
    String a = x1.getText();
    num1 = Integer.parseInt(a);
    would the input be parsed in this method?
    The input would be the text held by the appropriate JTextField, but more importantly, when would the above code be called? Certainly not in the constructor before the user has had time to interact with the GUI and add data to the fields. No, it would be called inside of some listener, probably inside of an ActionListener that is triggered by the push of a JButton. You want to extract and analyze the data entered when the user tells the program that he has finished entering data. Usually you give the user a button for him to do this. You want to parse all of your text to numeric in a try/catch pair, but you do not want to ignore the NumberFormatExceptions if they occur, otherwise you're courting disaster.

  5. #25
    Join Date
    Jan 2012
    Location
    hungary
    Posts
    27
    Rep Power
    0

    Default Re: Paint and actionlistener!!!!

    ok so this is my refined code (when i select ok on my gui, instead of a jframe emerging, nothing happens) what class can i call to display JTrianglePanel (since paint, repaint and anything paint is out of the picture)
    Java Code:
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Polygon;
    import java.awt.*;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.*;
    
    
    
    public class JTrianglePanel extends JPanel{
    	
    	
        
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    	Cool c = new Cool();
    	private Graphics g;
    	
    	public JTrianglePanel(){
    		
    		
    		JPanel f = new JPanel();
    		f.setSize(380, 360);
    		f.setVisible(true);
    		
    		f.paintComponents(g);
    		
    		
    		
    		
    		
    		
    		
    	}
    	
    	
    	
    	
    	
    	
    	
    	
    	public void paintComponent(Graphics g){
            
    		super.paintComponent(g);
        	   
    		int [] xcoordinates = { c.matrix1, c.matrix2, c.matrix3};
    		int [] ycoordinates = { c.matrix4, c.matrix5, c.matrix6};
    		Polygon pol = new Polygon(xcoordinates, ycoordinates, 3);
            g.drawPolygon(pol);
    	
    		
    	
           }
    	
    	
    	
    	
    	
    	}

    main class

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.EventQueue;
    import java.awt.Graphics2D;
    import java.awt.Polygon;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.border.EmptyBorder;
    import java.awt.Toolkit;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.ArrayList;
    import java.awt.Graphics;
    import javax.swing.JLabel;
    import javax.swing.SwingConstants;
    import javax.swing.JTextField;
    import javax.swing.GroupLayout;
    import javax.swing.GroupLayout.Alignment;
    import com.jgoodies.forms.factories.DefaultComponentFactory;
    import javax.swing.LayoutStyle.ComponentPlacement;
    import javax.swing.JButton;
    import java.awt.Font;
    import java.awt.Color;
    import javax.swing.border.SoftBevelBorder;
    import javax.swing.border.BevelBorder;
    
    
    public class Cool extends JFrame {
    	
    
    	private JPanel contentPane;
    	private JTextField x1;
    	private JTextField x2;
    	private JTextField x3;
    	private JTextField y1;
    	private JTextField y2;
    	private JTextField y3;
    	
    
    	/**
    	 * Launch the application.
    	 */
    	
    	
    
    	public int num1,num2,num3,num4,num5,num6;
    	public int m1,m2,m3,m4;
    	private JTextField mat1;
    	private JTextField mat3;
    	private JTextField mat2;
    	private JTextField mat4;
    	
    	int matrix1 = num1 * m1 + num2 * m3;
    	int matrix2 = num1 * m2 + num2 * m4;
    	int matrix3 = num3 * m1 + num4 * m3;
    	int matrix4 = num3 * m2 + num4 * m4;
    	int matrix5 = num5 * m1 + num6 * m3;
    	int matrix6 = num5 * m2 + num6 * m4;
    	
    	public static void main(String[] args) {
    		EventQueue.invokeLater(new Runnable() {
    			public void run() {
    				try {
    					Cool frame = new Cool();
    					frame.setVisible(true);
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
    			}
    		});
    	}
    	
    	
    	Graphics g;
    	
    	
    	
    	
    	
    	
    
    	/**
    	 * Create the frame.
    	 */
    	public Cool() {
    		
    		
    		
    		
    		
    		
    		
    		setTitle("GraphingApp 1.0");
    		setIconImage(Toolkit.getDefaultToolkit().getImage(Cool.class.getResource("/com/sun/java/swing/plaf/windows/icons/Computer.gif")));
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setBounds(100, 100, 450, 300);
    		contentPane = new JPanel();
    		contentPane.setBackground(Color.WHITE);
    		contentPane.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null));
    		setContentPane(contentPane);
    		
    		JLabel lblNewJgoodiesLabel = DefaultComponentFactory.getInstance().createLabel("Enter your X coordinates:");
    		lblNewJgoodiesLabel.setFont(new Font("Aharoni", Font.PLAIN, 11));
    		
    		
    		JLabel lblEnterYourY = DefaultComponentFactory.getInstance().createLabel("Enter your Y coordinates:");
    		lblEnterYourY.setFont(new Font("Aharoni", Font.PLAIN, 11));
    		
    		
    		
    		
    		x1 = new JTextField();
    		x1.setText("x1");
    		x1.setColumns(10);
    		
    		
    		
    		
    		
    		x2 = new JTextField();
    		x2.setText("x2");
    		x2.setColumns(10);
    		
    		
    		
    		x3 = new JTextField();
    		x3.setText("x3");
    		x3.setColumns(10);
    		
    		
    		
    		y1 = new JTextField();
    		y1.setText("y1");
    		y1.setColumns(10);
    		
    		
    		
    		y2 = new JTextField();
    		y2.setText("y2");
    		y2.setColumns(10);
    		
    		
    		
    		
    		y3 = new JTextField();
    		y3.setText("y3");
    		y3.setColumns(10);
    		
    		
    		
    		mat1 = new JTextField();
    		mat1.setText("mat1");
    		mat1.setColumns(10);
    		
    
    		
    		
    		
    		mat2 = new JTextField();
    		mat2.setText("mat2");
    		mat2.setColumns(10);
    		
    		
    		
    		
    		
    		
    		
    		mat3 = new JTextField();
    		mat3.setText("mat3");
    		mat3.setColumns(10);
    		
    		
    		
    		
    		mat4 = new JTextField();
    		mat4.setText("mat4");
    		mat4.setColumns(10);
    	
    		
    		
    		
    		
    		
    		
    
    		
    		
    	
    		JButton btnOk = new JButton("OK");
    		btnOk.setBackground(Color.BLACK);
    		btnOk.setForeground(Color.GRAY);
    		btnOk.setFont(new Font("Aharoni", Font.PLAIN, 11));
    		btnOk.addActionListener(new ActionListener(){
    			
    			
    			@SuppressWarnings("deprecation")
    			public void actionPerformed (ActionEvent e){
    				
    				String a = x1.getText();
    				try{
    				num1 = Integer.parseInt(a);
    				}catch(Exception e1){}
    				
    				String b = x2.getText();
    				try{
    				num2 = Integer.parseInt(b);
    			    }catch(Exception e1){}
    				
    				String c = x3.getText();
    				try{
    				num3 = Integer.parseInt(c);
    			    }catch(Exception e1){}
    				
    				String d = y1.getText();
    				try{
    				num4 = Integer.parseInt(d);
    			    }catch(Exception e1){}
    				
    				String e2 = y2.getText();
    				try{
    				num5 = Integer.parseInt(e2);
    			    }catch(Exception e1){}
    				
    				String f = y3.getText();
    				try{
    				num6 = Integer.parseInt(f);
    			    }catch(Exception e1){}
    				
    				
    				String g = mat1.getText();
    				try{
    				m1 = Integer.parseInt(g);
    			    }catch(Exception e1){}
    				
    				String i = mat2.getText();
    				try{
    				m2 = Integer.parseInt(i);
    				}catch(Exception e1){}
    				
    				String h = mat3.getText();
    				try{
    				m3 = Integer.parseInt(h);
    				}catch(Exception e1){}
    				
    				String j = mat4.getText();
    				try{
    				m4 = Integer.parseInt(j);
    				}catch(Exception e1){}
    				
    				
    				
    				
    				JTrianglePanel p = new JTrianglePanel();
    			    p.show(true);
    				
    				
                    
    				
    				
    				
    				
    				
    				
    			
    			
    		}
    		});
    
    		
    		
    		
    		
    	
    		
    	
    	
    		
    		
    	
    		
    		
    		
    		
    		
    		
    		
    		
    
    		
    		
    		
    		
    		
    		
    		
    		GroupLayout gl_contentPane = new GroupLayout(contentPane);
    		gl_contentPane.setHorizontalGroup(
    			gl_contentPane.createParallelGroup(Alignment.LEADING)
    				.addGroup(gl_contentPane.createSequentialGroup()
    					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
    						.addGroup(gl_contentPane.createSequentialGroup()
    							.addGap(67)
    							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
    								.addComponent(x3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
    								.addGroup(gl_contentPane.createSequentialGroup()
    									.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
    										.addComponent(lblNewJgoodiesLabel)
    										.addComponent(x1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
    									.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
    										.addGroup(gl_contentPane.createSequentialGroup()
    											.addGap(73)
    											.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
    												.addComponent(y1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
    												.addComponent(y2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
    												.addComponent(y3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
    										.addGroup(gl_contentPane.createSequentialGroup()
    											.addGap(50)
    											.addComponent(lblEnterYourY))))
    								.addComponent(x2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
    						.addGroup(gl_contentPane.createSequentialGroup()
    							.addGap(185)
    							.addComponent(btnOk))
    						.addGroup(gl_contentPane.createSequentialGroup()
    							.addGap(162)
    							.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
    								.addComponent(mat1, GroupLayout.PREFERRED_SIZE, 37, GroupLayout.PREFERRED_SIZE)
    								.addComponent(mat3, GroupLayout.PREFERRED_SIZE, 37, GroupLayout.PREFERRED_SIZE))
    							.addGap(18)
    							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
    								.addComponent(mat4, GroupLayout.PREFERRED_SIZE, 37, GroupLayout.PREFERRED_SIZE)
    								.addComponent(mat2, GroupLayout.PREFERRED_SIZE, 37, GroupLayout.PREFERRED_SIZE))))
    					.addContainerGap(69, Short.MAX_VALUE))
    		);
    		gl_contentPane.setVerticalGroup(
    			gl_contentPane.createParallelGroup(Alignment.LEADING)
    				.addGroup(gl_contentPane.createSequentialGroup()
    					.addGap(29)
    					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
    						.addComponent(lblNewJgoodiesLabel)
    						.addComponent(lblEnterYourY))
    					.addGap(33)
    					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
    						.addComponent(x1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
    						.addComponent(y1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
    					.addPreferredGap(ComponentPlacement.RELATED)
    					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
    						.addComponent(x2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
    						.addComponent(y2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
    					.addPreferredGap(ComponentPlacement.RELATED)
    					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
    						.addComponent(x3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
    						.addComponent(y3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
    					.addGap(18)
    					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
    						.addComponent(mat2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
    						.addComponent(mat1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
    					.addPreferredGap(ComponentPlacement.RELATED)
    					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
    						.addComponent(mat4, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
    						.addComponent(mat3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
    					.addPreferredGap(ComponentPlacement.RELATED, 17, Short.MAX_VALUE)
    					.addComponent(btnOk)
    					.addContainerGap())
    		);
    		contentPane.setLayout(gl_contentPane);
    	}
    	
    	
    
    	
    }

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

    Default Re: Paint and actionlistener!!!!

    Regarding Cool and its actionPerformed method:

    Java Code:
          btnOk.addActionListener(new ActionListener() {
             @SuppressWarnings("deprecation")
             public void actionPerformed(ActionEvent e) {
                String a = x1.getText();
                try {
                   num1 = Integer.parseInt(a);
                } catch (Exception e1) {
                }
                String b = x2.getText();
                try {
                   num2 = Integer.parseInt(b);
                } catch (Exception e1) {
                }
                String c = x3.getText();
                try {
                   num3 = Integer.parseInt(c);
                } catch (Exception e1) {
                }
                String d = y1.getText();
                try {
                   num4 = Integer.parseInt(d);
                } catch (Exception e1) {
                }
                String e2 = y2.getText();
                try {
                   num5 = Integer.parseInt(e2);
                } catch (Exception e1) {
                }
                String f = y3.getText();
                try {
                   num6 = Integer.parseInt(f);
                } catch (Exception e1) {
                }
                String g = mat1.getText();
                try {
                   m1 = Integer.parseInt(g);
                } catch (Exception e1) {
                }
                String i = mat2.getText();
                try {
                   m2 = Integer.parseInt(i);
                } catch (Exception e1) {
                }
                String h = mat3.getText();
                try {
                   m3 = Integer.parseInt(h);
                } catch (Exception e1) {
                }
                String j = mat4.getText();
                try {
                   m4 = Integer.parseInt(j);
                } catch (Exception e1) {
                }
                JTrianglePanel p = new JTrianglePanel();
                p.show(true);
             }
          });
    It's good that you're parsing the fields in actionPerformed, since it makes sense to extract information from them when the user tells the program to do so, but again, you're completely ignoring all catch blocks.

    I suggest that you
    • use one single try / catch block, that you do all of the parsing in the try block,
    • that you catch for the more specific NumberFormatException, not for the overly general Exception.
    • that you not have an empty try block but instead show a JOptionPane that warns the user that bad information was entered and that you clear all fields in the catch block.
    .

    Your last bit of code in this method is also in error. JTrianglePanel is a JPanel and you can't display a JPanel like that, it needs to be shown in something. The easiest way to display it by far is to simply show in a JOptionPane. i'd use a JOptionPane.showMessage(...) dialog with this JPanel as the method's second parameter, the "Object" parameter.

    More to come....

  7. #27
    Join Date
    Jan 2012
    Location
    hungary
    Posts
    27
    Rep Power
    0

    Default Re: Paint and actionlistener!!!!

    it's asking for a parent component, what should i enter exactly? would null work? (since it doesnt do anything) i apologize for these noobish questions. but i am learning a lot.

    and crap, i just ran the app and all it does was what i had anticipated..it just shows an empty MessageDialog pop box with nothing but an ok button.
    i switched the "p" Object (JTrianglePanel) the parentcomponent and no luck either.
    Last edited by rowrowrowyourboat; 01-22-2012 at 09:14 PM.

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

    Default Re: Paint and actionlistener!!!!

    Regarding JTrianglePanel. Again, please get rid of the unnecessary white space. Again, while this may seem like annoying nit-picking, you are asking for free advice from volunteers, and if you're asking us to put in effort to help you, you should put in the minimal effort so that it's not difficult for us to do this. I've asked you to do this before, but you keep ignoring this request. I will fix it this time, but will not ask again.

    Anyway here is your code with more reasonable amount of white space and with comments that correspond to my notes below:

    Java Code:
    import java.awt.Graphics;
    import java.awt.Polygon;
    import javax.swing.JPanel;
    
    public class JTrianglePanel extends JPanel {
    
       private static final long serialVersionUID = 1L;
       
       Cool c = new Cool(); // (A)
       private Graphics g;  // (B)
    
       // (C)
       public JTrianglePanel() {
          JPanel f = new JPanel(); // (D)
          f.setSize(380, 360); // (E)
          f.setVisible(true);  // (F)
          f.paintComponents(g); // (G)
       }
    
       public void paintComponent(Graphics g) {
          super.paintComponent(g); // good
          
          // (H)
          int[] xcoordinates = { c.matrix1, c.matrix2, c.matrix3 };
          int[] ycoordinates = { c.matrix4, c.matrix5, c.matrix6 };
          Polygon pol = new Polygon(xcoordinates, ycoordinates, 3);
          g.drawPolygon(pol);
       }
    }
    1. You may want your class to have a Cool variable, but if you had this, you shouldn't create a Cool object here. Instead and as I've stated before, you'd have the Cool class which is creating the JTrianglePanel object pass itself into JTrianglePanel via its constructor. But ignore all this because I'm going to recommend that you not have a Cool object at all. Instead I recommend that you have two int array class fields, xcoordinates and ycoordinates.
    2. Never, never give your class a Graphics field like this as you will never use it in this fashion and it will only tempt you to do so. Get rid of this Graphics g variable.
    3. Change JTrianglePanel's constructor to allow it to accept two int[] arrays. Then on the first line of the constructor assign them to the two int array class fields that I told you to create in my recommendation (A) above.
    4. You don't want to create this JPanel, f, inside of JTrianglePanel. The JTrianglePanel class is the JPanel of interest and no other one is needed at this time. In future GUI programs you will often need to create other JPanels like this, but you don't need "f" now, so get rid of it.
    5. Don't set anything's size. Instead set this class's preferredSize by calling this.setPreferredSize(new Dimension(....));.
    6. setVisible(true) is a wasted unnecessary call on a JPanel. Get rid of it.
    7. paintComponents(g) is a wasted, unnecessary and misleading call, so get rid of it. Your class's paintComponent method will be called by the JVM automatically when it is displayed.
    8. Don't create your int arrays in the paintComponent method. Instead use the class fields that I recommended that you create in the first recommendation of this list. Your Polygon will then use the class fields that have been updated by the data passed into this class's constructor parameters.


    Since you'll be changing this class's constructor, you'll have to call the new constructor in the Cool class when you want to create and display this JPanel.

  9. #29
    Join Date
    Jan 2012
    Location
    hungary
    Posts
    27
    Rep Power
    0

    Default Re: Paint and actionlistener!!!!

    im sorry but how many arguments can a constructor's parameters have? obviously all the matrix variables wont fit in there, or am i getting something wrong.
    and you're asking me to get rid of the array's but then how will i induct my elements into the drawpolygon method, it only accepts arrays.

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

    Default Re: Paint and actionlistener!!!!

    Quote Originally Posted by rowrowrowyourboat View Post
    im sorry but how many arguments can a constructor's parameters have?
    as many as reasonably needed.

    ... obviously all the matrix variables wont fit in there, or am i getting something wrong.
    You're getting something wrong. I'm recommending that the constructor have but two parameters, both int[], and both arrays holding 3 ints.

    ..and you're asking me to get rid of the array's but then how will i induct my elements into the drawpolygon method, it only accepts arrays.
    I'm not asking you to get rid of the arrays. I'm asking you to not create the arrays within paintComponent, to make int array class fields, to have your constructor update these arrays, and for your paintComponent to use the arrays.

  11. #31
    Join Date
    Jan 2012
    Location
    hungary
    Posts
    27
    Rep Power
    0

    Default Re: Paint and actionlistener!!!!

    okay, same results, except i entered a larger width and height. so its essentially a bigger box. i tried invoking a paint method in JTrianglePanel to see if it would work but no diversion.
    even with all these change in code, i can get it to work.
    Last edited by rowrowrowyourboat; 01-22-2012 at 10:16 PM.

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

    Default Re: Paint and actionlistener!!!!

    Quote Originally Posted by rowrowrowyourboat View Post
    okay, same results, except i entered a larger width and height. so its essentially a bigger box. i tried invoking a paint method in JTrianglePanel to see if it would work but no diversion.
    even with all these change in code, i can get it to work.
    ???
    I've recommended none of these things.

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

    Default Re: Paint and actionlistener!!!!

    Let's see your code where you try to implement my recommendations.

  14. #34
    Join Date
    Jan 2012
    Location
    hungary
    Posts
    27
    Rep Power
    0

    Default Re: Paint and actionlistener!!!!

    here::better?

    Java Code:
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Polygon;
    import java.awt.*;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.*;
    
    
    
    public class JTrianglePanel extends JPanel{
        
        
        
        /**
         * 
         */
        private static final long serialVersionUID = 1L;
    
        private int matrix1;
        private int matrix2;
        private int matrix3;
        private int matrix4;
        private int matrix5;
        private int matrix6;
        
        
        public JTrianglePanel(int matri1,int matri2, int matri3, int matri4, int matri5, int matri6){
            
            
            
            this.setPreferredSize(new Dimension(300,300));
            
            repaint();
            
            
        }
            
         public void paintComponent(Graphics g){
            
            super.paintComponent(g);
               
            int [] xcoordinates = { matrix1, matrix2, matrix3};
            int [] ycoordinates = { matrix4, matrix5, matrix6};
            Polygon pol = new Polygon(xcoordinates, ycoordinates, 3);
            g.drawPolygon(pol);
        
            
        
           }
        
        
        
        
        
        }



    Java Code:
    import java.awt.BorderLayout;
    import java.awt.EventQueue;
    import java.awt.Graphics2D;
    import java.awt.Polygon;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.border.EmptyBorder;
    import java.awt.Toolkit;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.ArrayList;
    import java.awt.Graphics;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.SwingConstants;
    import javax.swing.JTextField;
    import javax.swing.GroupLayout;
    import javax.swing.GroupLayout.Alignment;
    import com.jgoodies.forms.factories.DefaultComponentFactory;
    import javax.swing.LayoutStyle.ComponentPlacement;
    import javax.swing.JButton;
    import java.awt.Font;
    import java.awt.Color;
    import javax.swing.border.SoftBevelBorder;
    import javax.swing.border.BevelBorder;
    
    
    public class Cool extends JFrame {
        
    
        private JPanel contentPane;
        private JTextField x1;
        private JTextField x2;
        private JTextField x3;
        private JTextField y1;
        private JTextField y2;
        private JTextField y3;
        
    
        /**
         * Launch the application.
         */
        
        
    
        public int num1,num2,num3,num4,num5,num6;
        public int m1,m2,m3,m4;
        private JTextField mat1;
        private JTextField mat3;
        private JTextField mat2;
        private JTextField mat4;
        
        int matrix1 = num1 * m1 + num2 * m3;
        int matrix2 = num1 * m2 + num2 * m4;
        int matrix3 = num3 * m1 + num4 * m3;
        int matrix4 = num3 * m2 + num4 * m4;
        int matrix5 = num5 * m1 + num6 * m3;
        int matrix6 = num5 * m2 + num6 * m4;
        
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    try {
                        Cool frame = new Cool();
                        frame.setVisible(true);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }
        
        
        Graphics g;
        
    /**
         * Create the frame.
         */
        public Cool() {
            
            setTitle("GraphingApp 1.0");
            setIconImage(Toolkit.getDefaultToolkit().getImage(Cool.class.getResource("/com/sun/java/swing/plaf/windows/icons/Computer.gif")));
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setBounds(100, 100, 450, 300);
            contentPane = new JPanel();
            contentPane.setBackground(Color.WHITE);
            contentPane.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null));
            setContentPane(contentPane);
            
            JLabel lblNewJgoodiesLabel = DefaultComponentFactory.getInstance().createLabel("Enter your X coordinates:");
            lblNewJgoodiesLabel.setFont(new Font("Aharoni", Font.PLAIN, 11));
            
            
            JLabel lblEnterYourY = DefaultComponentFactory.getInstance().createLabel("Enter your Y coordinates:");
            lblEnterYourY.setFont(new Font("Aharoni", Font.PLAIN, 11));
            
            
            
            
            x1 = new JTextField();
            x1.setText("x1");
            x1.setColumns(10);
            
            
            
            
            
            x2 = new JTextField();
            x2.setText("x2");
            x2.setColumns(10);
            
            
            
            x3 = new JTextField();
            x3.setText("x3");
            x3.setColumns(10);
            
            
            
            y1 = new JTextField();
            y1.setText("y1");
            y1.setColumns(10);
            
            
            
            y2 = new JTextField();
            y2.setText("y2");
            y2.setColumns(10);
            
            
            
            
            y3 = new JTextField();
            y3.setText("y3");
            y3.setColumns(10);
            
            
            
            mat1 = new JTextField();
            mat1.setText("mat1");
            mat1.setColumns(10);
            
    
            
            
            
            mat2 = new JTextField();
            mat2.setText("mat2");
            mat2.setColumns(10);
            
            
            
            
            
            
            
            mat3 = new JTextField();
            mat3.setText("mat3");
            mat3.setColumns(10);
            
            
            
            
            mat4 = new JTextField();
            mat4.setText("mat4");
            mat4.setColumns(10);
        
            
            
            
            
            
            
    
            
            
        
            JButton btnOk = new JButton("OK");
            btnOk.setBackground(Color.BLACK);
            btnOk.setForeground(Color.GRAY);
            btnOk.setFont(new Font("Aharoni", Font.PLAIN, 11));
            btnOk.addActionListener(new ActionListener(){
                
                
                
                public void actionPerformed (ActionEvent e){
                    
                    String a = x1.getText();
                    try{
                    num1 = Integer.parseInt(a);
                    }catch(Exception e1){}
                    
                    String b = x2.getText();
                    try{
                    num2 = Integer.parseInt(b);
                    }catch(Exception e1){}
                    
                    String c = x3.getText();
                    try{
                    num3 = Integer.parseInt(c);
                    }catch(Exception e1){}
                    
                    String d = y1.getText();
                    try{
                    num4 = Integer.parseInt(d);
                    }catch(Exception e1){}
                    
                    String e2 = y2.getText();
                    try{
                    num5 = Integer.parseInt(e2);
                    }catch(Exception e1){}
                    
                    String f = y3.getText();
                    try{
                    num6 = Integer.parseInt(f);
                    }catch(Exception e1){}
                    
                    
                    String g = mat1.getText();
                    try{
                    m1 = Integer.parseInt(g);
                    }catch(Exception e1){}
                    
                    String i = mat2.getText();
                    try{
                    m2 = Integer.parseInt(i);
                    }catch(Exception e1){}
                    
                    String h = mat3.getText();
                    try{
                    m3 = Integer.parseInt(h);
                    }catch(Exception e1){}
                    
                    String j = mat4.getText();
                    try{
                    m4 = Integer.parseInt(j);
                    }catch(Exception e1){}
                    
                    
                    
                    
                    JTrianglePanel p = new JTrianglePanel(matrix1, matrix2, matrix3, matrix4, matrix5, matrix6);
                    JOptionPane.showMessageDialog(p, p);
                    
                    
                    
                    
                    
                    
                    
                    
                    
                
                
            }
            });
    Last edited by rowrowrowyourboat; 01-22-2012 at 10:52 PM.

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

    Default Re: Paint and actionlistener!!!!

    Please edit your post above to get rid of the unnecessary and distracting white space. I'll wait.
    Last edited by Fubarable; 01-22-2012 at 10:51 PM.

  16. #36
    Join Date
    Jan 2012
    Location
    hungary
    Posts
    27
    Rep Power
    0

    Default Re: Paint and actionlistener!!!!

    better?

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

    Default Re: Paint and actionlistener!!!!

    It's some better. You should not have more than one blank line in a row.

    Anyway...
    • Again, you should have the constructor accept two int array parameters as this is much more compact and easier to understand than 6 int variables.
    • Whichever route you go, you need to use the values passed into the parameters to update the class fields. i.e.,
      Java Code:
      class Foo {
         private int[] xArray;
         private int[] yArray;
      
        public Foo(int[] xArray, int[] yArray) {
      
            // you need to update the class fields with the parameters in the constructor.
            this.xArray = xArray;
            this.yArray = yArray;
        }
      }
    • Then create two int arrays just before you create your JTrianglePanel object inside of the actionPerformed.
    • The first parameter of the JOptionPane that you use to display the JTrianglePane object should be this which is the current Cool JFrame. The JTrianglePane object, p, should be the second parameter (as you've currently got it).

  18. #38
    Join Date
    Jan 2012
    Location
    hungary
    Posts
    27
    Rep Power
    0

    Default Re: Paint and actionlistener!!!!

    ok, but if i choose the variable pathway, how will i update the class fields?

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

    Default Re: Paint and actionlistener!!!!

    Quote Originally Posted by rowrowrowyourboat View Post
    ok, but if i choose the variable pathway, how will i update the class fields?
    Please explain what you mean?

  20. #40
    Join Date
    Jan 2012
    Location
    hungary
    Posts
    27
    Rep Power
    0

    Default Re: Paint and actionlistener!!!!

    lets assume i chose to do it by variables, not arrays. do i simply say this.matrix1 = matrix1. and we're talking about jtrianglepanel's constructor right?

Page 2 of 3 FirstFirst 123 LastLast

Similar Threads

  1. Paint
    By ninjaturtlez in forum AWT / Swing
    Replies: 4
    Last Post: 12-17-2011, 04:15 AM
  2. Please help with actionlistener
    By ADustedEwok in forum New To Java
    Replies: 5
    Last Post: 12-08-2011, 10:04 PM
  3. ActionListener
    By jaylimix in forum Java Applets
    Replies: 5
    Last Post: 11-06-2011, 06:05 PM
  4. Paint????
    By seanfmglobal in forum New To Java
    Replies: 3
    Last Post: 02-15-2011, 08:00 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
  •