Results 1 to 2 of 2
  1. #1
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Talking Why doesn't the panel update unless I drag the edges?

    Hai!
    Im like total newbie and I'v finaly got a feeling on how to import images =)
    Weee, took me days to find a good example that didnt use 1 quadrilion lines to explain.
    But anyways, I'v made this gui thing. Just a window and a picture.
    Then I got a loop running.
    This loop will change the X and Y values and repaint the picture.
    But the problem is, you can see that the image is moving unless you resize the window, but the x andy is changing so if u wait a long time, then the picture (when drag the edges) will have moves far down.

    So heres my code:
    PHP Code:
    /**
     * @(#)TestingArea.java
     *
     * TestingArea application
     *
     * @author 
     * @version 1.00 2009/11/2
     */
    import javax.swing.*;
    import javax.imageio.*;
    import java.awt.*; 
    import java.io.*;
    import java.awt.image.*;
    import java.awt.image.BufferedImage;
    import java.util.*;
     
    public class TestingArea {
        static Vector <Image> list = new Vector<Image>();
        static int X = 0;
        static int Y = 0;
        public static void main(String[] args) {
        	
        	// TODO, add your application code
        	gameFrame gf = new gameFrame();
        	while (true){
        		X += 10;
        		Y += 10;
        		
        		gf.add(new paintIt());
        		try{
     				 //do what you want to do before sleeping
      				Thread.currentThread().sleep(1000);//sleep for 1000 ms
      				//do what you want to do after sleeptig
    			} catch(Exception ie){
    				//If this thread was intrrupted by nother thread 
    			}
        	}
        }
        
    }
    
    class gameFrame extends JFrame{
    	public gameFrame(){
    		
    		
    		
    		paintIt p = new paintIt();
    		add(p);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setSize(500,300);
    		setTitle("Image test");
    		setVisible(true);
    		
    	}
    }
    
    class paintIt extends JPanel{
    	public void paintComponent(Graphics g){
    		super.paintComponent(g);
    		try{
    		BufferedImage pic = ImageIO.read(new File("C:\\Users\\Andreas\\Bilder\\CNDF.png"));
    		TestingArea.list.add(pic);
    		g.drawImage(pic,TestingArea.X,TestingArea.Y,null);
    		}catch (Exception e){
    			e.printStackTrace();
    		}
    	}
    }
    Suggestion on ways/patterns to use for making a 2d game is also appriciated =)

  2. #2
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

Similar Threads

  1. Drag polygon
    By cassysumandak in forum New To Java
    Replies: 5
    Last Post: 03-10-2011, 07:30 PM
  2. Drag and Drop
    By carderne in forum New To Java
    Replies: 0
    Last Post: 08-31-2009, 10:18 AM
  3. Replies: 2
    Last Post: 04-14-2009, 11:01 PM
  4. Help with drag from panel
    By fernando in forum AWT / Swing
    Replies: 2
    Last Post: 08-07-2007, 11:19 PM
  5. Rectangle with rounded edges??
    By orchid in forum Java 2D
    Replies: 1
    Last Post: 05-10-2007, 03:31 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
  •