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

Thread: Help With Applets

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Help With Applets

    Hi im using applet for my project to draw road maps with the latitude longitude information.But im having a problem in tat.my values read
    minX=40.1128
    maxX=40.1150
    minY=30.1145
    maxY=30.1138

    when i draw a line i get only a dot.can i give my own scale to the applet or do u have any other ideas ???plz help thanks ..............


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Help With Applets

    Without an SSCCE one can only guess that you are trying to draw floating point values to a panel which can only draw integers.

  3. #3
    Junior Member
    Join Date
    Jan 2011
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help With Applets

    well yeah tats my problem i have sorted out the quest of drawing lines with floating points............. but for a line with a very "small distance" do u have any idea of drawing it?..........can we change the pixel spacing or any other ideas????
    Last edited by mukilan; January 30th, 2011 at 11:55 AM.

  4. #4
    Junior Member
    Join Date
    Jan 2011
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help With Applets

    This is my code

     import java.sql.*;
     import java.sql.DriverManager;
     import java.sql.ResultSet;
     import java.awt.geom.Line2D;
     import java.awt.Graphics2D;
     import java.sql.Statement;
     import java.applet.*;
     import java.awt.*;
     
     public class ConnectSQLite extends Applet
     {
          int x;
      float y;
      Graphics gr;
      Connection connection = null;
         ResultSet resultSet = null;
         Statement statement = null;
     
     
     
     
        try
         {
             Class.forName("org.sqlite.JDBC");
             connection = DriverManager.getConnection("jdbc:sqlite:///home/student/Desktop//roads.db");
             statement = connection.createStatement();
             resultSet = statement
                     .executeQuery("SELECT * FROM coord");
     
             while (resultSet.next())
             {
                  System.out.println("id:"+ resultSet.getString("id"));
                  System.out.println("minX:"+ resultSet.getString("minX"));
                  System.out.println("minY:"+ resultSet.getString("minY"));
     
                   float x1=Float.parseFloat(resultSet.getString("minX"));
                   float y1=Float.parseFloat(resultSet.getString("minY"));
                   float x2=Float.parseFloat(resultSet.getString("maxX"));
                   float y2=Float.parseFloat(resultSet.getString("maxY"));
     
    		   //Drawing lines with floating point coordinates
     
     
                   BasicStroke ll=new BasicStroke(2.0f);
     
                   Graphics2D g2=(Graphics2D)this.getGraphics();
                   g2.setStroke(ll);
                   g2.draw(new Line2D.Float(x1*Applet.WIDTH, y1*Applet.HEIGHT, x2*Applet.WIDTH, y2*Applet.HEIGHT));
     
              }
         }
         catch (Exception e)
         {
             javax.swing.JOptionPane.showMessageDialog(null,e);
         }
     
     
     
     }

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Help With Applets

    One post is enough....your other post has been deleted. For future reference, please do not post your question twice to the forums.

    Do not call getGraphics to draw to a Component. Override the paint method (for awt) or paintComponent (for Swing) and do your drawing there. If you have small lines, then scale the drawing by either altering the values, or calling graphics.scale

  6. #6
    Junior Member
    Join Date
    Jan 2011
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help With Applets

    sorry that was my first post so i i was not familiar with thread creation...

    thanks for ur reply ill look into it and reply

  7. #7
    Junior Member
    Join Date
    Jan 2011
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help With Applets

    If you have small lines, then scale the drawing by either altering the values, or calling graphics.scale

    hi i have tried scaling but i get divide by zero exception.....can u plz give an elaborate idea???

  8. #8
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Help With Applets

    Quote Originally Posted by mukilan View Post
    hi i have tried scaling but i get divide by zero exception.....can u plz give an elaborate idea???
    I recommend posting an SSCCE demonstrating the problem. Division by zero seems more like a logic problem that can't be resolved without a code example.

  9. #9
    Junior Member
    Join Date
    Jan 2011
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help With Applets

    ok this is my test data

    id:1000
    minX:43.0999984741211
    minY:80.0999984741211
    minX:43.0999984741211
    minY:80.1019973754883
    id:1001
    minX:43.101001739502
    minY:80.0999984741211
    minX:43.101001739502
    minY:80.1019973754883
    id:1002
    minX:43.1004981994629
    minY:80.0999984741211
    minX:43.100700378418
    minY:80.1019973754883
    id:1003
    minX:43.0999984741211
    minY:80.0999984741211
    minX:43.1004981994629
    minY:80.0999984741211
    id:1004
    minX:43.1004981994629
    minY:80.0999984741211
    minX:43.101001739502
    minY:80.0999984741211
    id:1005
    minX:43.0999984741211
    minY:80.1019973754883
    minX:43.100700378418
    minY:80.1019973754883
    id:1006
    minX:43.100700378418
    minY:80.1019973754883
    minX:43.101001739502
    minY:80.1019973754883


    and my code is
     import java.sql.*;
     import java.sql.DriverManager;
     import java.sql.ResultSet;
     import java.awt.geom.Line2D;
     import java.awt.Graphics2D;
     import java.sql.Statement;
     import java.applet.*;
     import java.awt.*;
     
     public class ConnectSQLite extends Applet
     {
          int x;
      float y;
      Graphics gr;
      Connection connection = null;
         ResultSet resultSet = null;
         Statement statement = null;
     
        @Override
      public void init()
      {
    try
         {
             Class.forName("org.sqlite.JDBC");
             connection = DriverManager.getConnection("jdbc:sqlite:///home/foss/Desktop//roads.db");
             statement = connection.createStatement();
             resultSet = statement
                     .executeQuery("SELECT * FROM coord");
     
     
         }
         catch (Exception e)
         {
             javax.swing.JOptionPane.showMessageDialog(null,e);
         }
      }
     
     
        @Override
        public void paint(Graphics g)
        {
                super.paintComponents(g);
            try{
             while (resultSet.next())
             {
                  //omit this while running as i have procided the out put of this as the testdata.
     
                  System.out.println("id:"+ resultSet.getString("id"));
                  System.out.println("minX:"+ resultSet.getString("minX"));
                  System.out.println("minY:"+ resultSet.getString("minY"));
                  System.out.println("minX:"+ resultSet.getString("maxX"));
                  System.out.println("minY:"+ resultSet.getString("maxY"));
     
                   double x1=Double.parseDouble(resultSet.getString("minX"));
                   double y1=Double.parseDouble(resultSet.getString("minY"));
                   double x2=Double.parseDouble(resultSet.getString("maxX"));
                   double y2=Double.parseDouble(resultSet.getString("maxY"));
                 BasicStroke ll=new BasicStroke(1);
     Graphics2D g2=(Graphics2D)g;
                   g2.setStroke(ll);
     
     //this function doesnt work for me as i wanted plz provide me with a appropriate value for my test data.
                   g2.scale(2.0,2.0);
                   g2.draw(new Line2D.Double(x1, y1, x2, y2));
     
              }
            }
            catch(Exception e)
            {
                javax.swing.JOptionPane.showMessageDialog(null,e);
            }
     
            }  
        }

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

    Default Re: Help With Applets

    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.

  11. #11
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Help With Applets

    What you have posted is not an SSCCE as requested, since we don't have access to that database file (and even if we did, an SSCCE about drawing should not include code for reading data from anywhere).

    You haven't even really told us what you're trying to do, other than draw a line. You say that the function did not work for you as you wanted- how does it work instead? What did you expect? What does the documentation say?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  12. #12
    Junior Member
    Join Date
    Jan 2011
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help With Applets

    @ kevin

    i have provided a test data for my problem in my second post.im just trying to read the road coordinates from a file(latitude,longitude endpoints) and trying to plot it in my screen.since the distance between the two points are very small im just getting a dot instead of a road network. hope this helps u to figure out wat im saying........

  13. #13
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Help With Applets

    Pixels are pixels, 1=1 and 1.0001=1. So providing floating points that close together will boil down to a single point in a real application.You need to transform and scale the coordinates appropriately so they fit and scale to what you want, and doing so is highly dependent upon the result you want. Have a look at this turorial Java2d: Transformations

  14. The Following User Says Thank You to copeg For This Useful Post:

    mukilan (February 2nd, 2011)

Similar Threads

  1. Applets progress bar not increasing
    By Edvinauskas in forum Java Servlet
    Replies: 8
    Last Post: November 22nd, 2010, 07:04 PM
  2. Drawing in applets. Tell me why this way is wrong.
    By Asido in forum Java Theory & Questions
    Replies: 9
    Last Post: August 1st, 2010, 01:55 PM
  3. Applets
    By santiagomez in forum Java Theory & Questions
    Replies: 2
    Last Post: July 31st, 2010, 04:05 PM
  4. Help needed with applets
    By Brt93yoda in forum What's Wrong With My Code?
    Replies: 16
    Last Post: July 12th, 2010, 07:36 PM
  5. Learn applets in java
    By mohsendeveloper in forum Java Applets
    Replies: 2
    Last Post: June 25th, 2009, 02:50 PM

Tags for this Thread