Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Can't call "paint" method from other method

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Can't call "paint" method from other method

    I am new member at this forum.
    I want to call "paint method" from other "actionPerformed method".
    It want to "Graphics g" parameter

    Codes are below.The problem is in "actionPerformed" method .I specified with "?" sign.

    Waiting for help in an emergency. Thank you in advance...

    ----------------------------------------------
    import java.awt.Graphics;
     
    public interface GrafikCiz {
     
    public void paint(Graphics g);
     
    }
    ---------------------------------
    import java.awt.Graphics;
     
    public class Grafik {
     
    public GrafikCiz grafikciz;
     
    public Grafik(GrafikCiz grafikciz) {
    this.grafikciz = grafikciz;
    }
    }
    public void paint(Graphics g) {
    grafikciz.paint(g);
     
    }
    ----------------------------------------------------------------------
    import java.applet.Applet;
    import java.awt.Color;
    import java.awt.Graphics;
     
    public class Sinus extends Applet implements GrafikCiz {
     
    @Override
    public void paint(Graphics g) {
     
    final int CYCLE = 4;
    final int MAX = 1000;
    int x1 = 0;
    int x2 = 0;
    g.setColor(Color.red);// color for axes
    g.drawLine(0, 150, 700, 150);// x-axis
    g.drawLine(240, 0, 240, 500);// y-axis
    g.drawString("X-Axis", 430, 140);// Label for x-axis
    g.drawString("Y-Axis", 200, 270);// Label for y-axis
    g.setColor(Color.blue);// color for the sin curve
    for (int i = -130; i <= 368; i++) {
    x1 = (int) (100 * Math.sin(((i) * 2 * Math.PI * CYCLE) / (MAX)));
    x2 = (int) (100 * Math.sin(((i + 1) * 2 * Math.PI * CYCLE) / (MAX)));
    g.drawLine(i + 121, x1 + 138, (i + 1) + 121, x2 + 138);
    }
     
    }
     
    }
    ------------------------------------------------------------------------------------
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.Graphics;
     
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
     
    class ButtonPanel extends JPanel implements ActionListener {
    private JButton sin;
    private JButton cos;
    private JButton tan;
    private JFrame frame;
    private JPanel p;
     
    public ButtonPanel() {
    sin = new JButton("Sinus");
    cos = new JButton("Cosinus");
    tan = new JButton("Tanjant");
    frame = new JFrame("Fonksiyonları Grafigi");
    p = new JPanel();
    sin.setVisible(true);
    cos.setVisible(true);
    tan.setVisible(true);
    frame.setVisible(true);
    p.setVisible(true);
    sin.setSize(50, 30);
    cos.setSize(50, 30);
    tan.setSize(50, 30);
    frame.setSize(300, 200);
    frame.add(p);
    p.add(sin);
    p.add(cos);
    p.add(tan);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    sin.addActionListener((ActionListener) this);
    cos.addActionListener((ActionListener) this);
    tan.addActionListener((ActionListener) this);
     
    }
     
    public void actionPerformed(ActionEvent evt) {
    Object source = evt.getSource();
    if (source == sin) {
     
    Grafik grafik=new Grafik(new Sinus());
    ------???????? grafik.paint()//I want to call "paint method"
     
    } else if (source == cos) {
    Grafik grafik=new Grafik(new Cosinus());
     
    }
     
    else if (source == tan) {
    Grafik grafik=new Grafik(new Tanjant());
     
    }
    }
    }
     
    public class Client extends javax.swing.JFrame {
     
    public static void main(String[] args) {
    ButtonPanel c = new ButtonPanel();
     
    }
     
    }
    Last edited by curmudgeon; November 17th, 2012 at 12:20 PM. Reason: code tags added


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Can't call "paint" method from other method

    I've added [code=java] [/code] around your code to let the code's formatting show through, but none of your code is well formatted and in fact all is left justified. If you truly need help "in an emergency", please put in the effort to only post well formatted code so that we can actually read and understand it. I invite you to edit your original post, and add well formatted code, surrounded by code tags as I've shown you above.

    Also, why are you trying to mix an Applet, an AWT top-level window with a JFrame, a Swing component top-level window. This is a most unusual thing to do, and should generally be discouraged.

    Finally, you would "call" the paint method of any component by having it called from within the Swing component's paintComponent method, here the paintComponent method of your JPanel. More on this once you post better code, code that is easier to read.

  3. #3
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Can't call "paint" method from other method

    Hmm...maybe try using grafikciz.repaint(g);? That's the only thing I can think of off the top of my head that could be wrong.
    Current Projects (and planned release dates):

    Chomp's Wacky Worlds [???] (PC, Android and Ouya)
    Kyle the Caiman [???] (PC, Android and Ouya)
    KTC: King Crocko's Mystic Maze [???] (PC, Android and Ouya)

  4. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Can't call "paint" method from other method

    Quote Originally Posted by Gravity Games View Post
    Hmm...maybe try using grafikciz.repaint(g);? That's the only thing I can think of off the top of my head that could be wrong.
    Yes, that is wrong and won't work since grafikciz has never been rendered.

  5. #5
    Junior Member
    Join Date
    Nov 2012
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can't call "paint" method from other method

    I want to run "paint method" when I pressed "sinus button"
    But I can't call the "paint method" because.It wants the parameter(Graphics g).What I should write the parameter while I call the "paint method".

  6. #6
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Can't call "paint" method from other method

    Again, please edit your original post so that it the posted code is formatted. It's very hard to understand code that others post here, why make it harder?

    Again, please address my concerns on why are you mixing Applet code with JFrame code? This shouldn't be done.

  7. #7
    Junior Member
    Join Date
    Nov 2012
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can't call "paint" method from other method

    //There is a class that draw the function of sinus.
    import java.applet.Applet;
    import java.awt.Color;
    import java.awt.Graphics;

    public class Sinus extends Applet {

    public void paint(Graphics g) {

    final int CYCLE = 4;
    final int MAX = 1000;
    int x1 = 0;
    int x2 = 0;
    g.setColor(Color.red);// color for axes
    g.drawLine(0, 150, 700, 150);// x-axis
    g.drawLine(240, 0, 240, 500);// y-axis
    g.drawString("X-Axis", 430, 140);// Label for x-axis
    g.drawString("Y-Axis", 200, 270);// Label for y-axis
    g.setColor(Color.blue);// color for the sin curve
    for (int i = -130; i <= 368; i++) {
    x1 = (int) (100 * Math.sin(((i) * 2 * Math.PI * CYCLE) / (MAX)));
    x2 = (int) (100 * Math.sin(((i + 1) * 2 * Math.PI * CYCLE) / (MAX)));
    g.drawLine(i + 121, x1 + 138, (i + 1) + 121, x2 + 138);
    }

    }
    }
    -----------------------------------------------------------------------------------------------------------------------
    //If user choose "sinus",ıt will run paint()method
    public class Client{
    public static void main(String[] args) {
    Scanner scan=new Scanner(System.in);
    System.out.println("Choose the function");
    System.out.println("1-Sinus");
    System.out.println("1-Cosinus");
    int choice=scan.nextInt();
    if(choice==1){
    //Here I want to call paint()
    }
    }
    }

  8. #8
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Can't call "paint" method from other method

    Again, please tell us why are you using an Applet here? Again, please use [code=java] [/code] tags around your posted code. If you edit your original post, you can see how I used them around your previously posted code.

  9. #9
    Junior Member
    Join Date
    Nov 2012
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can't call "paint" method from other method

    //There is a class that draw the function of sinus.
    import java.applet.Applet;
    import java.awt.Color;
    import java.awt.Graphics;
     
    public class Sinus extends Applet {
     
    public void paint(Graphics g) {
     
    final int CYCLE = 4;
    final int MAX = 1000;
    int x1 = 0;
    int x2 = 0;
    g.setColor(Color.red);// color for axes
    g.drawLine(0, 150, 700, 150);// x-axis
    g.drawLine(240, 0, 240, 500);// y-axis
    g.drawString("X-Axis", 430, 140);// Label for x-axis
    g.drawString("Y-Axis", 200, 270);// Label for y-axis
    g.setColor(Color.blue);// color for the sin curve
    for (int i = -130; i <= 368; i++) {
    x1 = (int) (100 * Math.sin(((i) * 2 * Math.PI * CYCLE) / (MAX)));
    x2 = (int) (100 * Math.sin(((i + 1) * 2 * Math.PI * CYCLE) / (MAX)));
    g.drawLine(i + 121, x1 + 138, (i + 1) + 121, x2 + 138);
    }
     
    }
    }

    //If user choose "sinus",ıt will run paint()method
    public class Client{
    public static void main(String[] args) {
    Scanner scan=new Scanner(System.in);
    System.out.println("Choose the function");
    System.out.println("1-Sinus");
    System.out.println("1-Cosinus");
    int choice=scan.nextInt();
    if(choice==1){
    //Here I want to call paint()
    }
    }
    }

Similar Threads

  1. Can't call "paint" method from other method
    By aslanali555 in forum Object Oriented Programming
    Replies: 1
    Last Post: November 17th, 2012, 01:30 PM
  2. Can't call paint() method from another class
    By Huw in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 18th, 2012, 02:15 AM
  3. Shouldn't "list" be a parameter of the method Create()?
    By wholegrain in forum Object Oriented Programming
    Replies: 6
    Last Post: February 13th, 2012, 12:40 AM
  4. How to run a "Add"-method from a method?
    By pinotje in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 2nd, 2011, 11:01 AM
  5. "Static method cannot hide instance method from implemented Interface"
    By Gthoma2 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 21st, 2011, 03:03 AM