Page 3 of 3 FirstFirst 123
Results 41 to 53 of 53
Like Tree2Likes

Thread: Paint and actionlistener!!!!

  1. #41
    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
    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?
    You can do it that way too, have 6 int parameters and 6 int class fields. Then you will have to create your arrays in the paintComponent method. But either your way or mine, you're still using variables, it's just with mine the variables in question are int array variables and with your way they are int variables.

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

    Default Re: Paint and actionlistener!!!!

    excuse my stupidity.. but yeah there is an error. JOptionPane.showMessageDialog(this,p);
    "The method showMessageDialog(Component, Object) in the type JOptionPane is not applicable for the arguments (new ActionListener(){}, JTrianglePanel)"

    also, here is the new code i revised via your method:
    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.matrix1 = matri1;
    		this.matrix2 = matri2;
    		this.matrix3 = matri3;
    		this.matrix4 = matri4;
    		this.matrix5 = matri5;
    		this.matrix6 = 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);
    	
    		
    	
           }
    	
    	
    	
    	
    	
    	}

  3. #43
    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
    excuse my stupidity.. but yeah there is an error. JOptionPane.showMessageDialog(this,p);
    "The method showMessageDialog(Component, Object) in the type JOptionPane is not applicable for the arguments (new ActionListener(){}, JTrianglePanel)"
    My bad. You are calling this inside of an inner ActionListener class, and you have to specify that the "this" you are referring to is the Cool class that holds the ActionListener. You do that by changing "this" to "Cool.this":

    Java Code:
                JTrianglePanel p = new JTrianglePanel(/* ... load all the data in here */);
                
                JOptionPane.showMessageDialog(Cool.this, p);
    Your code is gradually looking better logic and syntax wise. I'd still like less white-space

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

    Default Re: Paint and actionlistener!!!!

    right, i debugged the syntax. and a blank frame..like last time
    Java Code:
    				
    				JTrianglePanel p = new JTrianglePanel(matrix1, matrix2, matrix3, matrix4, matrix5, matrix6);
    			    JOptionPane.showMessageDialog(Cool.this, p);
    so thats and i presented my other class at the previous post. so its about as logical as it can get (without hacking numbers away) what now?

  5. #45
    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
    right, i debugged the syntax. and a blank frame..like last time

    so thats and i presented my other class at the previous post. so its about as logical as it can get (without hacking numbers away) what now?
    1. Post your latest version of both classes again. Let's be sure that there's no ambiguity about what the code looks like. Again I ask that empty lines be limited to no more than 1. Don't worry about wasting forum resources. This project is important and deserves full attention.
    2. Use println's to help you debug -- to show that you're values are what you think they are.

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

    Default Re: Paint and actionlistener!!!!

    Main(Cool)
    Java Code:
    import java.awt.BorderLayout;
    
    
    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(Cool.this, p);
    				
    				
    		}
    		});
    
    		
    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);
    	}
    		
    }

    JTrianglePanel
    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.matrix1 = matri1;
    		this.matrix2 = matri2;
    		this.matrix3 = matri3;
    		this.matrix4 = matri4;
    		this.matrix5 = matri5;
    		this.matrix6 = matri6;
    		
    		this.setPreferredSize(new Dimension(300,300));
    		
    		repaint();
    		
    		System.out.println("RUNNNING!");
    		
    	}
    		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);
    	
    	}
    }
    i apologize for any white spaces.

  7. #47
    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
    .....
    i apologize for any white spaces.
    Don't apologize... fix!

    By using println's I meant doing something like this:

    Java Code:
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Polygon;
    import javax.swing.JPanel;
    
    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.matrix1 = matri1;
          this.matrix2 = matri2;
          this.matrix3 = matri3;
          this.matrix4 = matri4;
          this.matrix5 = matri5;
          this.matrix6 = matri6;
    
          System.out.println("matrix1: " + matrix1);
          System.out.println("matrix2: " + matrix2);
          System.out.println("matrix3: " + matrix3);
          System.out.println("matrix4: " + matrix4);
          System.out.println("matrix5: " + matrix5);
          System.out.println("matrix6: " + matrix6);
          
          this.setPreferredSize(new Dimension(300, 300));
    
       }
    
       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);
    
       }
    }
    If you do this, what do you see?

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

    Default Re: Paint and actionlistener!!!!

    hmm, all matrices are 0. there's an issue with the parsing methods.
    i input 1 2 5 6 2 7, and yeah i did this earlier. and it worked but then i encountered the paint problem that we discussed earlier, and you solved that but this seems new to my perception.
    matrix1: 0
    matrix2: 0
    matrix3: 0
    matrix4: 0
    matrix5: 0
    matrix6: 0
    Last edited by rowrowrowyourboat; 01-23-2012 at 12:21 AM.

  9. #49
    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!!!!

    OK, so all the parameters passed in are zero... now look back in your code and check to see what variables you use as parameters when you call the JTrianglePanel constructor. Do you update these parameters with the values that the user has placed into the JTextFields at the time the JButton is called (when the ActionListener calls its actionPerformed method)? Are you sure?

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

    Default Re: Paint and actionlistener!!!!

    wow! alright, i just fixed it, and its working. shit, thats good. thank you so much for being generous and helpful. and sorry for inquiring so much!
    thank you!

  11. #51
    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
    wow! alright, i just fixed it, and its working. shit, thats good. thank you so much for being generous and helpful. and sorry for inquiring so much!
    thank you!
    You're welcome, and please don't apologize! The key is that you've solved it yourself without anyone giving you the code -- congrats!

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

    Default Re: Paint and actionlistener!!!!

    thanks! maybe when i gain more experience, ill begin contributing to this forum! really nice place (better than other places ive visited)
    in brevity, thanks for everything!

  13. #53
    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!!!!

    Again, you're welcome! I got a lot out of this interaction, mainly seeing you solve this yourself.

Page 3 of 3 FirstFirst 123

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
  •