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 2 of 2 FirstFirst 12
Results 26 to 38 of 38

Thread: buttons that call methods

  1. #26
    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

    Split the GUI area into sections and add separate JPanels to each section. Put the buttons in one JPanel and do the drawing in another JPanel.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: buttons that call methods

    Can't we never learned JPanels, not sure what the teacher would think of that....

  3. #28
    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

    How does the teacher expect you to do drawing and have components shown on the same screen without using JPanels?
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: buttons that call methods

    I run my programs in eclipse and they run in java applets

  5. #30
    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

    Did you read the tutorial I linked to in post#11?
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: buttons that call methods

    Yes it had to do with JApplets

  7. #32
    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 was more about JPanels that JApplets
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: buttons that call methods

    Sorry thats what I meant.

  9. #34
    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

    I have no other suggestions other than that you use separate panels for components and for drawing.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: buttons that call methods

    this is where I am at now buttons working they call the methods and repaint. How do I setup button 3 to flip back and forth between my img file and my drawpicture method

    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;
     
     
    	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)
    		{
    			C=1;
    		}
     
    			//button 3 picture toggling 
    		else if (a.getSource() == change)
     
    				{
    					vis = true;
    					showmypicture =! showmypicture;	
    					  B = 1;
    					   count++;
    					   repaint();
    				} 
     
    				//button 4 surprise
    		 if (a.getSource() == surprise)
    				{
    			   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);
    		}
    		else if (B==1)
    		{
    			drawpicture(g,400,100);
     
    		}
     
    	}
     
    }

  11. #36
    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

    How do I setup button 3 to flip back and forth between
    The listener should set some values that the paint() method can use to determine what is to be drawn.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: buttons that call methods

    I still haven't gotten the toggling between the two methods to work this is turning my hair grey

  13. #38
    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

    Try working out the logic by writing a list of the steps (pseudo code) that the program needs to do to toggle between the two methods. When you get a good design (we'll help) then write the code to do it.
    If you don't understand my answer, don't ignore it, ask a question.

Page 2 of 2 FirstFirst 12

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