Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

drawing imageicon on bufferedImage

843806Dec 9 2008 — edited Dec 9 2008
in this code i want to appear 8 icon in one row and their is an equal space between each icon

i do this but 4 icons only appear why ??

my code:

first class:
________
import javax.swing.JFrame;

public class RandomDemo extends JFrame {

      private TestRandom random;
    public RandomDemo() {
        random=new TestRandom();
      getContentPane().add(random);
    }

    public static void main(String[] args) {
        JFrame frame=new RandomDemo();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(600, 300);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}
second class:
_______________
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.JPanel;

public class TestRandom extends JPanel {

    private BufferedImage image;
    private Graphics g;
    private ImageIcon wall1;
    private int wall1_size=40;

    public TestRandom() {
        image = new BufferedImage(1000, 1000, BufferedImage.TYPE_INT_RGB);
        g = image.getGraphics();
        g.fillRect(0, 0, 1000, 1000);
        g.setColor(Color.WHITE);
        wall1 = new ImageIcon(getClass().getResource("wall.gif"));

        int x=(image.getWidth()-(wall1.getIconWidth()+wall1_size)*7)/2;
        for (int i = 0; i <=7; ++i) {
            wall1.paintIcon(this, g, (wall1.getIconWidth()+wall1_size)+ x*i , 0 );
            repaint();
        }
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponents(g);
        g.drawImage(image, 0, 0, null);
    }
}
the icon i use in this link

http://www.4shared.com/file/75186018/c080da00/icon.html
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 6 2009
Added on Dec 9 2008
10 comments
625 views