Results 1 to 4 of 4

Thread: images

  1. #1
    amith is offline Member
    Join Date
    May 2008
    Posts
    24
    Rep Power
    0

    Default images

    import java.awt.*;
    import javax.swing.*;
    class d extends JComponent implements Runnable
    {
    private Image r;
    private Image g;
    int x=0;
    long floor=329;
    boolean t=true;

    public void run()
    {
    r=new ImageIcon("red.png").getImage();
    g=new ImageIcon("red.png").getImage();
    try
    {
    while(t)
    {
    repaint();
    x++;
    if(x==floor)
    {
    t=false;

    q.drawImage(g,229,x-32,this);

    }
    Thread.sleep(20);
    }}catch(Exception e)
    {
    }
    }
    public void paint(Graphics q)
    {
    q.drawImage(r,229,x,this);


    }}
    class f
    {
    public static void main(String args[])
    {
    d v=new d();
    JFrame b=new JFrame("sdfdsf");
    b.getContentPane().add(v);
    b.setVisible(true);
    b.setSize(300,400);
    (new Thread(v)).start();
    }
    }

    i just want ot place this images one by one i just wnat ot make fist image newfloor so that other image can lie on the top of the other image

  2. #2
    amith is offline Member
    Join Date
    May 2008
    Posts
    24
    Rep Power
    0

    Default images building

    i am having five off screen images i had load it with the help of Imageicon class and i had drawn it using paint method
    i just want the images to be placed on the top of each one in vertical format like a building using threads
    could u plz help with this code and and each image width and height is of 32*32

    import java.awt.*;
    import javax.swing.*;
    class d extends JComponent implements Runnable
    {
    Image r;
    Image g;
    Image n;
    Image t;
    int x=0;
    long floor=329;

    public void run()
    {

    r=new ImageIcon("1.png").getImage();
    g=new ImageIcon("2.png").getImage();
    n=new ImageIcon("3.png").getImage();
    t=new ImageIcon("4.png").getImage();
    try
    {
    while(x!=floor)
    {x++;
    repaint();
    Thread.sleep(10);

    }

    }
    catch(Exception e)
    {
    }
    }

    public void paint(Graphics q)
    {
    q.drawImage(r,229,x,this);
    }
    }
    class f
    {
    public static void main(String args[])
    {
    d v=new d();
    JFrame b=new JFrame("sdfdsf");
    b.getContentPane().add(v);
    b.setVisible(true);
    b.setSize(300,400);
    (new Thread(v)).start();
    }
    }
    if the code is incomplete then plz help iam stuck in this program

  3. #3
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    To stack the images you don't need a thread/Runnable.
    Main trouble was that the letters "g" and "q" got mixed up.
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    class dRx extends JComponent
    {
        Image r;
        Image q;
        Image n;
        Image t;
        int x=0;
        long floor=329;
    
        public dRx()
        {
    
            r=new ImageIcon("1.png").getImage();
            q=new ImageIcon("2.png").getImage();
            n=new ImageIcon("3.png").getImage();
            t=new ImageIcon("4.png").getImage();
    /*
            String z = "images/geek/geek";
            r=new ImageIcon(z+"-c---.gif").getImage();
            q=new ImageIcon(z+"--g--.gif").getImage();
            n=new ImageIcon(z+"---h-.gif").getImage();
            t=new ImageIcon(z+"----t.gif").getImage();
    */
        }
    
        protected void paintComponent(Graphics g)
        {
            super.paintComponent(g);
            int y = getHeight() - r.getHeight(this);
            System.out.println("y = " + y);
            g.drawImage(r,x,y,this);
            y -= r.getHeight(this);
            g.drawImage(q,x,y,this);
            y -= q.getHeight(this);
            g.drawImage(n,x,y,this);
            y -= n.getHeight(this);
            g.drawImage(t,x,y,this);
        }
    
        public static void main(String[] args)
        {
            dRx v=new dRx();
            JFrame b=new JFrame("sdfdsf");
            b.getContentPane().add(v);
            b.setSize(300,400);
            b.setVisible(true);
        }
    }

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

    Default

    General commet on your coding style.

    Using single letters for variable makes it very hard to understand the logic of the program. Please take the time to think what a variable is used for and give it a descriptive name.
    Personally I wouldn't bother trying to solve problems with this code as its too hard to follow what you are trying to do.

    Another useful coding technique is to place comments in the code to describe what it is you are trying to do. Again, this code is void of comments.

Similar Threads

  1. XML Images
    By JavaWizz in forum XML
    Replies: 1
    Last Post: 10-17-2008, 10:19 AM
  2. images
    By amith in forum AWT / Swing
    Replies: 1
    Last Post: 05-20-2008, 10:54 AM
  3. Help using images in Java
    By toby in forum Advanced Java
    Replies: 1
    Last Post: 08-07-2007, 05:54 AM
  4. Help with images...
    By toby in forum Java Applets
    Replies: 1
    Last Post: 08-04-2007, 05:25 AM
  5. Images in JSP
    By Daniel in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 06-05-2007, 06:01 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
  •