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.

Page 1 of 2 12 LastLast
Results 1 to 25 of 38

Thread: buttons that call methods

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

    Default buttons that call methods

    Hey guys been lurking for awhile, I am at wits end on this. I have been learning java for the past couple months been doing usual stuff like methods, loops, if and thens that kind of stuff. Well I have to write a program that will 4 buttons and print an image that I created in the method. My biggest problem is my actionperformed is not looking outside to see my method as they are called when button three is pressed. I am at a loss as to what to do. Button three is suppose to flip flop between a drawn img(drawpicture) and a stored img(fishimg) in a folder

    heres the code. Any help would be appreciated I am just frustrated and this is an online class so not really any teacher to turn to when ever I need help.

    also one last thing, a loop that draws a line one right under the other thickening the line to the input of the user, I can do most of that just not sure how to tell it write the next line directly below the first line.

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.Random;

    //Cole Williams
    //Making smiles and colors with buttons

    public class williamsclab10 extends JApplet implements ActionListener {

    Image fishimg;
    String imgFile = "../images/fish.gif";

    TextField text = new TextField(10);
    int number = -1;

    private static Random background = new Random();

    private Button color;

    private Button print;

    private Button change;

    private Button surprise;

    int count = 0;

    boolean vis = false;
    boolean showmypicture = false;


    public void init() {
    setSize(600, 400);

    Container content = getContentPane();

    int red = Math.abs(background.nextInt()) % 256;
    int green = Math.abs(background.nextInt()) % 256;
    int blue = Math.abs(background.nextInt()) % 256;
    getContentPane().setBackground(new Color(red, green, blue));

    fishimg = getImage(getCodeBase(), imgFile);

    setLayout(new FlowLayout());

    text.addActionListener(this);
    add(text);

    color = new Button("Change the color");
    add(color);
    color.addActionListener(this);

    print = new Button("Print my picture!");
    add(print);
    color.addActionListener(this);

    change = new Button("Change the picture!");
    add(change);
    color.addActionListener(this);

    surprise = new Button("Click here for a surpise!");
    add(surprise);
    color.addActionListener(this);

    }

    private void drawpicture(Graphics g, int x, int y)
    {
    // tail
    g.setColor(Color.blue);
    g.drawLine(x, y, x + 30, y);
    g.drawLine(x + 30, y, x + 30, y + 30);
    g.drawLine(x + 30, y + 30, x, y);

    // body
    g.setColor(Color.blue);
    g.drawOval(x + 20, y + -33, 40, 40);
    g.setColor(Color.blue);
    g.drawOval(x + 36, y + -32, 10, 10);

    }

    public void paint(Graphics g) {
    super.paint(g);

    Dimension d = getSize();

    text.setLocation(d.width / 2 - 30, 30);

    int centerX = d.width / 2;
    int centerY = d.height / 2;
    int widthOfPicture = 100;
    int heightOfPicture = 100;
    int x = centerX - widthOfPicture / 2;
    int y = centerY - heightOfPicture / 2;


    change.setSize(120, 50);
    change.setLocation(d.width - 600, d.height - 50);
    color.setSize(120, 50);
    color.setLocation(d.width - 120, d.height - 50);
    print.setSize(120, 50);
    print.setLocation(d.width - 600, d.height - 400);
    surprise.setSize(150, 50);
    surprise.setLocation(d.width - 150, d.height - 400);

    }

    public void actionPerformed(ActionEvent a)
    {
    // button 1 background change
    if (a.getSource() == color)
    {

    int red = Math.abs(background.nextInt()) % 256;
    int green = Math.abs(background.nextInt()) % 256;
    int blue = Math.abs(background.nextInt()) % 256;

    getContentPane().setBackground(new Color(red, green, blue));
    }

    //button 2 prints picture centered
    if (a.getSource() == print)
    {


    }


    //button 3 picture toggling
    if (a.getSource() == change)

    {
    vis = true;
    showmypicture =! showmypicture;

    }
    //button 4 surprise
    if (a.getSource() == surprise)
    {

    drawpicture(25,25);

    }




    count++;
    repaint();

    }
    }


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: buttons that call methods

    Please use highlight tags in the future. You have a copy-and-paste mistake. Look at where you create the print button, the change button, and the surprise button. You attach the listener to the copy button each time, not to the print button, the change button, or the surprise button.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: buttons that call methods

    Will do on the highlight tags, are you saying I should have a,b,c,d for the actionperformed?

    --- Update ---

    OMG I see it now up top

    --- Update ---

    Corrected copy and paste issue, I was told I am also missing the G for the action listener but I am not sure I did it right.

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.Random;

    //Cole Williams
    //Making smiles and colors with buttons

    public class williamsclab10 extends JApplet implements ActionListener
    {

    Image fishimg;
    String imgFile = "../images/fish.gif";

    TextField text = new TextField(10);
    int number = -1;

    private static Random background = new Random();

    private Button color;

    private Button print;

    private Button change;

    private Button surprise;

    int count = 0;

    boolean vis = false;
    boolean showmypicture = false;


    public void init() {
    setSize(600, 400);

    Container content = getContentPane();

    int red = Math.abs(background.nextInt()) % 256;
    int green = Math.abs(background.nextInt()) % 256;
    int blue = Math.abs(background.nextInt()) % 256;
    getContentPane().setBackground(new Color(red, green, blue));

    fishimg = getImage(getCodeBase(), imgFile);

    setLayout(new FlowLayout());

    text.addActionListener(this);
    add(text);

    color = new Button("Change the color");
    add(color);
    color.addActionListener(this);

    print = new Button("Print my picture!");
    add(print);
    print.addActionListener(this);

    change = new Button("Change the picture!");
    add(change);
    change.addActionListener(this);

    surprise = new Button("Click here for a surpise!");
    add(surprise);
    surprise.addActionListener(this);

    }

    private void drawpicture(Graphics g, int x, int y)
    {
    // tail
    g.setColor(Color.blue);
    g.drawLine(x, y, x + 30, y);
    g.drawLine(x + 30, y, x + 30, y + 30);
    g.drawLine(x + 30, y + 30, x, y);

    // body
    g.setColor(Color.blue);
    g.drawOval(x + 20, y + -33, 40, 40);
    g.setColor(Color.blue);
    g.drawOval(x + 36, y + -32, 10, 10);

    }

    public void paint(Graphics g) {
    super.paint(g);

    Dimension d = getSize();

    text.setLocation(d.width / 2 - 30, 30);

    int centerX = d.width / 2;
    int centerY = d.height / 2;
    int widthOfPicture = 100;
    int heightOfPicture = 100;
    int x = centerX - widthOfPicture / 2;
    int y = centerY - heightOfPicture / 2;


    change.setSize(120, 50);
    change.setLocation(d.width - 600, d.height - 50);
    color.setSize(120, 50);
    color.setLocation(d.width - 120, d.height - 50);
    print.setSize(120, 50);
    print.setLocation(d.width - 600, d.height - 400);
    surprise.setSize(150, 50);
    surprise.setLocation(d.width - 150, d.height - 400);

    }

    public void actionPerformed(ActionEvent a)


    {
    Graphics g = getContentPane().getGraphics();
    // button 1 background change
    if (a.getSource() == color)
    {

    int red = Math.abs(background.nextInt()) % 256;
    int green = Math.abs(background.nextInt()) % 256;
    int blue = Math.abs(background.nextInt()) % 256;

    getContentPane().setBackground(new Color(red, green, blue));
    }

    //button 2 prints picture centered
    if (a.getSource() == print)
    {


    }


    //button 3 picture toggling
    if (a.getSource() == change)

    {
    vis = true;
    showmypicture =! showmypicture;

    }
    //button 4 surprise
    if (a.getSource() == surprise)
    {

    drawpicture(g,25,25);

    }




    count++;
    repaint();

    }
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: buttons that call methods

    Please Edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: buttons that call methods

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.Random;
     
    //Cole Williams 
    //Making smiles and colors with buttons 
     
    public class williamsclab10 extends JApplet implements ActionListener
    {
     
    	Image fishimg;	            											
    	String imgFile = "../images/fish.gif";
     
    	TextField text = new TextField(10);
    	int number = -1;
     
    	private static Random background = new Random();
     
    	private Button color;
     
    	private Button print;
     
    	private Button change;
     
    	private Button surprise;
     
    	int count = 0;
     
    	boolean vis = false;
    	boolean showmypicture = false;
     
     
    	public void init() {
    		setSize(600, 400);
     
    		Container content = getContentPane();
     
    		int red = Math.abs(background.nextInt()) % 256;
    		int green = Math.abs(background.nextInt()) % 256;
    		int blue = Math.abs(background.nextInt()) % 256;
    		getContentPane().setBackground(new Color(red, green, blue));
     
    		fishimg = getImage(getCodeBase(), imgFile);
     
    		setLayout(new FlowLayout());
     
    		text.addActionListener(this);
    		add(text);
     
    		color = new Button("Change the color");
    		add(color);
    		color.addActionListener(this);
     
    		print = new Button("Print my picture!");
    		add(print);
    		print.addActionListener(this);
     
    		change = new Button("Change the picture!");
    		add(change);
    		change.addActionListener(this);
     
    		surprise = new Button("Click here for a surpise!");
    		add(surprise);
    		surprise.addActionListener(this);
     
    	}
     
    	private void drawpicture(Graphics g, int x, int y)
    	{
    		// tail
    		g.setColor(Color.blue);
    		g.drawLine(x, y, x + 30, y);
    		g.drawLine(x + 30, y, x + 30, y + 30);
    		g.drawLine(x + 30, y + 30, x, y);
     
    		// body
    		g.setColor(Color.blue);
    		g.drawOval(x + 20, y + -33, 40, 40);
    		g.setColor(Color.blue);
    		g.drawOval(x + 36, y + -32, 10, 10);
     
    	}
     
    	public void paint(Graphics g) {
    		super.paint(g);
     
    		Dimension d = getSize();
     
    		text.setLocation(d.width / 2 - 30, 30);
     
    		int centerX = d.width / 2;
    		int centerY = d.height / 2;
    		int widthOfPicture = 100;
    		int heightOfPicture = 100;
    		int x = centerX - widthOfPicture / 2;
    		int y = centerY - heightOfPicture / 2;
     
     
    		change.setSize(120, 50);
    		change.setLocation(d.width - 600, d.height - 50);
    		color.setSize(120, 50);
    		color.setLocation(d.width - 120, d.height - 50);
    		print.setSize(120, 50);
    		print.setLocation(d.width - 600, d.height - 400);
    		surprise.setSize(150, 50);
    		surprise.setLocation(d.width - 150, d.height - 400);
     
    	}
     
    	public void actionPerformed(ActionEvent a) 
     
     
    	{		
    		Graphics g = getContentPane().getGraphics();
    	// button 1 background change
    		if (a.getSource() == color) 
    		{
     
    			int red = Math.abs(background.nextInt()) % 256;
    			int green = Math.abs(background.nextInt()) % 256;
    			int blue = Math.abs(background.nextInt()) % 256;
     
    			getContentPane().setBackground(new Color(red, green, blue));
    		}
     
    		//button 2 prints picture centered 
    		if (a.getSource() == print)
    		{
     
     
    		}
     
     
    			//button 3 picture toggling 
    				if (a.getSource() == change)
     
    				{
    					vis = true;
    					showmypicture =! showmypicture;	
     
    				} 
    				//button 4 surprise
    				if (a.getSource() == surprise)
    				{
     
    					drawpicture(g,25,25);
     
    				}
     
     
     
     
    		count++;
    		repaint();
     
    	}
    }

  6. #6
    Junior Member
    Join Date
    Nov 2012
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: buttons that call methods

    modified the code fixed mistakes, still not sure why its not pulling my drawnimage in

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: buttons that call methods

    The usual problem is that the path is wrong. Use the path to the image to create a File object and print the File object's absolute path to see it that is where the file is located.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member
    Join Date
    Nov 2012
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: buttons that call methods

    It should be the right path, it doesn't print anything.

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: buttons that call methods

    It should be the right path
    I know that you want it to be right, but what does the computer think its value is?

    it doesn't print anything.
    Then you must have the call to the println method in the wrong place. If it's executed it will print something. Move the println call to where it will be executed.

    Look in the browser's java console to see the output.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Junior Member
    Join Date
    Nov 2012
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: buttons that call methods

    It does work I called the image under init without buttons it pulled in just fine. I ran text.setText("test"); under each button they all printer test to my text box. My buttons are not calling my methods properly I do not know how to tell them to call outside of the actionperformed. It runs fine because they are declared but once it tries to call them it can't find them.

    heres the code as to what I have been doing so far Buttons and img work methods will not call
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.Random;
     
    //Cole Williams 
    //Making smiles and colors with buttons 
     
    public class williamsclab10 extends JApplet implements ActionListener
    {
     
    	Image fishimg;	            											
    	String imgFile = "../images/fish.gif";
     
    	TextField text = new TextField(10);
    	int number = -1;
     
    	private static Random background = new Random();
     
    	private Button color;
     
    	private Button print;
     
    	private Button change;
     
    	private Button surprise;
     
    	int count = 0;
     
    	boolean vis = false;
    	boolean showmypicture = false;
     
     
    	public void init() {
    		setSize(600, 400);
     
    		Container content = getContentPane();
     
    		int red = Math.abs(background.nextInt()) % 256;
    		int green = Math.abs(background.nextInt()) % 256;
    		int blue = Math.abs(background.nextInt()) % 256;
    		getContentPane().setBackground(new Color(red, green, blue));
     
    		fishimg = getImage(getCodeBase(), imgFile);
     
    		setLayout(new FlowLayout());
     
    		text.addActionListener(this);
    		add(text);
     
    		color = new Button("Change the color");
    		add(color);
    		color.addActionListener(this);
     
    		print = new Button("Print my picture!");
    		add(print);
    		print.addActionListener(this);
     
    		change = new Button("Change the picture!");
    		add(change);
    		change.addActionListener(this);
     
    		surprise = new Button("Click here for a surpise!");
    		add(surprise);
    		surprise.addActionListener(this);
     
    	}
     
    	private void drawpicture(Graphics g, int x, int y)
    	{
    		// tail
    		g.setColor(Color.blue);
    		g.drawLine(x, y, x + 30, y);
    		g.drawLine(x + 30, y, x + 30, y + 30);
    		g.drawLine(x + 30, y + 30, x, y);
     
    		// body
    		g.setColor(Color.blue);
    		g.drawOval(x + 20, y + -33, 40, 40);
    		g.setColor(Color.blue);
    		g.drawOval(x + 36, y + -32, 10, 10);
     
    	}
     
    	public void paint(Graphics g) {
    		super.paint(g);
     
    		Dimension d = getSize();
     
    		text.setLocation(d.width / 2 - 30, 30);
     
    		int centerX = d.width / 2;
    		int centerY = d.height / 2;
    		int widthOfPicture = 100;
    		int heightOfPicture = 100;
    		int x = centerX - widthOfPicture / 2;
    		int y = centerY - heightOfPicture / 2;
     
     
    		change.setSize(120, 50);
    		change.setLocation(d.width - 600, d.height - 50);
    		color.setSize(120, 50);
    		color.setLocation(d.width - 120, d.height - 50);
    		print.setSize(120, 50);
    		print.setLocation(d.width - 600, d.height - 400);
    		surprise.setSize(150, 50);
    		surprise.setLocation(d.width - 150, d.height - 400);
     
    	}
     
    	public void actionPerformed(ActionEvent a) 
     
     
    	{		
     
    		Graphics g = this.getContentPane().getGraphics();
    	// button 1 background change
    		if (a.getSource() == color) 
    		{
     
    			int red = Math.abs(background.nextInt()) % 256;
    			int green = Math.abs(background.nextInt()) % 256;
    			int blue = Math.abs(background.nextInt()) % 256;
     
    			getContentPane().setBackground(new Color(red, green, blue));
    		}
     
    		//button 2 prints picture centered 
    		else if (a.getSource() == print)
    		{
    			drawpicture(g,25,25);
    		}
     
     
    			//button 3 picture toggling 
    		else if (a.getSource() == change)
     
    				{
    					vis = true;
    					g.drawImage(fishimg, 50,100,200,200,this);
    					showmypicture =! showmypicture;	
     
    				} 
    				//button 4 surprise
    		else if (a.getSource() == surprise)
    				{
     
    			drawpicture(g,25,25);
    			g.drawImage(fishimg, 50,100,200,200,this);
     
    				}
     
     
     
     
    		count++;
    		repaint();
     
    	}
    }

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: buttons that call methods

    it can't find them.
    Does that mean you are getting compiler errors?
    Please explain how the code "can't find them".

    Buttons and img work methods will not call
    Can you explain what that means?

    You need to study how to display images from a program using the paintComponent() method. Your code violates the normal techniques for displaying images. Take a look at the tutorial:
    http://docs.oracle.com/javase/tutori...ing/index.html

    Also look at sample code on this forum that overrides the paintComponent() method.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: buttons that call methods

    what I mean by not finding them is as follows. When the code called by the button is contained inside the actionperformed it runs perfectly fine . Thats why button 1 works properly. If I call drawpicture with button 4 it does not display anything. It complies just fine it no errors, it can't call any method outside the actionperformed. It is seeing it in the compiler because it is not telling me it is undefined so it must see it. But if it is called when the applet is running it doesn't print the method. Both method's work because if I call them to print inside INIT they work. Could my background be hiding my methods once they are called by the buttons??

    --- Update ---

    this code I modified same code different order. My image flashed for a second then disappeared. I am not sure why it disappeared but its calling my methods at least something is just over w

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.Random;
     
    //Cole Williams 
    //Making smiles and colors with buttons 
     
    public class williamsclab10 extends JApplet implements ActionListener
    {
     
    	Image fishimg;	            											
    	String imgFile = "../images/fish.gif";
     
    	TextField text = new TextField(10);
    	int number = -1;
     
    	private static Random background = new Random();
     
    	private Button color;
     
    	private Button print;
     
    	private Button change;
     
    	private Button surprise;
     
    	int count = 0;
     
    	boolean vis = false;
    	boolean showmypicture = false;
     
     
    	public void init() {
    		setSize(600, 400);
     
    		Container content = getContentPane();
     
    		int red = Math.abs(background.nextInt()) % 256;
    		int green = Math.abs(background.nextInt()) % 256;
    		int blue = Math.abs(background.nextInt()) % 256;
    		getContentPane().setBackground(new Color(red, green, blue));
     
    		fishimg = getImage(getCodeBase(), imgFile);
     
    		setLayout(new FlowLayout());
     
    		text.addActionListener(this);
    		add(text);
     
    		color = new Button("Change the color");
    		add(color);
    		color.addActionListener(this);
     
    		print = new Button("Print my picture!");
    		add(print);
    		print.addActionListener(this);
     
    		change = new Button("Change the picture!");
    		add(change);
    		change.addActionListener(this);
     
    		surprise = new Button("Click here for a surpise!");
    		add(surprise);
    		surprise.addActionListener(this);
     
    	}
     
     
     
    	public void actionPerformed(ActionEvent a) 
     
     
    	{		
     
    		Graphics g = this.getContentPane().getGraphics();
    	// button 1 background change
    		if (a.getSource() == color) 
    		{
     
    			int red = Math.abs(background.nextInt()) % 256;
    			int green = Math.abs(background.nextInt()) % 256;
    			int blue = Math.abs(background.nextInt()) % 256;
     
    			getContentPane().setBackground(new Color(red, green, blue));
    		}
     
    		//button 2 prints picture centered 
    		else if (a.getSource() == print)
    		{
    			drawpicture(g,25,25);
    		}
     
     
    			//button 3 picture toggling 
    		else if (a.getSource() == change)
     
    				{
    					vis = true;
    					g.drawImage(fishimg, 50,100,200,200,this);
    					showmypicture =! showmypicture;	
     
    				} 
    				//button 4 surprise
    		else if (a.getSource() == surprise)
    				{
     
    			drawpicture(g,400,50);
    			g.drawImage(fishimg, 50,100,200,200,this);
     
    				}
     
     
     
     
    		count++;
    		repaint();
     
    	}
     
    	private void drawpicture(Graphics g, int x, int y)
    	{
    		// tail
    		g.setColor(Color.blue);
    		g.drawLine(x, y, x + 30, y);
    		g.drawLine(x + 30, y, x + 30, y + 30);
    		g.drawLine(x + 30, y + 30, x, y);
     
    		// body
    		g.setColor(Color.blue);
    		g.drawOval(x + 20, y + -33, 40, 40);
    		g.setColor(Color.blue);
    		g.drawOval(x + 36, y + -32, 10, 10);
     
    	}
     
     
    	public void paint(Graphics g) {
    		super.paint(g);
     
    		Dimension d = getSize();
     
    		text.setLocation(d.width / 2 - 30, 30);
     
    		int centerX = d.width / 2;
    		int centerY = d.height / 2;
    		int widthOfPicture = 100;
    		int heightOfPicture = 100;
    		int x = centerX - widthOfPicture / 2;
    		int y = centerY - heightOfPicture / 2;
     
     
    		change.setSize(120, 50);
    		change.setLocation(d.width - 600, d.height - 50);
    		color.setSize(120, 50);
    		color.setLocation(d.width - 120, d.height - 50);
    		print.setSize(120, 50);
    		print.setLocation(d.width - 600, d.height - 400);
    		surprise.setSize(150, 50);
    		surprise.setLocation(d.width - 150, d.height - 400);
    		count++;
    	//	repaint();
     
    	}
    }

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: buttons that call methods

    it can't call any method outside the actionperformed.
    How do you know that? Add a call to println() to the method that is being called and have it print a message when it is called.

    My image flashed for a second then disappeared.
    I said your code was NOT right for drawing in post#11. Did you read the tutorial about how to do drawing and painting?

    You do NOT get a graphics object and use it. You do all the drawing in the paint() method using the Graphics object passed to that method.
    If you don't understand my answer, don't ignore it, ask a question.

  14. #14
    Junior Member
    Join Date
    Nov 2012
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: buttons that call methods

    ok I am getting it, so something like if button 1 is pressed then do A and A corresponds with something in paint. like paint A which = the method. right?

    it can't print in the actionperformed but it can call something to print like: if (a=1) {drawpicture(g,25,25);} and the button calls A to be grabbed in the actionperformed

    am i understanding?

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: buttons that call methods

    The action listener determines what is to be drawn, saves some values somewhere and calls the repaint() method. A short time later the paint() method is called by the jvm, paint() looks at the saved values and draws what was decided by the listener.
    If you don't understand my answer, don't ignore it, ask a question.

  16. #16
    Junior Member
    Join Date
    Nov 2012
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: buttons that call methods

    right, so my action listener should have a value in it of some kind that tells paint what to print. When the action listener proves true it should print my picture when false it should do nothing, right?

  17. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: buttons that call methods

    The listener will decide what is to be drawn and set some class values for the paint() method to use to determine what it is going to draw.
    If you don't understand my answer, don't ignore it, ask a question.

  18. #18
    Junior Member
    Join Date
    Nov 2012
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: buttons that call methods

    like this I am only working with button 4 by the way?
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.Random;
     
    //Cole Williams 
    //Making smiles and colors with buttons 
     
    public class williamsclab10 extends JApplet implements ActionListener
    {
     
    	Image fishimg;	            											
    	String imgFile = "../images/fish.gif";
     
    	TextField text = new TextField(10);
    	int number = -1;
     
    	private static Random background = new Random();
     
    	private JButton color;
     
    	private JButton print;
     
    	private JButton change;
     
    	private JButton surprise;
     
    	int count = 0;
    	int A = 1;
    	int B = 1;
    	int C = 1;
    	int D = 1;
     
    	boolean vis = false;
    	boolean showmypicture = false;
     
     
    	public void init() {
    		setSize(600, 400);
     
    		Container content = getContentPane();
     
    		int red = Math.abs(background.nextInt()) % 256;
    		int green = Math.abs(background.nextInt()) % 256;
    		int blue = Math.abs(background.nextInt()) % 256;
    		getContentPane().setBackground(new Color(red, green, blue));
     
    		fishimg = getImage(getCodeBase(), imgFile);
     
    		setLayout(new FlowLayout());
     
    		text.addActionListener(this);
    		add(text);
     
    		color = new JButton("Change the color");
    		add(color);
    		color.addActionListener(this);
     
    		print = new JButton("Print my picture!");
    		add(print);
    		print.addActionListener(this);
     
    		change = new JButton("Change the picture!");
    		add(change);
    		change.addActionListener(this);
     
    		surprise = new JButton("Click here for a surpise!");
    		add(surprise);
    		surprise.addActionListener(this);
     
    	}
     
     
     
    	public void actionPerformed(ActionEvent a) 
     
     
    	{		
     
    		Graphics g = this.getContentPane().getGraphics();
    	// button 1 background change
    		if (a.getSource() == color) 
    		{
     
    			int red = Math.abs(background.nextInt()) % 256;
    			int green = Math.abs(background.nextInt()) % 256;
    			int blue = Math.abs(background.nextInt()) % 256;
     
    			getContentPane().setBackground(new Color(red, green, blue));
    		}
     
    		//button 2 prints picture centered 
    		else if (a.getSource() == print)
    		{
    			drawpicture(g,400,50);
    			g.drawImage(fishimg, 50,100,200,200,this);
    		}
     
    			//button 3 picture toggling 
    		else if (a.getSource() == change)
     
    				{
    					vis = true;
    					g.drawImage(fishimg, 50,100,200,200,this);
    					showmypicture =! showmypicture;	
     
    				} 
     
    				//button 4 surprise
    		else if (a.getSource() == surprise)
    				{
    			 	if (A == 1);
    			 	{
    			 		A = 1;
     
    			 	}
     
    				}
     
    		count++;
    		repaint();
     
    	}
     
    	private void drawpicture(Graphics g, int x, int y)
    	{
    		// tail
    		g.setColor(Color.blue);
    		g.drawLine(x, y, x + 30, y);
    		g.drawLine(x + 30, y, x + 30, y + 30);
    		g.drawLine(x + 30, y + 30, x, y);
     
    		// body
    		g.setColor(Color.blue);
    		g.drawOval(x + 20, y + -33, 40, 40);
    		g.setColor(Color.blue);
    		g.drawOval(x + 36, y + -32, 10, 10);
     
    	}
     
     
    	public void paint(Graphics g) {
    		super.paint(g);
     
    		Dimension d = getSize();
     
    		text.setLocation(d.width / 2 - 30, 30);
     
    		int centerX = d.width / 2;
    		int centerY = d.height / 2;
    		int widthOfPicture = 600;
    		int heightOfPicture = 600;
    		int x = centerX - widthOfPicture / 2;
    		int y = centerY - heightOfPicture / 2;
     
     
    		change.setSize(150, 50);
    		change.setLocation(d.width - 600, d.height - 50);
     
    		color.setSize(150, 50);
    		color.setLocation(d.width - 150, d.height - 50);
     
    		print.setSize(150, 50);
    		print.setLocation(d.width - 600, d.height - 400);
     
    		surprise.setSize(160, 50);
    		surprise.setLocation(d.width - 160, d.height - 400);
     
    		if (A == 1);
    		{
    			drawpicture(g,400,100);
    			g.drawImage(fishimg, 50,100,200,200,this);
    		}
     
     
     
    	}
    }

  19. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: buttons that call methods

    Does it work like you want?

    This line should be removed:
    	Graphics g = this.getContentPane().getGraphics();

    The paint() method gets the Graphics object that should be used.


    What is the purpose of this code? It doesn't make sense:
                                    if (A == 1);
    			 	{
    			 		A = 1;
    If you don't understand my answer, don't ignore it, ask a question.

  20. #20
    Junior Member
    Join Date
    Nov 2012
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: buttons that call methods

    Quote Originally Posted by Norm View Post
    Does it work like you want?

    This line should be removed:
    	Graphics g = this.getContentPane().getGraphics();

    The paint() method gets the Graphics object that should be used.


    What is the purpose of this code? It doesn't make sense:
                                    if (A == 1);
    			 	{
    			 		A = 1;

    It doesn't do what I want it just constantly prints the methods now no matter what. The purpose of that code was because I wasn't sure what to put in there I was testing and playing to see if I could get it do anything.

  21. #21
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: buttons that call methods

    it just constantly prints the methods
    Can you explain and show what is printed? I don't know what "prints the methods" means.

    It doesn't do what I want
    Can you explain what it should do?


    There should not be code in the paint() method that changes the layout:
    		change.setSize(150, 50);
    		change.setLocation(d.width - 600, d.height - 50);
     
    		color.setSize(150, 50);
    		color.setLocation(d.width - 150, d.height - 50);
     
    		print.setSize(150, 50);
    		print.setLocation(d.width - 600, d.height - 400);
     
    		surprise.setSize(160, 50);
    		surprise.setLocation(d.width - 160, d.height - 400);

    Move that code out of the paint() method.
    If you don't understand my answer, don't ignore it, ask a question.

  22. #22
    Junior Member
    Join Date
    Nov 2012
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: buttons that call methods

    When I click button 4 it should print my method for drawpicture and drawImage but instead it just prints both methods all the time instead of only when the button is clicked.


    methods.jpg

  23. #23
    Junior Member
    Join Date
    Nov 2012
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: buttons that call methods

    does this make more sense? int A = 0 unless button 4 is pushed turning A into a 1 allowing the if statement to run
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.Random;
     
    //Cole Williams 
    //Making smiles and colors with buttons 
     
    public class williamsclab10 extends JApplet implements ActionListener
    {
     
    	Image fishimg;	            											
    	String imgFile = "../images/fish.gif";
     
    	TextField text = new TextField(10);
    	int number = -1;
     
    	private static Random background = new Random();
     
    	private JButton color;
     
    	private JButton print;
     
    	private JButton change;
     
    	private JButton surprise;
     
    	int count = 0;
    	int A = 0;
    	int B = 0;
    	int C = 0;
    	int D = 0;
     
    	boolean vis = false;
    	boolean showmypicture = false;
     
     
    	public void init() {
    		setSize(600, 400);
     
    		Container content = getContentPane();
     
    		int red = Math.abs(background.nextInt()) % 256;
    		int green = Math.abs(background.nextInt()) % 256;
    		int blue = Math.abs(background.nextInt()) % 256;
    		getContentPane().setBackground(new Color(red, green, blue));
     
    		fishimg = getImage(getCodeBase(), imgFile);
     
    		setLayout(new FlowLayout());
     
    		text.addActionListener(this);
    		add(text);
     
    		color = new JButton("Change the color");
    		add(color);
    		color.addActionListener(this);
     
    		print = new JButton("Print my picture!");
    		add(print);
    		print.addActionListener(this);
     
    		change = new JButton("Change the picture!");
    		add(change);
    		change.addActionListener(this);
     
    		surprise = new JButton("Click here for a surpise!");
    		add(surprise);
    		surprise.addActionListener(this);
     
    	}
     
     
     
    	public void actionPerformed(ActionEvent a) 
     
     
    	{		
     
    	// button 1 background change
    		if (a.getSource() == color) 
    		{
     
    			int red = Math.abs(background.nextInt()) % 256;
    			int green = Math.abs(background.nextInt()) % 256;
    			int blue = Math.abs(background.nextInt()) % 256;
     
    			getContentPane().setBackground(new Color(red, green, blue));
    		}
     
    		//button 2 prints picture centered 
    		else if (a.getSource() == print)
    		{
     
    		}
     
    			//button 3 picture toggling 
    		else if (a.getSource() == change)
     
    				{
    					vis = true;
     
    					showmypicture =! showmypicture;	
     
    				} 
     
    				//button 4 surprise
    		else if (a.getSource() == surprise)
    				{
    			 	A = 1;				
    				}
     
     
    	}
     
    	private void drawpicture(Graphics g, int x, int y)
    	{
    		// tail
    		g.setColor(Color.blue);
    		g.drawLine(x, y, x + 30, y);
    		g.drawLine(x + 30, y, x + 30, y + 30);
    		g.drawLine(x + 30, y + 30, x, y);
     
    		// body
    		g.setColor(Color.blue);
    		g.drawOval(x + 20, y + -33, 40, 40);
    		g.setColor(Color.blue);
    		g.drawOval(x + 36, y + -32, 10, 10);
     
    	}
     
     
    	public void paint(Graphics g) {
    		super.paint(g);
     
    		Dimension d = getSize();
     
    		text.setLocation(d.width / 2 - 30, 30);
     
    		int centerX = d.width / 2;
    		int centerY = d.height / 2;
    		int widthOfPicture = 600;
    		int heightOfPicture = 600;
    		int x = centerX - widthOfPicture / 2;
    		int y = centerY - heightOfPicture / 2;
     
     
    		change.setSize(150, 50);
    		change.setLocation(d.width - 600, d.height - 50);
     
    		color.setSize(150, 50);
    		color.setLocation(d.width - 150, d.height - 50);
     
    		print.setSize(150, 50);
    		print.setLocation(d.width - 600, d.height - 400);
     
    		surprise.setSize(160, 50);
    		surprise.setLocation(d.width - 160, d.height - 400);
     
    		if (A == 1);
    		{
    			drawpicture(g,400,100);
    			g.drawImage(fishimg, 50,100,200,200,this);
    		}
     
    	}
    }

  24. #24
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: buttons that call methods

    You need to remove the code from the paint() method that changes components' sizes and locations.

    Does the code work now? If not please explain.
    If you don't understand my answer, don't ignore it, ask a question.

  25. #25
    Junior Member
    Join Date
    Nov 2012
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: buttons that call methods

    where should my buttons go then?

    Also it does not work when I remove the code for my button locations. It still displays the methods all the time, also when I change the background I can no longer see my method and clicking to reprint them doesn't bring them back.

Page 1 of 2 12 LastLast

Similar Threads

  1. call by value and call by reference
    By Appu14 in forum The Cafe
    Replies: 1
    Last Post: September 2nd, 2012, 06:58 AM
  2. Why and where abstract methods & classes and static methods are used?
    By ajaysharma in forum Object Oriented Programming
    Replies: 7
    Last Post: July 14th, 2012, 01:16 AM
  3. Help with actionListener and buttons
    By jm24 in forum AWT / Java Swing
    Replies: 10
    Last Post: February 11th, 2012, 01:50 PM
  4. Call by Reference & Call by Value
    By Lokesh in forum Java Theory & Questions
    Replies: 1
    Last Post: February 21st, 2011, 01:19 PM
  5. Need more buttons!
    By GotWankel? in forum AWT / Java Swing
    Replies: 0
    Last Post: October 5th, 2009, 01:08 AM