Results 1 to 16 of 16
  1. #1
    ptuckley is offline Member
    Join Date
    Dec 2010
    Posts
    11
    Rep Power
    0

    Default Using methods for graphics

    I apologise if this is wrong/obvious/the wrong place to put this/stupid but I could really do with some help. I started studying programming and we've been told to make a map of a region and use various techniques.

    I'm trying to make 3 'bars' which are essentially just circles, and I've been told to create them using methods. I don't understand how this works though because I don't want them all in the same place!

    This is the code I've come up with so far.

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import javax.swing.text.*;
    
    public class question1
    extends JFrame implements MouseListener
    	{
    		int xCentre = 350, yCentre = 350;
    		int stageWidth = 0, stageHeight = 0
    		int barWidth = 25, barHeight = 25;
    	
    			
    		public question1()
    			{
    				setSize(700, 700);
    				setVisible(true);
    				show();
    				addMouseListener(this);
    				
    			}
    		
    		public void mouseClicked(MouseEvent e)
    			{
    				int x = e.getX(), y = e.getY();
    				
    				System.out.println("Clicked at "  + x +" "+y);
    			}
    		public void mouseExited(MouseEvent e) {}
    		public void mouseEntered(MouseEvent e) {}
    		public void mouseReleased(MouseEvent e) {}
    		public void mousePressed(MouseEvent e) {}
    
    
    		public void boundary(Graphics g)
    			{
    				
    				g.drawLine(xCentre-150, yCentre+300, xCentre+150, yCentre+275); //1
    				g.drawLine(xCentre+150, yCentre+275, xCentre+100, yCentre-100); //2
    				g.drawLine(xCentre+100, yCentre-100, xCentre+300, yCentre-200); //3
    				g.drawLine(xCentre+300, yCentre-200, xCentre+100, yCentre-300); //4
    				g.drawLine(xCentre+100, yCentre-300, xCentre-150, yCentre-250); //5
    				//g.drawRect(xCentre-200, yCentre-250, 50, 550);	
    				g.drawArc(xCentre-175, yCentre-250, 50, 550,90,180); //6
    				
    			
    			}
    		public void bars(Graphics g)
    			{
    				g.drawOval(xCentre+150, yCentre-250, barWidth, barHeight);
    			}
    		
    		public void paint(Graphics g)
    			{
    				super.paint(g);
    				boundary(g);
    				bars(g);
    			}
    	}

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,038
    Rep Power
    10

    Default

    Create a method that draws a single "bar" at a specified location. Call that method three times with different arguments.

  3. #3
    ptuckley is offline Member
    Join Date
    Dec 2010
    Posts
    11
    Rep Power
    0

    Default

    Again, sorry if i sound stupid but how would i do that?

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,038
    Rep Power
    10

    Default

    Well you already have the methods, and you already have the code that draws something based on variables (xCentre and yCentre). So instead of referring to those global variables, add some parameters to the functions.

    For example, your bars function might look like this:

    Java Code:
    public void bars(Graphics g, barX, barY)
       {
          g.drawOval(barX+150, barY-250, barWidth, barHeight);
       }
    You could also parameterize barWidth and barHeight if they'll be changing as well.

  5. #5
    ptuckley is offline Member
    Join Date
    Dec 2010
    Posts
    11
    Rep Power
    0

    Default

    That's brilliant, thank you!

  6. #6
    ptuckley is offline Member
    Join Date
    Dec 2010
    Posts
    11
    Rep Power
    0

    Default

    Ok, I'm getting errors based on not being able to find the class barX or barY, can you help?

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import javax.swing.text.*;
    
    public class question1
    extends JFrame implements MouseListener
    	{
    		int xCentre = 350, yCentre = 350;
    		int stageWidth = 0, stageHeight = 0;
    		int barX=350, barY=350;
    		int barWidth = 25, barHeight = 25;
    	
    			
    		public question1()
    			{
    				setSize(700, 700);
    				setVisible(true);
    				show();
    				addMouseListener(this);
    				
    			}
    		
    		public void mouseClicked(MouseEvent e)
    			{
    				int x = e.getX(), y = e.getY();
    				
    				System.out.println("Clicked at "  + x +" "+y);
    			}
    		public void mouseExited(MouseEvent e) {}
    		public void mouseEntered(MouseEvent e) {}
    		public void mouseReleased(MouseEvent e) {}
    		public void mousePressed(MouseEvent e) {}
    
    
    		public void boundary(Graphics g)
    			{ 
    				
    				g.drawLine(xCentre-150, yCentre+300, xCentre+150, yCentre+275); //1
    				g.drawLine(xCentre+150, yCentre+275, xCentre+100, yCentre-100); //2
    				g.drawLine(xCentre+100, yCentre-100, xCentre+300, yCentre-200); //3
    				g.drawLine(xCentre+300, yCentre-200, xCentre+100, yCentre-300); //4
    				g.drawLine(xCentre+100, yCentre-300, xCentre-150, yCentre-250); //5
    				//g.drawRect(xCentre-200, yCentre-250, 50, 550);	
    				g.drawArc(xCentre-175, yCentre-250, 50, 550,90,180); //6
    				
    			
    			}
    
    		public void bars(Graphics g, barX, barY)
       			{
         				 g.drawOval(barX+150, barY-250, barWidth, barHeight);
       			}
    		
    		public void paint(Graphics g)
    			{
    				super.paint(g);
    				boundary(g);
    				bars(g);
    			}
    	}

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default

    public void bars(Graphics g, barX, barY)
    Huh?

    db

  8. #8
    ptuckley is offline Member
    Join Date
    Dec 2010
    Posts
    11
    Rep Power
    0

    Default

    I just used the code KevinWorkman suggested?

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default

    I see. Kevin would have (mis)typed that here, it's up to you to convert it to valid Java syntax. What do you think it should be? Look at just about any method that takes parameters for inspiration (or even the first parameter of your bars method).

    db

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default

    Additionally, don't override paint(...) of a top level window, override paintComponent(...) of a JPanel or JComponent.
    Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    db

  11. #11
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,776
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by ptuckley View Post
    I just used the code KevinWorkman suggested?
    That's what you deserve if you blindly copy and paste code suggestions without thinking. Parameters should have a type when you define a method; it should be:

    Java Code:
    public void bars(Graphics g, int barX, int barY) { ... }
    kind regards,

    Jos

    ps. and don't come back here telling us that your compiler whines about '...'
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,038
    Rep Power
    10

    Default

    Quote Originally Posted by ptuckley View Post
    I just used the code KevinWorkman suggested?
    Yeah, whoops, sorry. But did you figure it out following the others' advice?

  13. #13
    ptuckley is offline Member
    Join Date
    Dec 2010
    Posts
    11
    Rep Power
    0

    Default

    I'll be honest, didn't understand a word of what anyone's trying to tell me.

    Thanks for the help, i appreciate it. But I think I'll just go ask my teacher.

  14. #14
    ptuckley is offline Member
    Join Date
    Dec 2010
    Posts
    11
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    That's what you deserve if you blindly copy and paste code suggestions without thinking. Parameters should have a type when you define a method; it should be:

    Java Code:
    public void bars(Graphics g, int barX, int barY) { ... }
    kind regards,

    Jos

    ps. and don't come back here telling us that your compiler whines about '...'
    Just a last ditch attempt, but I defined the parameters when I defined everything else, is that wrong?

  15. #15
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,038
    Rep Power
    10

    Default

    Quote Originally Posted by ptuckley View Post
    Just a last ditch attempt, but I defined the parameters when I defined everything else, is that wrong?
    What's wrong is that I missed the types of the parameters when I typed it into the forum editor. Jos pointed out what the function definition should look like instead. The rest should work just fine.

    I'd suggest you post an SSCCE that demonstrates where you're stuck. But going to the instructor isn't a bad idea either.

  16. #16
    ptuckley is offline Member
    Join Date
    Dec 2010
    Posts
    11
    Rep Power
    0

Similar Threads

  1. Replies: 8
    Last Post: 04-17-2012, 12:00 AM
  2. using multiple methods for graphics
    By jforce93 in forum Advanced Java
    Replies: 3
    Last Post: 04-25-2010, 07:05 PM
  3. Help me with graphics
    By 7oclock in forum New To Java
    Replies: 12
    Last Post: 04-05-2009, 12:20 AM
  4. Replies: 4
    Last Post: 04-03-2009, 07:36 AM
  5. Trying to use Graphics draw methods
    By Lang in forum SWT / JFace
    Replies: 5
    Last Post: 03-26-2008, 06:49 AM

Posting Permissions

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