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 3 of 3

Thread: Looking for Assistance with Graphing using Drawing Panel

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Looking for Assistance with Graphing using Drawing Panel

    import java.awt.*;
     
    public class Program5 {
     
    	public static void main(String[] args) {
    		DrawingPanel panel = new DrawingPanel(300,500);
    		panel.setBackground(Color.white);
    		Graphics g = panel.getGraphics();
    		g.setFont(new Font("Helvetica", Font.PLAIN, 12));
    		double ball = 10;
    			for(int i=1; i<=20; i++){
    				ball=ball*0.8;	
    			g.drawLine(i*10, (int)(425-ball*50), i, i);
    	}
    			for(int j=0; j<=8; j++){
    			g.drawString(j+ "Meters", 225, 420-j*50);
    		}
    			g.drawString("Bounce Number", 75, 455);
    			g.drawString("1", 10, 440);
    			for(int k=1; k<=4; k++){
    			g.drawString((k*5)+"", k*50, 440);
    		}
     
    }
    }

    I'm fairly new to Java and am having a hard time with this particular assignment. The program runs and shows up on the drawing panel fine but I can't get the lines quite right.
    Capture.jpg

    These are what the lines should look like. Where should I start to fix it? Thank you very much for reading.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Looking for Assistance with Graphing using Drawing Panel

    You are not using the standard technique for drawing.
    See the tutorial: Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    Bascially create a class that extends JPanel, override the paintComponent() method and add all drawing code there. Create a JFrame and add the new class to that.

    Where is the class: DrawingPanel defined?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Looking for Assistance with Graphing using Drawing Panel

    Welcome to the forum! Thanks for taking the time to learn how to post code correctly. If you haven't already, please read this topic to learn other useful info for new members.

    It appears that you're using "Building Java Programs: A Back to Basics Approach, by Stuart Reges and Marty Stepp." Their use of custom graphics "helper" classes that do most of the work required to draw graphics. The DrawingPanel class that you're using is not core Java, it's a class the authors of the book wrote to "help" students using the book.

    Most here know standard or core Java and the classes distributed by Oracle with the Java SDK. To help you, we'd have to learn the Reges and Stepp approach or already know it. Be patient, and someone may come along who can help, but I haven't seen this approach before on this forum, so it may be a long wait.

Similar Threads

  1. In need of assistance!
    By Nexcit in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 11th, 2014, 05:24 AM
  2. Help with bouncing on a panel
    By hellhunt in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 9th, 2012, 08:28 PM
  3. <b>TRIGONOMETRIC FUNCTION GRAPHING</b>
    By XNEE2 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 2nd, 2012, 01:00 AM
  4. Need some assistance please
    By JavaPhish in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 25th, 2012, 10:00 AM