Results 1 to 3 of 3
  1. #1
    Mincet is offline Member
    Join Date
    Feb 2017
    Posts
    1
    Rep Power
    0

    Default My Images Are Looping

    Hello!

    I just started using java not too long ago, and am having trouble with my code. My code should make a starry background appear on the screen, however, it appears to cycle through itself for some reason. Here is the raw code itself:

    Java Code:
    import java.awt.*;
    
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    
    public class Test extends JFrame {
    	public static void main(String[] args){
    		
    		DisplayMode dm = new DisplayMode(800,600,16, DisplayMode.REFRESH_RATE_UNKNOWN);
    		Test b = new Test();
    		b.run(dm);
    		
    	}
    	private Screen s;
    	private Image bg;
    	private Image pic;
    	private boolean loaded;
    	
    	public void run(DisplayMode dm){
    		getContentPane().setBackground(Color.BLUE);
    		setForeground(Color.WHITE);
    		setFont(new Font("Arial", Font.PLAIN, 72));
    		loaded = false;
    		
    		s = new Screen();
    		
    		try{
    			s.setFullScreen(dm, this);
    			loadpics();
    			try{
    				Thread.sleep(5000);
    			}catch(Exception ex){}
    		}finally{
    			s.restoreScreen();
    		}
    	}
    	
    	//Loads Pictures
    	public void loadpics(){
    		bg = new ImageIcon("C:\\JavaProject\\Kamon\\src\\StarBackGround1.gif").getImage();
    		pic = new ImageIcon("C:\\JavaProject\\Kamon\\src\\Untitled.jpg").getImage();
    		loaded = true;
    		repaint();
    	}
    	
    	public void paint(Graphics g){
    		if(g instanceof Graphics2D){
    			Graphics2D g2 = (Graphics2D)g;
    			g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    		}
    		if (loaded){
    			g.drawImage(bg,0,0,null);
    			g.drawImage(pic,170,180,null);	
    		}
            super.paint(g); 
            g.drawString("Test",300, 500);
    		repaint();
    	}
    }
    This specific part appears to be looping, although I am not too sure:

    Java Code:
    	public void paint(Graphics g){
    		if(g instanceof Graphics2D){
    			Graphics2D g2 = (Graphics2D)g;
    			g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    		}
    		if (loaded){
    			g.drawImage(bg,0,0,null);
    			g.drawImage(pic,170,180,null);	
    		}
            super.paint(g); 
            g.drawString("Test",300, 500);
    		repaint();
    	}
    (I use eclipse as well)

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: My Images Are Looping

    You need to go through this section of the Java tutorial: Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    Then change your code accordingly, and if you still have problems ask about them.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: My Images Are Looping

    Related to your other post on a similar topic. I checked out some of the videos. You would probably do better reading a book on Java. I am not thrilled with some of the techniques the guy uses (Like extending JFrame and overriding paint()). Plus most of the Java videos are 7 years old. A lot has changed since then.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 6
    Last Post: 12-11-2016, 02:19 PM
  2. Replies: 0
    Last Post: 11-14-2012, 11:15 AM
  3. Replies: 0
    Last Post: 12-16-2010, 05:14 PM
  4. Replies: 4
    Last Post: 12-16-2010, 04:32 PM
  5. Looping Help Please
    By JonnySnip3r in forum New To Java
    Replies: 5
    Last Post: 01-31-2010, 05:57 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
  •