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: How to Simplify my code ?

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question How to Simplify my code ?

    These points are cubic curves coordinates used by java Graphics2D libraries to draw high quality shapes such as svg the path took from Inkscape Please Help me programmatically how to put these points in array,collection to remove the hardcode by reading each point to read them all at once instead of substitute each curve individually the code is :

    PHP Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.geom.*;
    import java.util.*;
    import javax.swing.*;

    public class 
    Dal extends Frame {

        public static 
    void main(String[] a){
           (new 
    Dal()).setVisible(true);
            
    GeneralPath g=new GeneralPath();
        }

        
    Dal(){
            
    super("Test");
            
    setSize(500500);
            
    addWindowListener(new WindowAdapter() {
                public 
    void windowClosing(WindowEvent e) {
                    
    System.exit(0);
                }
            });
        }

        @
    SuppressWarnings("unused")
        public 
    void paint(Graphics g) {
             
    Graphics2D g2d = (Graphics2Dg;
             
    g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATIONRenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
             
    g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROLRenderingHints.VALUE_STROKE_PURE);
             
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASINGRenderingHints.VALUE_ANTIALIAS_ON);

             
    GeneralPath path=new GeneralPath();
             
    path.moveTo(457.61616,470.82943 );               
                
    /* */
             
    path.curveTo(458.41016,425.70843 ,427.74316,392.55343 ,403.93516,370.91243 );
             
    path.curveTo(399.48516,366.83843 ,398.54916,368.02743 ,397.41516,372.27043 );
             
    path.curveTo(394.75116,382.25643 ,392.96616,392.69543 ,391.09516,402.03043 );
             
    path.curveTo(390.35916,405.62343 ,389.79116,406.92443 ,392.62616,409.52743 );
             
    path.curveTo(406.00316,421.83343 ,442.19716,458.07143 ,444.89016,482.76843 );
             
    path.curveTo(431.76716,528.31343 ,393.39116,574.56743 ,350.22516,594.56743 );
             
    path.curveTo(316.63916,610.12643 ,278.88716,614.34043 ,242.18316,610.35243 );
             
    path.curveTo(232.12112,609.27843 ,228.38012,619.29143 ,238.47016,621.92243 );
             
    path.curveTo(274.01216,631.28543 ,320.32416,637.73643 ,356.57416,628.91043 );
             
    path.curveTo(420.03416,613.46343 ,456.48216,533.71643 ,457.61616,470.82943);  

             
    path.closePath();
             
    g2d.draw(path);
             
    g2d.fill(path);
        }       



  2. #2
    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: How to Simplify my code ?

    How were the hardcoded values determined? Can they be calculated or derived mathematically somehow, perhaps from an initial point using knowledge of the shape being drawn and its width, height, etc.?

  3. #3
    Junior Member
    Join Date
    Dec 2013
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Re: How to Simplify my code ?

    i want to remove the hardcode by reading each point, i thought about array to remove the complexity in my code,but i am not sure because path.curveTo() Adds a curved segment, defined by three new points, to the path by drawing a Bézier curve that intersects both the current coordinates and the specified coordinates (x3,y3), using the specified points (x1,y1) and (x2,y2) as Bézier control points. All coordinates are specified in double precision. Overrides: curveTo(...) in Path2D
    Parameters: x1 the X coordinate of the first Bézier control point y1 the Y coordinate of the first Bézier control point x2 the X coordinate of the second Bézier control point y2 the Y coordinate of the second Bézier control point x3 the X coordinate of the final end point y3 the Y coordinate of the final end point
    Any suggestion to simplify my code because i have other shapes the have so many coordinates ? And how to use collection class with Shapes?

    --- Update ---

    I got this answer
    "Create a Bezier class (inheriting from an abstract Shape), which holds all three co-ordinates. You can then build a collection of Bezier objects and create your graphics from that."
    How to implement that programmatically ?
    Thanks

  4. #4
    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: How to Simplify my code ?

    It appears you've posted the request for help in multiple places, known as cross posting. Cross posting is okay here, but it is requested that you provide a link to the other post(s).

    I recommend that you direct follow-up questions to the person who gave you the suggestion.

    You never answered my question, and here's another one: Do you mean to say that you do not know how to create a class with 3 fields for coordinates?

    If you already have the coordinates, why not copy/paste them to a file and read them from there? Data is data and you've given no valid reason to "shorten" the data, and you probably don't want to. However, handling the data programmatically can be generalized so that the program doesn't just work for a specific, hardcoded case.

  5. #5
    Junior Member
    Join Date
    Dec 2013
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How to Simplify my code ?

    Well, perhaps my question wasn't clear.
    I have all coordinates in database, how can i retrieve them from MySQL database and draw curves ?
    Thanks

  6. #6
    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: How to Simplify my code ?

    Well, perhaps my question wasn't clear.
    An understatement, but I assume your thinking evolved.

    There are several tutorials available on the web that show how to access modern databases from Java. You might start with Oracle's own tutorial on the subject.[COLOR="Silver"]

    And there was no reason to start another topic for the same thing.

Similar Threads

  1. Replies: 5
    Last Post: June 17th, 2013, 06:54 PM
  2. Replies: 3
    Last Post: April 27th, 2013, 07:19 AM
  3. Replies: 7
    Last Post: January 24th, 2013, 10:41 AM
  4. Replies: 5
    Last Post: November 14th, 2012, 10:47 AM
  5. Replies: 3
    Last Post: September 3rd, 2012, 11:36 AM