Results 1 to 6 of 6
  1. #1
    tamer is offline Member
    Join Date
    Apr 2010
    Posts
    3
    Rep Power
    0

    Default Traffic Organizing In JAVA2D Problems

    hi everybody,
    please try to understand me becuase my english is not so good..

    i study in Damasucs university,IT..

    i have a project : Traffic Organizing

    i used java2D to draw Map

    i made very good work until now

    but I have some problems:

    1-how can i save the new map (that the user draw it)..

    2-when the user load an image from PC some parts of it disapear and after seconds it apears or after click on some part on screen >>> how can make it apear always ...

    this the code :

    Java Code:
     try{
          Graphics2D g2=(Graphics2D)jPanel6.getGraphics();
          File file = new File( "src/Maps/Capture.jpg" );
          FileInputStream inputStream = new FileInputStream( file );
          JPEGImageDecoder decoder =JPEGCodec.createJPEGDecoder( inputStream );
          BufferedImage image = decoder.decodeAsBufferedImage();
    
          g2.drawImage(image, 0,0,image.getWidth(), image.getHeight(),this);
    }

    and this the code of dialog to load the image :
    Java Code:
     try {
                JFileChooser  jFileChooser1=new JFileChooser();
                int response = jFileChooser1.showOpenDialog( this );
                if (response==JFileChooser.APPROVE_OPTION)
                {
                  File FileName =jFileChooser1.getSelectedFile();
                  Image_Loaded_Name=FileName.getAbsolutePath();
                  jLabel1.setText(FileName.getName());
    
                  FileInputStream inputStream = new FileInputStream(Image_Loaded_Name);
                  JPEGImageDecoder decoder =JPEGCodec.createJPEGDecoder( inputStream );
                  Back_Image=decoder.decodeAsBufferedImage();
                  
                }
            } catch(Exception e) {       }

    and this the code of drawing streets and map :
    Java Code:
     g4=(Graphics2D)jPanel4.getGraphics();
         g4.drawImage(Back_Image,0,0,Back_Image.getWidth(),Back_Image.getHeight(),null);
      double dx,dy;
         stroke1 = new BasicStroke(6,BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 0,dashPattern1, 0);
         stroke2 = new BasicStroke(1,BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 0,dashPattern, 0);
    
    for(int i=0;i<counter-1 ;i++)
            {
            if (points[i][0]!=-10000000&&points[i+1][0]!=-10000000)
                {
                     dx=Math.abs(points[i][0]-points[i+1][0]);
                     dy=Math.abs(points[i][1]-points[i+1][1]);
                     g4.setColor(Color.DARK_GRAY);
                      g4.setFont(new Font("",2,16));
                     g4.setStroke(stroke1);
                     g4.draw(new Line2D.Double(points[i][0],points[i][1],points[i+1][0],points[i+1][1]));
                     
                     g4.setStroke(stroke2);
                     g4.setColor(Color.white);
                     g4.draw(new Line2D.Double(points[i][0],points[i][1],points[i+1][0],points[i+1][1]));
    
                }
            }
    
          for (int i=0;i<node_counter  ;i++)
             {      g4.setColor(Color.blue);
                    Ellipse2D e1=new Ellipse2D.Float(points_circle[i][0],  points_circle[i][1], radius_node, radius_node);
                    Ellipse2D e2=new Ellipse2D.Float(points_circle[i][0]+2,  points_circle[i][1]+2, radius_node-4, radius_node-4);
                    g4.draw(e1);
                    g4.draw(e2);
                    
             }
    3- when i resize the window the graphics also disapear >>
    :confused::confused:

    please try to help me:o
    Last edited by tamer; 04-23-2010 at 10:18 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Read the Sun tutorials on using graphics in Swing and Java2D as this is not how you're supposed to paint. You'll want to instead use the paintComponent method, and the tutorials will show you how. Much luck.

  3. #3
    tamer is offline Member
    Join Date
    Apr 2010
    Posts
    3
    Rep Power
    0

    Default

    I tried the paint component before and i tried again >>>
    it didn't work >>>

    ok..... look the problem that when i open dailog (load,save) some part of the map(i.e the graphics in the panel) disapear ...

    is there any other solution...

    thanks for helping me...

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by tamer View Post
    is there any other solution...
    No. The tutorials describe the correct way to do graphics.

  5. #5
    tamer is offline Member
    Join Date
    Apr 2010
    Posts
    3
    Rep Power
    0

    Default

    ohhhh it's done .....

    thanx the problem solved by this code
    Java Code:
    @Override
    public void paint( Graphics g)
    {
        super.paintComponents(g);
        Draw();
    
    
    }
    :):)
    thanx very much ...
    Last edited by tamer; 04-26-2010 at 12:18 AM.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by tamer View Post
    ohhhh it's done .....

    thanx the problem solved by this code
    Java Code:
    @Override
    public void paint( Graphics g)
    {
        super.paintComponents(g);
        Draw();
    
    
    }
    :):)
    thanx very much ...

    This looks like dangerous code that I suggest you fix quickly. You should not be calling the super.paintComponent method from within a paint override. Instead I suggest that you override paintComponent and call the super paintComponent method from within it. Ignore this advice at your own risk. Best of luck.

Similar Threads

  1. Help with my traffic light program
    By topps in forum New To Java
    Replies: 4
    Last Post: 04-09-2010, 03:56 PM
  2. organizing elements in the jpanel/jbox
    By okabeer in forum AWT / Swing
    Replies: 7
    Last Post: 04-15-2009, 01:42 PM
  3. Generate IP/Ethernet traffic
    By vincent2001@gmail.com in forum New To Java
    Replies: 0
    Last Post: 08-22-2008, 11:08 PM
  4. organizing larger application code / modules
    By zenner in forum Advanced Java
    Replies: 5
    Last Post: 07-28-2008, 10:18 AM
  5. Traffic simulator
    By dirtycash in forum New To Java
    Replies: 1
    Last Post: 03-12-2008, 04:59 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
  •