Results 1 to 4 of 4

Thread: change size

  1. #1
    lucasmcd is offline Member
    Join Date
    Sep 2010
    Posts
    1
    Rep Power
    0

    Default change size

    I was wondering how I would go about changing the size of the ovals/ "target" with the use of buttons, or the use of right and left arrows.
    Java Code:
    /**
     * @(#)Target.java
     *
     * Target Applet application
     *
     * @author
     * @version 1.00 2010/9/19
     */
    
    import java.awt.*;
    import java.applet.*;
    import javax.swing.*;
    
    public class Target extends Applet {
    
    	
        JButton big;
        JButton small;
    
        public void init() {
    
            setLayout (new FlowLayout());
            big = new JButton("Big");
            small = new JButton ("Small");
            add(big);
            add(small);
        }
    
    
    	public void paint(Graphics g) {
    		super.paint(g);
    		
    	//set background to black
    		g.setColor(Color.black);
    		g.fillRect(0,0,getWidth(),getHeight());
    		
    	// 10th ring of target
    		g.setColor(new Color(128,0,0));
    		g.fillOval(160,160, 190,190);
    	
    	// 9th ring of target
    		g.setColor(new Color(0,0,128));
    		g.fillOval(170,170, 170,170);
    	
    	// 8th ring of target
    		g.setColor(new Color(0,128,0));
    		g.fillOval(180,180, 150,150);
    
    	// 7th ring of target
    		g.setColor(Color.cyan);
    		g.fillOval(190, 190, 130,130);
    	
    	// 6th ring of target
    		g.setColor(Color.orange);
    		g.fillOval(200,200, 110,110);
    	
    	// 5th ring of target
    		g.setColor(new Color(128,128,128));
    		g.fillOval(210, 210, 90,90);
    
    	// 4th ring of target
    		g.setColor(Color.pink);
    		g.fillOval(220, 220, 70,70);
    	
    	// 3rd ring of target
    		g.setColor(Color.green);
    		g.fillOval(230,230, 50,50);
    
    	// 2nd ring of target
    		g.setColor(Color.BLUE);
    		g.fillOval(240,240, 30,30);
    	
    	//center of target
    		g.setColor(Color.RED);
    		g.fillOval(250,250, 10, 10);
    
    	}
    }
    Thanks for pointing me in the right direction!
    Last edited by lucasmcd; 09-19-2010 at 11:40 PM. Reason: Updated code with comments

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default

    Where is the button you want to use?
    Where is the "target"?
    Can you add comments to your code describing what it does?

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

    Default

    For starters, your code mixes AWT components (Applet) with Swing components (JButtons) which in general is a no-no. Instead use all Swing components such as a JApplet, then do your drawing in the paintComponent override method of a JPanel.

    If you want to change the parameters used to draw your ovals, then the parameters in your fillOval methods must be fields not fixed "magic" numbers.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default

    with the use of buttons
    You need to add action listeners to your two buttons. The listener will get control when you press the button and can set some variables that can be used by the paint method to change the size of the target ovals and then call repaint() to tell the JVM to call your paint method to draw then new target.

Similar Threads

  1. Changing JTableModel does not change JScrollpane size
    By stelzergil in forum New To Java
    Replies: 3
    Last Post: 10-19-2009, 02:14 AM
  2. change text size in all GUI components
    By itaipee in forum AWT / Swing
    Replies: 2
    Last Post: 08-07-2009, 04:59 AM
  3. Replies: 4
    Last Post: 02-22-2009, 06:31 AM
  4. Replies: 1
    Last Post: 06-06-2008, 07:55 AM
  5. Listener for JFrame size change
    By Thez in forum AWT / Swing
    Replies: 10
    Last Post: 02-14-2008, 03:10 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
  •