Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Swing / AWT / SWT
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Swing / AWT / SWT
Creating multiple images using a loop.
John Wayne
Greenhorn
Posts: 1
posted 16 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
How? I've tried many times, this is what I have so far:
public static void addComponentsToPane(Container pane) { for(int i = 0; i < 400; i++) { pane.setLayout(null); Insets insets = pane.getInsets(); ImageIcon icon = new ImageIcon("Grass.gif"); JLabel GrassTop = new JLabel(icon); JLabel GrassBottom = new JLabel(icon); Dimension sizeTop = GrassTop.getPreferredSize(); Dimension sizeBottom = GrassBottom.getPreferredSize(); GrassTop.setBounds(insets.left, insets.top, sizeTop.width, sizeTop.height); GrassBottom.setBounds(insets.left - 20, insets.top + 20, sizeBottom.width - 20, sizeBottom.height); pane.add(GrassTop); pane.add(GrassBottom); } }
But, I want to display the images within a certain amount of pixels so they don't overlap, help?
Kaydell Leavitt
Ranch Hand
Posts: 694
I like...
posted 16 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Welcome to the
Java
Ranch,
You could use a GridBagLayout and a GridBagConstraints object instead of using a null layout manager.
package com.kaydell.test; import java.awt.Container; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.SwingUtilities; // This annotation suppresses warnings about serialization for the following class. @SuppressWarnings("serial") // The grass window extends JFrame window and implements the Runnable interface to allow the window to be more thread-safe. public class GrassWindow extends JFrame implements Runnable { // This GridBagConstrainst object lets you control the size and placement of Component objects public GridBagConstraints createGridBagConstrains() { GridBagConstraints constraints = new GridBagConstraints(); constraints.anchor = GridBagConstraints.NORTH; constraints.gridx = 0; constraints.gridy = 0; constraints.gridwidth = 1; constraints.gridheight = 1; return constraints; } // This method adds icons by row and column using the GridBagLayout and the GridBagConstraint objects. public void addComponentsToPane() { Container pane = getContentPane(); pane.setLayout(new GridBagLayout()); GridBagConstraints constraints = createGridBagConstrains(); ImageIcon icon = new ImageIcon("Grass.gif"); for(int i = 0; i < 20; i++) { for (int j = 0; j < 20; j++) { JLabel GrassTop = new JLabel(icon); JLabel GrassBottom = new JLabel(icon); constraints.gridx = i; constraints.gridy = j; pane.add(GrassTop,constraints); pane.add(GrassBottom,constraints); } } } // This is called by the Swing thread to run this window on the Swing thread. public void run() { pack(); setVisible(true); } // Entry-point into the program. public static void main(String[] args) { GrassWindow theGrassWindow = new GrassWindow(); theGrassWindow.addComponentsToPane(); SwingUtilities.invokeLater(theGrassWindow); } }
Did you see how Paul
cut 87% off of his electric heat bill with 82 watts of micro heaters
?
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Positioning many panels...
Scrolling through a textarea
Applet not inialized
Null Pointer Exception
Displaying an image in a set location in an applet
More...