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

Thread: Simple graphics practice on previous Java code

  1. #1
    Junior Member
    Join Date
    Jun 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Simple graphics practice on previous Java code

    I am leaning Java basics. I wrote code to produce the following output



    the image on the right. I have a problem to make things touch each other at the middle of the screen. i. e. line going from the left upper corner touch lines going from the right bottom corner. Also, the output should have the same look when resizing the window. My code( Java pastebin - collaborative debugging tool )and ( Java pastebin - collaborative debugging tool ). I am not so good in math. Can anyone here help me to get the required output?

    Thanks in advance


  2. #2
    Junior Member
    Join Date
    Jun 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Sorry I forgot to paste the code

    //Code Test Class
     
    import javax.swing.JFrame;
     
    public class DrawPanelTest
    {
     
    public static void main( String args[] )
    {
     
    // create a panel that contains our drawing
     
    DrawPanel panel = new DrawPanel();
     
    // create a new frame to hold the panel
     
    JFrame application = new JFrame();
     
    // set the frame to exit when it is closed
     
    application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
     
    application.add( panel ); // add the panel to the frame
     
    application.setSize( 250, 250 ); // set the size of the frame
     
    application.setVisible( true ); // make the frame visible
     
    } // end main
     
    } // end class DrawPanelTest

    //code Draw panel
     
    import java.awt.Graphics;
     
    import javax.swing.JPanel;
     
    public class DrawPanel extends JPanel
     
    {
     
    // draws an X from the corners of the panel
     
    public void paintComponent( Graphics g )
     
    {
     
    // call paintComponent to ensure the panel displays correctly
     
    super.paintComponent( g );
     
    int width = getWidth(); // total width
     
    int height = getHeight(); // total height
     
    int i = 0, j = 0, k = 0;
     
    for ( i = 1; i <= 15 ; i++)
     
    {
     
    g.drawLine( 0, 0, j, height - k);
     
    j += width / 15;
     
    k += height / 15;
     
    }
     
    j = 0;
     
    k = 0;
     
    for ( i = 1; i <= 15 ; i++)
     
    {
     
    g.drawLine( 0, height, j, k);
     
    j += width / 15;
     
    k += height /15;
     
    }
     
    j = 0;
     
    k = 0;
     
    for ( i = 1; i <= 15 ; i++)
     
    {
     
    g.drawLine( width, 0, width - j, height - k);
     
    j += width / 15;
     
    k += height / 15;
     
    }
     
    j = 0;
     
    k = 0;
     
    for ( i = 1; i <= 15 ; i++)
     
    {
     
    g.drawLine( width, height, width - j, k);
     
    j += width / 15;
     
    k += height / 15;
     
    }
     
    } // end method paintComponent
     
    } // end class DrawPanel
    Last edited by JavaPF; June 17th, 2009 at 12:28 PM. Reason: Please don't paste line numbers!

  3. #3
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Location
    Wales, Bangor & England, Warwickshire
    Posts
    820
    My Mood
    Cynical
    Thanks
    7
    Thanked 104 Times in 90 Posts

    Default Re: Help with simple graphics practice

    Welcome to the forums

    The lines do meet in that picture :S, please use code tags, [ code ][/ code ] without the spaces also would you be as kind as to not use lines numbers -_-

    Chris
    chris[at]javaprogrammingforums[dot]com

    Prifysgol Bangor University, North Wales

  4. #4
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: Help with simple graphics practice

    Is the image on the left your example but you need to make it look like the image on the right?
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  5. #5
    Junior Member
    Join Date
    Jun 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with simple graphics practice

    The required output is the image on the right

  6. #6
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: Help with simple graphics practice

    I just compiled the code you submitted amrawad_85 and I get the same result as the image on the right!

    I don't see a problem here?
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

Similar Threads

  1. How to Write a simple XMPP (Jabber) client using the Smack API
    By JavaPF in forum Java Networking Tutorials
    Replies: 35
    Last Post: August 5th, 2014, 01:59 AM
  2. Someone please help me write a simple program!!!
    By ocean123 in forum Loops & Control Statements
    Replies: 3
    Last Post: June 14th, 2009, 10:46 PM
  3. [SOLVED] Java exception "result already defined"
    By rptech in forum Object Oriented Programming
    Replies: 3
    Last Post: May 20th, 2009, 06:48 AM
  4. Problem while programming a simple game of Car moving on a road
    By rojroj in forum Java Theory & Questions
    Replies: 3
    Last Post: April 2nd, 2009, 11:24 AM
  5. Java program to write game
    By GrosslyMisinformed in forum Paid Java Projects
    Replies: 3
    Last Post: January 27th, 2009, 03:33 PM