Results 1 to 8 of 8

Thread: Java muilti-thread to paint graphics.

  1. #1
    Join Date
    Sep 2010
    Posts
    5,762
    Mentioned
    136 Post(s)
    Quoted
    2739 Post(s)

    Default Java muilti-thread to paint graphics.

    I'm trying to draw text on the GUI for my java program but it's not working..

    java Code:
    import java.awt.*;
    //import java.awt.event.*;

    import javax.swing.*;
    //import javax.swing.event.*;

    //import java.awt.Container;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    //import java.awt.event.WindowAdapter;
    //import java.awt.event.WindowEvent;
    //import java.awt.AWTException;

    public class Size extends JPanel implements ActionListener {

        public Size() {

            JButton calc;

            calc = new JButton("Calculate");

            add(calc);

            calc.setBounds(135, 200, 120, 40);

            setPreferredSize(new Dimension(400, 300));
            setLayout(null);

        }

        public static void main(String[] args) {
            JFrame frame = new JFrame("calc");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.getContentPane().add(new Size());
            frame.pack();
            frame.setVisible(true);
        }

        @Override
        public void actionPerformed(ActionEvent e) {

        }
        public void DrawStats(Graphics g) {

            g.setFont(new Font(null, Font.PLAIN, 18));
            g.setColor(Color.red);
            g.drawString("string", 5, 5);

        }

    }

  2. #2
    Join Date
    Feb 2009
    Posts
    2,155
    Mentioned
    4 Post(s)
    Quoted
    42 Post(s)

    Default

    Any errors? Simply saying, it isn't working won't help anyone solve the issue.


    You don't have a paint method.

    Not sure if "DrawStats()" is a method that can override the paint method

  3. #3
    Join Date
    Sep 2010
    Posts
    5,762
    Mentioned
    136 Post(s)
    Quoted
    2739 Post(s)

    Default

    Quote Originally Posted by JPHamlett View Post
    Any errors? Simply saying, it isn't working won't help anyone solve the issue.


    You don't have a paint method.

    Not sure if "DrawStats()" is a method that can override the paint method
    No errors i just get the JPanel with the calculate button

  4. #4
    Join Date
    Feb 2009
    Posts
    2,155
    Mentioned
    4 Post(s)
    Quoted
    42 Post(s)

    Default

    Add something like this

    Code:
    public void paint(Graphics g){
       drawStats(g);
    }

  5. #5
    Join Date
    Sep 2010
    Posts
    5,762
    Mentioned
    136 Post(s)
    Quoted
    2739 Post(s)

    Default

    Quote Originally Posted by JPHamlett View Post
    Add something like this

    Code:
    public void paint(Graphics g){
       drawStats(g);
    }
    OO that worked but is it muilti-threading and updating in real time?

  6. #6
    Join Date
    Feb 2009
    Posts
    2,155
    Mentioned
    4 Post(s)
    Quoted
    42 Post(s)

    Default

    Multi I think.

  7. #7
    Join Date
    Sep 2010
    Posts
    5,762
    Mentioned
    136 Post(s)
    Quoted
    2739 Post(s)

    Default

    Quote Originally Posted by JPHamlett View Post
    Multi I think.
    Ok now it's not drawing the button and it's claiming "Calc" cannot be resolved to a variable (at e.getsource) but the code still runs

    java Code:
    import java.awt.*;
    //import java.awt.event.*;

    import javax.swing.*;
    //import javax.swing.event.*;

    //import java.awt.Container;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    //import java.awt.event.WindowAdapter;
    //import java.awt.event.WindowEvent;
    //import java.awt.AWTException;

    public class Size extends JPanel implements ActionListener {
        double base;
        int shoesize;
        String race;

        public Size() {

            JButton calc;

            calc = new JButton("Calculate");
           
            calc.addActionListener(this);


            add(calc);

            calc.setBounds(135, 200, 120, 40);

            setPreferredSize(new Dimension(400, 300));
            setLayout(null);

        }

        public static void main(String[] args) {
            JFrame frame = new JFrame("Size calc");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.getContentPane().add(new Size());
            frame.pack();
            frame.setVisible(true);
        }

        @Override
       
        public void paint(Graphics g){
            DrawStats(g);

        }
        public void actionPerformed(ActionEvent e) {
           
            if (e.getSource() == calc) {
               
               
            }
        }
        public void DrawStats(Graphics g) {

            g.setFont(new Font(null, Font.PLAIN, 12));
            g.setColor(Color.red);
            g.drawString("Aprrox Size", 7, 7);

        }
    }

  8. #8
    Join Date
    Feb 2011
    Location
    The Future.
    Posts
    5,600
    Mentioned
    396 Post(s)
    Quoted
    1598 Post(s)

    Default

    Calc is declared within scope of another function. The error simply states that the paint function cannot see the calc variable because it is not within global scope or local scope of the paint function. Calc is declared within the scope of the size function.

    Also when dealing with swing, you "should" obey the single thread rule and all events must be done through EDT:

    Java Code:
    SwingUtilities.InvokeAndWait(new Runnable() { //There are two functions: InvokeAndWait and InvokeLater..
        //Do Swing stuff here.. Update GUI..
        //Set JFrame stuff/visibility, etc..
    });

    You don't have to but you should.
    Last edited by Brandon; 05-15-2013 at 02:05 AM.
    I am Ggzz..
    Hackintosher

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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