Java Example Program/ Sample Source Code
/*
* Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* - Neither the name of Oracle or the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* GetLayoutAlignmentX_LayoutManager2.java requires one other file:
* DiagonalLayout.java
*/
import javax.swing.JButton;
import javax.swing.JFrame;
import java.awt.*;
public class GetLayoutAlignmentX_LayoutManager2 {
public static void addComponentsToPane(Container pane) {
pane.setLayout(new DiagonalLayout());
pane.add(new JButton("Button 1"));
pane.add(new JButton("Button 2"));
pane.add(new JButton("Button 3"));
pane.add(new JButton("Button 4"));
pane.add(new JButton("Button 5"));
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("GetLayoutAlignmentX_LayoutManager2");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Set up the content pane.
addComponentsToPane(frame.getContentPane());
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
class DiagonalLayout implements LayoutManager2 {
private int vgap;
private int minWidth = 0, minHeight = 0;
private int preferredWidth = 0, preferredHeight = 0;
private boolean sizeUnknown = true;
public DiagonalLayout() {
this(5);
}
public DiagonalLayout(int v) {
vgap = v;
}
/* Required by LayoutManager. */
public void addLayoutComponent(String name, Component comp) {
}
/* Required by LayoutManager. */
public void removeLayoutComponent(Component comp) {
}
private void setSizes(Container parent) {
int nComps = parent.getComponentCount();
Dimension d = null;
//Reset preferred/minimum width and height.
preferredWidth = 0;
preferredHeight = 0;
minWidth = 0;
minHeight = 0;
for (int i = 0; i < nComps; i++) {
Component c = parent.getComponent(i);
if (c.isVisible()) {
d = c.getPreferredSize();
if (i > 0) {
preferredWidth += d.width/2;
preferredHeight += vgap;
} else {
preferredWidth = d.width;
}
preferredHeight += d.height;
minWidth = Math.max(c.getMinimumSize().width,
minWidth);
minHeight = preferredHeight;
}
}
}
/* Required by LayoutManager. */
public Dimension preferredLayoutSize(Container parent) {
Dimension dim = new Dimension(0, 0);
setSizes(parent);
//Always add the container's insets!
Insets insets = parent.getInsets();
dim.width = preferredWidth
+ insets.left + insets.right;
dim.height = preferredHeight
+ insets.top + insets.bottom;
sizeUnknown = false;
return dim;
}
/* Required by LayoutManager. */
public Dimension minimumLayoutSize(Container parent) {
Dimension dim = new Dimension(0, 0);
//Always add the container's insets!
Insets insets = parent.getInsets();
dim.width = minWidth
+ insets.left + insets.right;
dim.height = minHeight
+ insets.top + insets.bottom;
sizeUnknown = false;
return dim;
}
/* Required by LayoutManager. */
/*
* This is called when the panel is first displayed,
* and every time its size changes.
* Note: You CAN'T assume preferredLayoutSize or
* minimumLayoutSize will be called -- in the case
* of applets, at least, they probably won't be.
*/
public void layoutContainer(Container parent) {
Insets insets = parent.getInsets();
int maxWidth = parent.getWidth()
- (insets.left + insets.right);
int maxHeight = parent.getHeight()
- (insets.top + insets.bottom);
int nComps = parent.getComponentCount();
int previousWidth = 0, previousHeight = 0;
int x = 0, y = insets.top;
int xFudge = 0, yFudge = 0;
boolean oneColumn = false;
// Go through the components' sizes, if neither
// preferredLayoutSize nor minimumLayoutSize has
// been called.
if (sizeUnknown) {
setSizes(parent);
}
if (maxWidth <= minWidth) {
oneColumn = true;
}
if (maxWidth != preferredWidth) {
xFudge = (maxWidth - preferredWidth)/(nComps - 1);
}
if (maxHeight > preferredHeight) {
yFudge = (maxHeight - preferredHeight)/(nComps - 1);
}
for (int i = 0 ; i < nComps ; i++) {
Component c = parent.getComponent(i);
if (c.isVisible()) {
Dimension d = c.getPreferredSize();
// increase x and y, if appropriate
if (i > 0) {
if (!oneColumn) {
x += previousWidth/2 + xFudge;
}
y += previousHeight + vgap + yFudge;
}
// If x is too large,
if ((!oneColumn) &&
(x + d.width) >
(parent.getWidth() - insets.right)) {
// reduce x to a reasonable number.
x = parent.getWidth()
- insets.bottom - d.width;
}
// If y is too large,
if ((y + d.height)
> (parent.getHeight() - insets.bottom)) {
// do nothing.
// Another choice would be to do what we do to x.
}
// Set the component's size and position.
c.setBounds(x, y, d.width, d.height);
previousWidth = d.width;
previousHeight = d.height;
}
}
}
public String toString() {
String str = "";
return getClass().getName() + "[vgap=" + vgap + str + "]";
}
@Override
public void addLayoutComponent(Component comp, Object constraints) {
// TODO Auto-generated method stub
}
@Override
public Dimension maximumLayoutSize(Container target) {
// TODO Auto-generated method stub
return null;
}
@Override
public float getLayoutAlignmentX(Container target) {
// TODO Auto-generated method stub
return 0;
}
@Override
public float getLayoutAlignmentY(Container target) {
// TODO Auto-generated method stub
return 0;
}
@Override
public void invalidateLayout(Container target) {
// TODO Auto-generated method stub
}
}
|
|
|
AWTEventMulticaster
- How To Use AWTEventMulticaster
- add(ActionListener a, ActionListener b) In AWTEventMulticaster
- add(AdjustmentListener a, AdjustmentListener b) In AWTEventMulticaster
- add(ComponentListener a, ComponentListener b) In AWTEventMulticaster
- add(ContainerListener a, ContainerListener b) In AWTEventMulticaster
- add(FocusListener a, FocusListener b) In AWTEventMulticaster
- add(HierarchyBoundsListener a, HierarchyBoundsListener b) In AWTEventMulticaster
- add(HierarchyListener a, HierarchyListener b) In AWTEventMulticaster
- add(InputMethodListener a, InputMethodListener b) In AWTEventMulticaster
- add(ItemListener a, ItemListener b) In AWTEventMulticaster
- add(KeyListener a, KeyListener b) In AWTEventMulticaster
- add(MouseListener a, MouseListener b) In AWTEventMulticaster
- add(MouseMotionListener a, MouseMotionListener b) In AWTEventMulticaster
- add(MouseWheelListener a, MouseWheelListener b) In AWTEventMulticaster
- add(TextListener a, TextListener b) In AWTEventMulticaster
- add(WindowFocusListener a, WindowFocusListener b) In AWTEventMulticaster
- add(WindowListener a, WindowListener b) In AWTEventMulticaster
- add(WindowStateListener a, WindowStateListener b) In AWTEventMulticaster
- addInternal(EventListener a, EventListener b) In AWTEventMulticaster
- remove(ActionListener l, ActionListener oldl) In AWTEventMulticaster
- remove(AdjustmentListener l, AdjustmentListener oldl) In AWTEventMulticaster
- remove(ComponentListener l, ComponentListener oldl) In AWTEventMulticaster
- remove(ContainerListener l, ContainerListener oldl) In AWTEventMulticaster
- remove(EventListener oldl) In AWTEventMulticaster
- remove(FocusListener l, FocusListener oldl) In AWTEventMulticaster
- remove(HierarchyBoundsListener l, HierarchyBoundsListener oldl) In AWTEventMulticaster
- remove(HierarchyListener l, HierarchyListener oldl) In AWTEventMulticaster
- remove(InputMethodListener l, InputMethodListener oldl) In AWTEventMulticaster
- remove(ItemListener l, ItemListener oldl) In AWTEventMulticaster
- remove(KeyListener l, KeyListener oldl) In AWTEventMulticaster
- remove(MouseListener l, MouseListener oldl) In AWTEventMulticaster
- remove(MouseMotionListener l, MouseMotionListener oldl) In AWTEventMulticaster
- remove(MouseWheelListener l, MouseWheelListener oldl) In AWTEventMulticaster
- remove(TextListener l, TextListener oldl) In AWTEventMulticaster
- remove(WindowFocusListener l, WindowFocusListener oldl) In AWTEventMulticaster
- remove(WindowListener l, WindowListener oldl) In AWTEventMulticaster
- remove(WindowStateListener l, WindowStateListener oldl) In AWTEventMulticaster
- removeInternal(EventListener l, EventListener oldl) In AWTEventMulticaster
- save(ObjectOutputStream s, String k, EventListener l) In AWTEventMulticaster
- saveInternal(ObjectOutputStream s, String k) In AWTEventMulticaster
DefaultFocusTraversalPolicy
DefaultKeyboardFocusManager
Graphics
- clipRect(int x, int y, int width, int height) In Graphics
- drawArc(int x, int y, int width, int height, int startAngle, int arcAngle) In Graphics
- drawLine(int x1, int y1, int x2, int y2) In Graphics
- drawOval(int x, int y, int width, int height) In Graphics
- drawPolygon(int[] xPoints, int[] yPoints, int nPoints) In Graphics
- drawRect(int x, int y, int width, int height) In Graphics
- drawString(String str, int x, int y) In Graphics
- fillArc(int x, int y, int width, int height, int startAngle, int arcAngle) In Graphics
- fillOval(int x, int y, int width, int height) In Graphics
- fillPolygon(int[] xPoints, int[] yPoints, int nPoints) In Graphics
- fillRect(int x, int y, int width, int height) In Graphics
- drawImage(Image img, int x, int y, ImageObserver observer) In Graphics
- setColor(Java.awt.Color) In Graphics
- setPaintMode() In Graphics
- setXORMode(Java.awt.Color) In Graphics
- setFont(Java.awt.Font) In Graphics
GraphicsConfiguration
- Using GraphicsConfiguration
- createCompatibleImage(int width, int height) In GraphicsConfiguration
- createCompatibleImage(int width, int height, int transparency) In GraphicsConfiguration
- createCompatibleVolatileImage(int width, int height) In GraphicsConfiguration
- createCompatibleVolatileImage(int width, int height, ImageCapabilities caps) In GraphicsConfiguration
- createCompatibleVolatileImage(int width, int height, ImageCapabilities caps, int transparency) In GraphicsConfiguration
- createCompatibleVolatileImage(int width, int height, int transparency) In GraphicsConfiguration
- getBounds() In GraphicsConfiguration
- getBufferCapabilities() In GraphicsConfiguration
- getColorModel() In GraphicsConfiguration
- getColorModel(int transparency) In GraphicsConfiguration
- getDefaultTransform() In GraphicsConfiguration
- getDevice() In GraphicsConfiguration
- getImageCapabilities() In GraphicsConfiguration
- getNormalizingTransform() In GraphicsConfiguration
- isTranslucencyCapable() In GraphicsConfiguration
MediaTracker
- How To Use MediaTracker
- addImage(Image image, int id) In MediaTracker
- addImage(Image image, int id, int w, int h) In MediaTracker
- checkAll() In MediaTracker
- checkAll(boolean load) In MediaTracker
- checkID(int id) In MediaTracker
- checkID(int id, boolean load) In MediaTracker
- getErrorsAny() In MediaTracker
- getErrorsID(int id) In MediaTracker
- isErrorAny() In MediaTracker
- isErrorID(int id) In MediaTracker
- removeImage(Image image) In MediaTracker
- removeImage(Image image, int id) In MediaTracker
- removeImage(Image image, int id, int width, int height) In MediaTracker
- statusAll(boolean load) In MediaTracker
- statusID(int id, boolean load) In MediaTracker
- waitForAll() In MediaTracker
- waitForAll(long ms) In MediaTracker
- waitForID(int id) In MediaTracker
- waitForID(int id, long ms) In MediaTracker
|
|
|
|