下面列出了java.awt.Dimension#getWidth ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static void imageMark(File srcFile, File destFile, int minWidth,
int minHeight, int pos, int offsetX, int offsetY,
String markContent, Color markColor, int markSize, int alpha)
throws IOException, MagickException {
ImageInfo info = new ImageInfo(srcFile.getAbsolutePath());
MagickImage image = new MagickImage(info);
Dimension dim = image.getDimension();
int width = (int) dim.getWidth();
int height = (int) dim.getHeight();
if (width < minWidth || height < minHeight) {
image.destroyImages();
if (!srcFile.equals(destFile)) {
FileUtils.copyFile(srcFile, destFile);
}
} else {
imageMark(image, info, width, height, pos, offsetX, offsetY,
markContent, markColor, markSize, alpha);
image.setFileName(destFile.getAbsolutePath());
image.writeImage(info);
image.destroyImages();
}
}
@SuppressWarnings("serial")
private Component makeCenterPanel() {
rosterEntryPanel = new JPanel(new WrapLayout()) {
// Fix to make it play nice with the scroll bar.
@Override
public Dimension getPreferredSize() {
Dimension d = super.getPreferredSize();
d.width = (int) (d.getWidth() - 20);
return d;
}
};
final JScrollPane ret = new JScrollPane(rosterEntryPanel, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
ret.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
ret.revalidate();
}
});
ret.getVerticalScrollBar().setUnitIncrement(27);
return ret;
}
public ProgressPanel(JFrame owner, String message, boolean modal) {
super(owner, modal);
title = message;
setTitle(message);
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
// setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
int x = 100;
int y = 100;
if (MainWindow.frame != null) {
x = MainWindow.frame.getX() + MainWindow.frame.getWidth()/2 - (message.length()*9)/2;
y = MainWindow.frame.getY() + MainWindow.frame.getHeight()/2;
} else {
Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
x = (int) ((dimension.getWidth() - this.getWidth()) / 2);
y = (int) ((dimension.getHeight() - this.getHeight()) / 2);
}
setBounds(100, 100, message.length()*11, 10);
this.setLocation(x, y);
}
/**
* Paints the panel.
*
* @param g the graphics device.
*/
public void paintComponent(final Graphics g) {
super.paintComponent(g);
final Graphics2D g2 = (Graphics2D) g;
final Dimension size = getSize();
final Insets insets = getInsets();
final Rectangle2D available = new Rectangle2D.Double(insets.left, insets.top,
size.getWidth() - insets.left - insets.right,
size.getHeight() - insets.top - insets.bottom);
final double x = available.getX();
final double y = available.getY();
final float width = (float) available.getWidth();
final TextBlock block = TextUtilities.createTextBlock(
this.text, this.font, Color.black, width, new G2TextMeasurer(g2)
);
g2.setPaint(Color.black);
block.draw(g2, (float) x, (float) y, TextBlockAnchor.TOP_LEFT, 0.0f, 0.0f, 0.0);
}
@Action(object = ObjectType.APP, desc ="Move the mouse to the location [<Data>]", input =InputType.YES)
public void moveMouseTO() {
try {
Robot r = new Robot();
int x = Integer.valueOf(Data.split(",")[0]);
int y = Integer.valueOf(Data.split(",")[1]);
//use graphic environment lib. if its is a multi SCREEN
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
if (x < 0) {
x += screenSize.getWidth();//x pixels left of right end
}
if (y < 0) {
y += screenSize.getHeight();//y pixels above bottom end
}
r.mouseMove(x, y);
Report.updateTestLog(Action, "Mouse moved to '" + x + "," + y + "' ", Status.DONE);
} catch (Exception ex) {
Report.updateTestLog(Action, ex.getMessage(), Status.FAIL);
Logger.getLogger(Mouse.class.getName()).log(Level.SEVERE, null, ex);
}
}
private BufferedImage createContentImage( Component c, Dimension contentSize ) {
GraphicsConfiguration config = GraphicsEnvironment.getLocalGraphicsEnvironment()
.getDefaultScreenDevice().getDefaultConfiguration();
BufferedImage res = config.createCompatibleImage(contentSize.width, contentSize.height);
Graphics2D g = res.createGraphics();
//some components may be non-opaque so just black rectangle would be painted then
g.setColor( Color.white );
g.fillRect(0, 0, contentSize.width, contentSize.height);
if( WinSysPrefs.HANDLER.getBoolean(WinSysPrefs.DND_SMALLWINDOWS, true) && c.getWidth() > 0 && c.getHeight() > 0 ) {
double xScale = contentSize.getWidth() / c.getWidth();
double yScale = contentSize.getHeight() / c.getHeight();
g.setTransform(AffineTransform.getScaleInstance(xScale, yScale) );
}
c.paint(g);
return res;
}
/**
* gets the tray size for the icons
*/
private void initSize() {
Dimension trayDim = java.awt.SystemTray.getSystemTray().getTrayIconSize();
if (trayDim == null || trayDim.getWidth() < 1.0 || trayDim.getWidth() < 1.0) {
traySize.setSize(24.0, 24.0);
} else {
traySize.setSize(trayDim);
}
}
/**
* Calculate maximum size for a window that fits on the game screen.
*
* @return size
*/
private Dimension genMaxSize() {
Dimension displaySize = stendhal.getDisplaySize();
final int width = (int) (displaySize.getWidth() - PADDING);
final int height = (int) (displaySize.getHeight() - PADDING);
return new Dimension(width, height);
}
@Override
public Dimension getMinimumSize() {
//Set minimum height to preferred height for small tracks, because there is not point to scroll them
//Minimum height for larger scroll groups is 250 pixels, because smaller view port is not that useful
//either
Dimension pref = super.getPreferredSize();
if (pref.getHeight() < 250) {
return pref;
}
return new Dimension((int) pref.getWidth(), 250);
}
/**
* Returns the farthest vertex position added by default margins.
* Relies on the getGraphSize()-method.
*
* @return the coordinates for the next root
*/
public Point getNewRootPosition(){
Dimension dim = graph.getGraphSize();
int x = (int)dim.getWidth();
//Snap to left border if close enough
if(x < 60){
x = 10;
}
return new Point(x, 10);
}
/**
* Addjusts {@link #panRect} to be postitionsed with the centter at a given
* point if possible. This methods ensures that {@link #panRect} fits
* entirely in {@link #paintRect}.
*
* @param point
* the desired panRect center
*/
private void panRectTo(Point point) {
Dimension viewSize = peerScroller.getViewport().getViewSize();
Rectangle viewRect = peerScroller.getViewport().getViewRect();
double panHalfWidth = panRect.getWidth() / 2;
double panHalfHeight = panRect.getHeight() / 2;
Point2D panOrigin = new Point2D.Double(point.x - panHalfWidth, point.y
- panHalfHeight);
double xk = panOrigin.getX() / paintRect.getWidth();
double yk = panOrigin.getY() / paintRect.getHeight();
Point viewPos = new Point((int) (viewSize.getWidth() * xk), (int) (viewSize
.getHeight() * yk));
// make sure we do not pan past the bounds:
if (viewPos.x < 0)
viewPos.x = 0;
if (viewPos.y < 0)
viewPos.y = 0;
int wd = (viewPos.x + viewRect.width) - viewSize.width;
int hd = (viewPos.y + viewRect.height) - viewSize.height;
if (wd > 0)
viewPos.x -= wd;
if (hd > 0)
viewPos.y -= hd;
// pan it
peerScroller.getViewport().setViewPosition(viewPos);
update();
repaint();
}
/**
* Graphics transform
*
* @param g Graphics2D
* @param x X location
* @param y Y location
* @param s String
* @param x_align X align
* @param y_align Y align
* @param angle Angle
* @return AffineTransform
*/
public static AffineTransform transform_bak(Graphics2D g, float x, float y, String s, XAlign x_align, YAlign y_align, float angle) {
Dimension dim = getStringDimension(s, g);
AffineTransform tempTrans = g.getTransform();
//AffineTransform myTrans = new AffineTransform();
AffineTransform myTrans = (AffineTransform) tempTrans.clone();
switch (x_align) {
case CENTER:
x -= (float)(dim.getWidth() * 0.5);
break;
case RIGHT:
x -= (float)dim.getWidth();
break;
}
switch (y_align) {
case TOP:
y += (float)dim.getHeight();
break;
case CENTER:
y += (float)(dim.getHeight() * 0.5);
break;
}
//myTrans.translate(tempTrans.getTranslateX() + x, tempTrans.getTranslateY() + y);
myTrans.translate(x, y);
myTrans.rotate(-angle * Math.PI / 180);
return myTrans;
}
@Override
public void windowOpened(WindowEvent windowEvent) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Dimension shotSize;
shotSize = windowAncestor.getSize();
int expectedX = (int) (shotSize.getWidth() / 4);
int expectedY = (int) (shotSize.getHeight() / 4);
int maxWidth = (int) (shotSize.getWidth() / 2);
int maxHeight = (int) (shotSize.getHeight() / 2);
boolean popupRes = checkImage(windowAncestor, shotSize, expectedX, expectedY, maxWidth, maxHeight);
shotSize = modalBlocker.getContentPane().getSize();
expectedX = (int) (shotSize.getWidth() / 2 + shotSize.getWidth() / 4);
expectedY = (int) (shotSize.getHeight() / 2 + shotSize.getHeight() / 4);
maxWidth = (int) (shotSize.getWidth());
maxHeight = (int) (shotSize.getHeight());
boolean modalBlockerRes = checkImage(modalBlocker.getContentPane(), shotSize, expectedX,
expectedY, maxWidth, maxHeight);
String msg = "";
if (!popupRes) msg = "The popup must be above the frame.";
if (!modalBlockerRes) msg += "The modal blocker must be above the popup.";
if (!popupRes || !modalBlockerRes)
throw new RuntimeException(msg);
modalBlocker.dispose();
jFrame.dispose();
}
public void setFitZoom(double zoom, Dimension size) {
if (zoom > 10)
zoom = 10;
if (zoom < 0.1)
zoom = 0.1;
int columnCount = 1;
Dimension2D pageSize = getPageSize();
while (((pageSize.getWidth() + W_SPACE / zoom) * columnCount + pageSize
.getWidth()) * zoom < size.getWidth()) {
columnCount++;
}
setup(columnCount, zoom);
}
/**
* Draw label point
*
* @param x X
* @param y Y
* @param font Font
* @param text Text
* @param color Color
* @param g Graphics2D
* @param angle Angle
*/
public static void drawTickLabel(float x, float y, Font font, String text, Color color, float angle, Graphics2D g) {
g.setColor(color);
g.setFont(font);
Dimension labSize = Draw.getStringDimension(text, g);
if (angle == 0) {
x = x - (float) labSize.getWidth() / 2;
y -= (float) labSize.getHeight() / 2;
Draw.drawString(g, text, x, y + labSize.height / 2);
} else {
AffineTransform tempTrans = g.getTransform();
//AffineTransform myTrans = new AffineTransform();
AffineTransform myTrans = (AffineTransform) tempTrans.clone();
myTrans.translate(x, y);
myTrans.translate(tempTrans.getTranslateX() + x, tempTrans.getTranslateY() + y);
myTrans.rotate(-angle * Math.PI / 180);
g.setTransform(myTrans);
if (angle == 90) {
x = -(float) (labSize.getWidth() - 10);
y = (float) (labSize.getHeight() / 3);
} else {
x = -(float) (labSize.getWidth() - 5);
y = 0;
}
Draw.drawString(g, text, x, y);
g.setTransform(tempTrans);
}
}
private static void openMainWindow(final MainPanel cp) throws IOException {
JFrame frame = new JFrame(Configuration.INSTANCE.getAppName());
Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize() ;
float ratio = (float) 0.8 ;
Dimension windowSize = new Dimension ((int)(screenSize.getWidth() * ratio), (int)(screenSize.getHeight() * ratio)) ;
frame.setSize(windowSize.getSize());
frame.setLocationByPlatform(true);
frame.setIconImage(ImageIO.read(SwiftExplorer.class.getResource("/icons/logo.png")));
frame.getContentPane().add(cp);
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
if (cp.onClose()) {
System.exit(0);
}
}
});
cp.setOwner(frame);
frame.setJMenuBar(cp.createMenuBar());
// center the frame
int x = (int) ((screenSize.getWidth() - frame.getWidth()) / 2);
int y = (int) ((screenSize.getHeight() - frame.getHeight()) / 2);
frame.setLocation(x, y);
frame.setVisible(true);
}
private void centreWindow() {
Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
int x = (int) ((dimension.getWidth() - getWidth()) / 2);
int y = (int) ((dimension.getHeight() - getHeight()) / 2);
setLocation(x, y);
}
/**
* Returns true if the scale or translate has changed.
*/
public boolean updateScaleAndTranslate()
{
double newScale = 1;
int dx = 0;
int dy = 0;
if (this.graphComponent != null)
{
Dimension graphSize = graphComponent.getGraphControl().getSize();
Dimension outlineSize = getSize();
int gw = (int) graphSize.getWidth();
int gh = (int) graphSize.getHeight();
if (gw > 0 && gh > 0)
{
boolean magnifyPage = graphComponent.isPageVisible()
&& isFitPage()
&& graphComponent.getHorizontalScrollBar().isVisible()
&& graphComponent.getVerticalScrollBar().isVisible();
double graphScale = graphComponent.getGraph().getView()
.getScale();
mxPoint trans = graphComponent.getGraph().getView()
.getTranslate();
int w = (int) outlineSize.getWidth() - 2 * outlineBorder;
int h = (int) outlineSize.getHeight() - 2 * outlineBorder;
if (magnifyPage)
{
gw -= 2 * Math.round(trans.getX() * graphScale);
gh -= 2 * Math.round(trans.getY() * graphScale);
}
newScale = Math.min((double) w / gw, (double) h / gh);
dx += (int) Math
.round((outlineSize.getWidth() - gw * newScale) / 2);
dy += (int) Math
.round((outlineSize.getHeight() - gh * newScale) / 2);
if (magnifyPage)
{
dx -= Math.round(trans.getX() * newScale * graphScale);
dy -= Math.round(trans.getY() * newScale * graphScale);
}
}
}
if (newScale != scale || translate.x != dx || translate.y != dy)
{
scale = newScale;
translate.setLocation(dx, dy);
return true;
}
else
{
return false;
}
}
public static Point getTitlePoint(Window decoratedWindow) {
Point p = decoratedWindow.getLocationOnScreen();
Dimension d = decoratedWindow.getSize();
return new Point(p.x + (int)(d.getWidth()/2),
p.y + (int)(decoratedWindow.getInsets().top/2));
}
@Override
public void setSize(Dimension d) {
newW=(int)d.getWidth();
newH=(int)d.getHeight();
}