Results 1 to 4 of 4
  1. #1
    Jcbconway is offline Senior Member
    Join Date
    Sep 2010
    Posts
    109
    Rep Power
    0

    Default Painting Problem!

    Hello, i had recently had some help with my variable reference program and now i am trying to use the paintcomponant(), which i have used a lot before but now for an unknown reason too me, i cannot call the repaint(); method.

    Java Code:
    [B]Main:
    [/B][CODE]
    package newguitest;
    //calls imports for JFrame
    import javax.swing.*;
    import java.awt.*;
    
    //Main class with extended JFrame throught the main object
    class Main extends JFrame{
    
        public void frameSetup(){
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            GridLayout gl = new GridLayout(2, 1);
            Gui g = new Gui();
            Setup s = new Setup();
            g.PanelOneSetup();
            s.panelTwoSetup();
            add(g);
            add(s);
            setLayout(gl);
            setSize(206, 335);
            setResizable(true);
            setLocationRelativeTo(null);
            setVisible(true);
        }
    
        //main class called
        public static void main(String[] args) {
            Main m = new Main();
            m.frameSetup();
        }
    
    }
    Gui:
    Java Code:
    package newguitest;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class Gui extends JPanel implements ActionListener{
    
        int px;
        int py;
    
        public void PanelOneSetup(){
            setBackground(Color.LIGHT_GRAY);
            setDoubleBuffered(true);
        }
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g;
            System.out.println("Painted");
            g2d.setColor(Color.BLACK);
            g2d.drawOval(px,py,3,3);
        }
      [COLOR="Red"] public void getNum(Setup sp){
         px = sp.ix;
         py = sp.iy;
         System.out.println(px+","+py);
         System.out.println("Printing...");
         testPrint();
         [B][I]repaint();[/I][/B]
        }[/COLOR]
       public void testPrint(){
           System.out.println("Tested");
       }
    
        public void actionPerformed(ActionEvent e) {
    
        }
    
    
    }
    Setup:
    Java Code:
    package newguitest;
    
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    
    public class Setup extends JPanel implements ActionListener {
    
        JButton b1 = new JButton("(Add)");
        JButton b2 = new JButton("Solve");
        JTextField tfx = new JTextField(7);
        JTextField tfy = new JTextField(7);
        int ix;
        int iy;
    
        [COLOR="Lime"]public void dataSetup() {
            Gui g = new Gui();
            ix = Integer.parseInt(tfx.getText());
            iy = Integer.parseInt(tfy.getText());
            tfx.setText("");
            tfy.setText("");
            g.getNum(this);
        }[/COLOR]
    
        public void panelTwoSetup() {
    
            JLabel l1 = new JLabel("Enter X");
            JLabel l2 = new JLabel("Enter Y");
            JLabel space = new JLabel("          ");
            JPanel p1 = new JPanel();
    
            add(l1);
            add(space);
            add(l2);
            add(tfx);
            add(tfy);
            add(b1);
            add(b2);
    
            setBackground(Color.gray);
    
            b1.addActionListener(this);
            b2.addActionListener(this);
    
        }
    
        public void actionPerformed(ActionEvent e){
            if (e.getSource() == b1) {
                dataSetup();
            }
            if (e.getSource() == b2) {
    
            }
        }
    
        }
    [/CODE]

    When i hit the b2 button, in my setup object, it gets the number from the textfield and then calls the getNum() method in my GUI object. there, it calls repaint, but it doesn't actually repaint.

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

    Default

    The Gui object being displayed is not the same as the one that has repaint called on it. Everytime you call new Gui(), you're creating a new object. Instead you're going to want to pass a reference from the viewed Gui to the other classes that need to use it.

  3. #3
    Jcbconway is offline Senior Member
    Join Date
    Sep 2010
    Posts
    109
    Rep Power
    0

    Default

    But im calling the repaint in Gui()? But idk whats different now because ive done stuff similar to this hundreds of times before! and im missing something!

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

    Default

    Again, when you call a class's constructor, you create a new object of the class. For example, here is the GUI object that is being shown in the display:

    Java Code:
        public void frameSetup(){
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            GridLayout gl = new GridLayout(2, 1);
            Gui g = [color="red"]new Gui();[/color] // you are creating a Gui object here
            Setup s = new Setup();
            g.PanelOneSetup();
            s.panelTwoSetup();
            add(g);  // and adding it to the display here.

    And the Setup ActionListener's actionPerformed method calls the dataSetup method, and in this method you create another Gui object:
    Java Code:
        public void dataSetup() {
            Gui g = [color="red"]new Gui();[/color]
            ix = Integer.parseInt(tfx.getText());
            iy = Integer.parseInt(tfy.getText());
            tfx.setText("");
            tfy.setText("");
            g.getNum(this);
        }
    and other than coming from the same class, the two Gui objects are completely unrelated to each other. Data in the JTextField of the displayed Gui object will have no effect on the empty and non-displayed JTextFields above. Your assumptions are equivalent to your having the Microsoft Word word-processing program on two computers, one at home and one at work, and thinking that if you create a document in one, it will automatically appear in the other. But of course it can't because even though they are the same program, they are two completely different realizations of the program, and the same is true for your objects above.

    The solution is to not create a new Gui object in the ActionListener but rather to pass the displayed Gui object into the ActionListener via say a constructor parameter.

    Java Code:
            Gui g = new Gui(); // you are creating a Gui object here
            Setup s = new Setup(g);  // pass the displayed Gui object into Setup

    Java Code:
    public class Setup extends JPanel implements ActionListener {
    
        [color="red"]private Gui g;  // added[/color]
    
        JButton b1 = new JButton("(Add)");
        JButton b2 = new JButton("Solve");
        JTextField tfx = new JTextField(7);
        JTextField tfy = new JTextField(7);
        int ix;
        int iy;
    
        [color="red"]public Setup(Gui g) {
            this.g = g;
        }[/color]
    
        public void dataSetup() {
            [color="red"]// Gui g = new Gui(); // commment this out[/color]
            ix = Integer.parseInt(tfx.getText());
            iy = Integer.parseInt(tfy.getText());
            tfx.setText("");
            tfy.setText("");
            g.getNum(this);
        }
    Last edited by Fubarable; 11-17-2010 at 04:20 AM.

Similar Threads

  1. Painting in SWT
    By jionnet in forum SWT / JFace
    Replies: 9
    Last Post: 09-24-2010, 06:52 AM
  2. Problem with JApplet painting
    By Fuzzier in forum Java Applets
    Replies: 5
    Last Post: 07-29-2010, 08:54 PM
  3. painting problem
    By hannes in forum New To Java
    Replies: 3
    Last Post: 01-17-2010, 11:44 AM
  4. Replies: 12
    Last Post: 10-09-2009, 06:26 AM
  5. Jpanel painting problem
    By kcakir in forum AWT / Swing
    Replies: 3
    Last Post: 04-15-2009, 10:21 PM

Posting Permissions

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