Java Code Examples for javax.swing.JComponent#getWidth()

The following examples show how to use javax.swing.JComponent#getWidth() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GridRenderer.java    From energy2d with GNU Lesser General Public License v3.0 6 votes vote down vote up
byte onGridLine(JComponent c, int x, int y) {
	float dx = (float) c.getWidth() / (float) nx;
	float dy = (float) c.getHeight() / (float) ny;
	int k;
	for (int i = 0; i < nx; i += gridSize) {
		k = Math.round(i * dx);
		if (Math.abs(x - k) < 5)
			return Y_LINE;
	}
	for (int i = 0; i < ny; i += gridSize) {
		k = Math.round(i * dy);
		if (Math.abs(y - k) < 5)
			return X_LINE;
	}
	return -1;
}
 
Example 2
Source File: InfoPanel.java    From JByteMod-Beta with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void setBoundsForFrame(JComponent f, int newX, int newY, int newWidth, int newHeight) {
  if (!(f instanceof JInternalFrame)) {
    return;
  }
  boolean didResize = (f.getWidth() != newWidth || f.getHeight() != newHeight);
  if (!inBounds((JInternalFrame) f, newX, newY, newWidth, newHeight)) {
    Container parent = f.getParent();
    Dimension parentSize = parent.getSize();
    int boundedX = (int) Math.min(Math.max(0, newX), parentSize.getWidth() - newWidth);
    int boundedY = (int) Math.min(Math.max(0, newY), parentSize.getHeight() - newHeight);
    f.setBounds(boundedX, boundedY, newWidth, newHeight);
  } else {
    f.setBounds(newX, newY, newWidth, newHeight);
  }
  if (didResize) {
    f.validate();
  }
}
 
Example 3
Source File: MainDesktopManager.java    From mars-sim with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void setBoundsForFrame(JComponent f, int newX, int newY, int newWidth, int newHeight) {
	boolean hitBoundary = (f.getWidth() != newWidth || f.getHeight() != newHeight);

	if (!inBounds((JInternalFrame) f, newX, newY, newWidth, newHeight)) {
		Container parent = f.getParent();
		Dimension parentSize = parent.getSize();

		// Limit the unit window or tool windows to stay inside and never go outside of
		// the desktop
		// or always show up fully (never show up less than the full window)
		int boundedX = (int) Math.min(Math.max(0, newX), parentSize.getWidth() - newWidth);
		int boundedY = (int) Math.min(Math.max(0, newY), parentSize.getHeight() - 40);// newHeight);
		if (f != null)
			f.setBounds(boundedX, boundedY, newWidth, newHeight);
	} else {
		if (f != null)
			f.setBounds(newX, newY, newWidth, newHeight);
	}

	if (hitBoundary) {
		if (f != null)
			f.validate();
	}

}
 
Example 4
Source File: SettlersDynamicLabelUi.java    From settlers-remake with MIT License 6 votes vote down vote up
@Override
public void paint(Graphics g, JComponent c) {
	int w = c.getWidth();
	int bgw = backgroundImage.getWidth();
	int bgh = backgroundImage.getHeight();

	// left border
	g.drawImage(backgroundImage, 0, 0, c);

	// draw center
	for (int x = bgw; x < w; x += bgw - 6) {
		g.drawImage(backgroundImage, x, 0, x + bgw - 6, bgh, 3, 0, bgw - 3, bgh, c);
	}

	// draw right border
	g.drawImage(backgroundImage, w, 0, w - 2, bgh, bgw - 2, 0, bgw, bgh, c);
	super.paint(g, c);
}
 
Example 5
Source File: GridRenderer.java    From energy2d with GNU Lesser General Public License v3.0 6 votes vote down vote up
void render(JComponent c, Graphics2D g) {

		int w = c.getWidth();
		int h = c.getHeight();
		float dx = (float) w / (float) nx;
		float dy = (float) h / (float) ny;

		Color oldColor = g.getColor();
		Stroke oldStroke = g.getStroke();
		g.setColor(color);
		g.setStroke(stroke);

		int k;
		for (int i = 0; i < nx; i += gridSize) {
			k = Math.round(i * dx);
			g.drawLine(k, 0, k, h);
		}
		for (int i = 0; i < ny; i += gridSize) {
			k = Math.round(i * dy);
			g.drawLine(0, k, w, k);
		}

		g.setColor(oldColor);
		g.setStroke(oldStroke);

	}
 
Example 6
Source File: SynthToggleButtonUI.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
@Override
void paintBackground(SynthContext context, Graphics g, JComponent c) {
    if (((AbstractButton) c).isContentAreaFilled()) {
        int x = 0, y = 0, w = c.getWidth(), h = c.getHeight();
        SynthPainter painter = context.getPainter();
        painter.paintToggleButtonBackground(context, g, x, y, w, h);
    }
}
 
Example 7
Source File: SynthToggleButtonUI.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Override
void paintBackground(SynthContext context, Graphics g, JComponent c) {
    if (((AbstractButton) c).isContentAreaFilled()) {
        int x = 0, y = 0, w = c.getWidth(), h = c.getHeight();
        SynthPainter painter = context.getPainter();
        painter.paintToggleButtonBackground(context, g, x, y, w, h);
    }
}
 
Example 8
Source File: CircularProgressBarUI.java    From pumpernickel with MIT License 5 votes vote down vote up
@Override
public void paint(Graphics g0, JComponent c) {
	Graphics2D g = (Graphics2D) g0;
	g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
			RenderingHints.VALUE_ANTIALIAS_ON);
	g.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL,
			RenderingHints.VALUE_STROKE_PURE);

	Insets i = progressBar.getInsets();

	int x = 0;
	int y = 0;
	int width = c.getWidth();
	int height = c.getHeight();
	x += i.left;
	y += i.top;
	width -= i.left + i.right;
	height -= i.top + i.bottom;

	int diameter = Math.min(width, height);
	int radius = diameter / 2;
	int centerX = x + width / 2;
	int centerY = y + height / 2;

	float strokeWidth = getStrokeWidth(diameter);

	if (progressBar.isIndeterminate()) {
		paintIndeterminate(g, radius, strokeWidth, centerX, centerY);
	} else {
		paintDeterminate(g, radius, strokeWidth, centerX, centerY);
	}
}
 
Example 9
Source File: GradientPanelUI.java    From pumpernickel with MIT License 5 votes vote down vote up
@Override
public void paint(Graphics g0, JComponent c) {
	Graphics2D g = (Graphics2D) g0;

	Insets i = c.getInsets();
	int topY = i.top;
	int bottomY = c.getHeight() - i.bottom;
	int leftX = i.left;
	int rightX = c.getWidth() - i.right;
	Rectangle r = new Rectangle(leftX, topY, rightX - leftX, bottomY - topY);
	paintGradient(g, r.x, r.y, r.width, r.height, r);
}
 
Example 10
Source File: SynthToggleButtonUI.java    From JDKSourceCode1.8 with MIT License 5 votes vote down vote up
@Override
void paintBackground(SynthContext context, Graphics g, JComponent c) {
    if (((AbstractButton) c).isContentAreaFilled()) {
        int x = 0, y = 0, w = c.getWidth(), h = c.getHeight();
        SynthPainter painter = context.getPainter();
        painter.paintToggleButtonBackground(context, g, x, y, w, h);
    }
}
 
Example 11
Source File: SwingExportUtil.java    From mzmine2 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Writes swing to EMF
 * 
 * @param panel
 * @param fileName
 * @throws Exception
 */
public static void writeToEMF(JComponent panel, File fileName) throws IOException {
  // print the panel to pdf
  int width = panel.getWidth();
  int height = panel.getWidth();
  logger.info(
      () -> MessageFormat.format("Exporting panel to EMF file (width x height; {0} x {1}): {2}",
          width, height, fileName.getAbsolutePath()));

  VectorGraphics g = new EMFGraphics2D(fileName, new Dimension(width, height));
  g.startExport();
  panel.print(g);
  g.endExport();
}
 
Example 12
Source File: SynthToggleButtonUI.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Override
void paintBackground(SynthContext context, Graphics g, JComponent c) {
    if (((AbstractButton) c).isContentAreaFilled()) {
        int x = 0, y = 0, w = c.getWidth(), h = c.getHeight();
        SynthPainter painter = context.getPainter();
        painter.paintToggleButtonBackground(context, g, x, y, w, h);
    }
}
 
Example 13
Source File: SynthToggleButtonUI.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
@Override
void paintBackground(SynthContext context, Graphics g, JComponent c) {
    if (((AbstractButton) c).isContentAreaFilled()) {
        int x = 0, y = 0, w = c.getWidth(), h = c.getHeight();
        SynthPainter painter = context.getPainter();
        painter.paintToggleButtonBackground(context, g, x, y, w, h);
    }
}
 
Example 14
Source File: FlatToolBarSeparatorUI.java    From FlatLaf with Apache License 2.0 5 votes vote down vote up
@Override
public void paint( Graphics g, JComponent c ) {
	int width = c.getWidth();
	int height = c.getHeight();
	float lineWidth = scale( 1f );
	float offset = scale( 2f );

	FlatUIUtils.setRenderingHints( (Graphics2D) g );
	g.setColor( separatorColor );

	if( isVertical( c ) )
		((Graphics2D)g).fill( new Rectangle2D.Float( Math.round( (width - lineWidth) / 2f ), offset, lineWidth, height - (offset * 2) ) );
	else
		((Graphics2D)g).fill( new Rectangle2D.Float( offset, Math.round( (height - lineWidth) / 2f ), width - (offset * 2), lineWidth ) );
}
 
Example 15
Source File: SeaGlassGraphicsUtils.java    From seaglass with Apache License 2.0 4 votes vote down vote up
/**
 * Paints an icon and text. This will render the text as html, if necessary,
 * and offset the location by the insets of the component.
 *
 * @param ss
 *            SynthContext
 * @param g
 *            Graphics to render string and icon into
 * @param text
 *            Text to layout
 * @param icon
 *            Icon to layout
 * @param hAlign
 *            horizontal alignment
 * @param vAlign
 *            vertical alignment
 * @param hTextPosition
 *            horizontal text position
 * @param vTextPosition
 *            vertical text position
 * @param iconTextGap
 *            gap between icon and text
 * @param mnemonicIndex
 *            Index into text to render the mnemonic at, -1 indicates no
 *            mnemonic.
 * @param textOffset
 *            Amount to offset the text when painting
 */
public void paintText(SynthContext ss, Graphics g, String text, Icon icon, int hAlign, int vAlign, int hTextPosition,
    int vTextPosition, int iconTextGap, int mnemonicIndex, int textOffset) {
    if ((icon == null) && (text == null)) {
        return;
    }

    Graphics2D g2d = (Graphics2D) g.create();

    g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    JComponent c = ss.getComponent();
    FontMetrics fm = SwingUtilities2.getFontMetrics(c, g2d);
    Insets insets = SeaGlassLookAndFeel.getPaintingInsets(ss, paintInsets);

    paintViewR.x = insets.left;
    paintViewR.y = insets.top;
    paintViewR.width = c.getWidth() - (insets.left + insets.right);
    paintViewR.height = c.getHeight() - (insets.top + insets.bottom);

    paintIconR.x = paintIconR.y = paintIconR.width = paintIconR.height = 0;
    paintTextR.x = paintTextR.y = paintTextR.width = paintTextR.height = 0;

    String clippedText = layoutText(ss, fm, text, icon, hAlign, vAlign, hTextPosition, vTextPosition, paintViewR, paintIconR,
        paintTextR, iconTextGap);

    if (icon != null) {
        Color color = g2d.getColor();

        paintIconR.x += textOffset;
        paintIconR.y += textOffset;
        SeaGlassIcon.paintIcon(icon, ss, g2d, paintIconR.x, paintIconR.y, paintIconR.width, paintIconR.height);
        g2d.setColor(color);
    }

    if (text != null) {
        View v = (View) c.getClientProperty(BasicHTML.propertyKey);

        if (v != null) {
            v.paint(g2d, paintTextR);
        } else {
            paintTextR.x += textOffset;
            paintTextR.y += textOffset;

            paintText(ss, g2d, clippedText, paintTextR, mnemonicIndex);
        }
    }
}
 
Example 16
Source File: ProgressBarUI.java    From rapidminer-studio with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
protected void paintIndeterminate(Graphics g, JComponent c) {
	boolean compressed = Boolean.parseBoolean(String.valueOf(progressBar
			.getClientProperty(RapidLookTools.PROPERTY_PROGRESSBAR_COMPRESSED)));

	int y = 0;
	int x = 0;
	int w;
	int h;
	if (compressed) {
		x = (int) (c.getWidth() * 0.67);
		w = (int) (c.getWidth() * 0.33);
		y = 3;
		h = c.getHeight() - 6;
	} else {
		w = c.getWidth();
		h = c.getHeight() / 2;
	}

	Graphics2D g2 = (Graphics2D) g;
	g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

	if (c.isOpaque()) {
		if (c.getParent() != null) {
			g2.setColor(c.getParent().getBackground());
		} else {
			g2.setColor(c.getBackground());
		}
		g2.fillRect(x, y, c.getWidth(), c.getHeight());
	}

	g2.setColor(Colors.PROGRESSBAR_BACKGROUND);
	g2.fillRoundRect(x + 1, y + 1, w - 2, h - 2, RapidLookAndFeel.CORNER_DEFAULT_RADIUS,
			RapidLookAndFeel.CORNER_DEFAULT_RADIUS);

	g2.setColor(Colors.PROGRESSBAR_BORDER);
	g2.drawRoundRect(x + 1, y + 1, w - 2, h - 2, RapidLookAndFeel.CORNER_DEFAULT_RADIUS,
			RapidLookAndFeel.CORNER_DEFAULT_RADIUS);

	// make sure we don't draw over the boundaries
	RoundRectangle2D clipRect = new RoundRectangle2D.Double(x + 3, y + 3, w - 5, h - 5,
			RapidLookAndFeel.CORNER_DEFAULT_RADIUS / 2, RapidLookAndFeel.CORNER_DEFAULT_RADIUS / 2);
	g2.setClip(clipRect);

	for (double xCoord = x + -4 * ANIMATION_BAR_LENGTH + System.currentTimeMillis() * ANIMATION_SPEED
			% (2 * ANIMATION_BAR_LENGTH); xCoord < x + w + 2 * ANIMATION_BAR_LENGTH;) {
		g2.setColor(Colors.PROGRESSBAR_INDETERMINATE_FOREGROUND_1);
		g2.fill(createIntermediateShape(xCoord, ANIMATION_BAR_LENGTH, h));
		xCoord += ANIMATION_BAR_LENGTH;
		g2.setColor(Colors.PROGRESSBAR_INDETERMINATE_FOREGROUND_2);
		g2.fill(createIntermediateShape(xCoord, ANIMATION_BAR_LENGTH, h));
		xCoord += ANIMATION_BAR_LENGTH;
	}
	g2.setClip(null);

	drawString(g2, w, h, compressed);
}
 
Example 17
Source File: BoxTabbedPaneUI.java    From pumpernickel with MIT License 4 votes vote down vote up
@Override
public boolean contains(JComponent c, int x, int y) {
	return x >= 0 && x < c.getWidth() && y >= 0 && y < c.getHeight();
}
 
Example 18
Source File: VerticalLabelUI.java    From mars-sim with GNU General Public License v3.0 4 votes vote down vote up
@Override
   public void paint(Graphics g, JComponent c) {
       JLabel label = (JLabel)c;
       String text = label.getText();
       Icon icon = (label.isEnabled()) ? label.getIcon() : label.getDisabledIcon();

       if ((icon == null) && (text == null)) {
           return;
       }

       FontMetrics fm = g.getFontMetrics();
       paintViewInsets = c.getInsets(paintViewInsets);

       paintViewR.x = paintViewInsets.left;
       paintViewR.y = paintViewInsets.top;

   	// Use inverted height &amp; width
       paintViewR.height = c.getWidth() - (paintViewInsets.left + paintViewInsets.right);
       paintViewR.width = c.getHeight() - (paintViewInsets.top + paintViewInsets.bottom);

       paintIconR.x = paintIconR.y = paintIconR.width = paintIconR.height = 0;
       paintTextR.x = paintTextR.y = paintTextR.width = paintTextR.height = 0;

       String clippedText = layoutCL(label, fm, text, icon, paintViewR, paintIconR, paintTextR);

   	Graphics2D g2 = (Graphics2D) g;
   	AffineTransform tr = g2.getTransform();
   	if (clockwise) {
           g2.rotate( Math.PI / 2 );
           g2.translate( 0, - c.getWidth() );
   	} else {
           g2.rotate( - Math.PI / 2 );
           g2.translate( - c.getHeight(), 0 );
   	}

   	if (icon != null) {
           icon.paintIcon(c, g, paintIconR.x, paintIconR.y);
       }

       if (text != null) {
           int textX = paintTextR.x;
           int textY = paintTextR.y + fm.getAscent();

           if (label.isEnabled()) {
               paintEnabledText(label, g, clippedText, textX, textY);
           } else {
               paintDisabledText(label, g, clippedText, textX, textY);
           }
       }
g2.setTransform( tr );
   }
 
Example 19
Source File: FlatButtonUI.java    From FlatLaf with Apache License 2.0 4 votes vote down vote up
protected void paintBackground( Graphics g, JComponent c ) {
	Color background = getBackground( c );
	if( background == null )
		return;

	Graphics2D g2 = (Graphics2D) g.create();
	try {
		FlatUIUtils.setRenderingHints( g2 );

		boolean isToolBarButton = isToolBarButton( c );
		float focusWidth = isToolBarButton ? 0 : FlatUIUtils.getBorderFocusWidth( c );
		float arc = FlatUIUtils.getBorderArc( c );

		boolean def = isDefaultButton( c );

		int x = 0;
		int y = 0;
		int width = c.getWidth();
		int height = c.getHeight();

		if( isToolBarButton ) {
			Insets spacing = UIScale.scale( toolbarSpacingInsets );
			x += spacing.left;
			y += spacing.top;
			width -= spacing.left + spacing.right;
			height -= spacing.top + spacing.bottom;
		}

		// paint shadow
		Color shadowColor = def ? defaultShadowColor : this.shadowColor;
		if( !isToolBarButton && shadowColor != null && shadowWidth > 0 && focusWidth > 0 &&
			!(isFocusPainted( c ) && FlatUIUtils.isPermanentFocusOwner( c )) && c.isEnabled() )
		{
			g2.setColor( shadowColor );
			g2.fill( new RoundRectangle2D.Float( focusWidth, focusWidth + UIScale.scale( (float) shadowWidth ),
				width - focusWidth * 2, height - focusWidth * 2, arc, arc ) );
		}

		// paint background
		Color startBg = def ? defaultBackground : startBackground;
		Color endBg = def ? defaultEndBackground : endBackground;
		if( background == startBg && endBg != null && !startBg.equals( endBg ) )
			g2.setPaint( new GradientPaint( 0, 0, startBg, 0, height, endBg ) );
		else
			g2.setColor( FlatUIUtils.deriveColor( background, getBackgroundBase( c, def ) ) );

		FlatUIUtils.paintComponentBackground( g2, x, y, width, height, focusWidth, arc );
	} finally {
		g2.dispose();
	}
}
 
Example 20
Source File: SeaGlassScrollPaneUI.java    From seaglass with Apache License 2.0 4 votes vote down vote up
/**
 * @param g
 * @param c
 */
@SuppressWarnings("unchecked")
private void paintScrollPaneCorner(Graphics g, JComponent c) {
    if (scrollpane == null) {
        return;
    }
    if (scrollpane.getHorizontalScrollBar() == null || !scrollpane.getHorizontalScrollBar().isVisible()) {
        return;
    }
    if (scrollpane.getVerticalScrollBar() == null || !scrollpane.getVerticalScrollBar().isVisible()) {
        return;
    }

    int vBarWidth = scrollpane.getVerticalScrollBar().getWidth();
    int hBarHeight = scrollpane.getHorizontalScrollBar().getHeight();

    Insets insets = c.getInsets();

    Graphics2D g2 = (Graphics2D) g.create();
    
    int translateX = c.getWidth() - insets.right - vBarWidth;
    int translateY = c.getHeight() - insets.bottom - hBarHeight;
    boolean ltr = scrollpane.getComponentOrientation().isLeftToRight();
    if (!ltr) {
        translateX = 15 + insets.right;
    }
    Rectangle visibleRect = scrollpane.getVisibleRect();
    
    // Berechnung, ob die Ecke im sichtbare Bereich liegt
    int clipX = Math.min( vBarWidth, visibleRect.x + visibleRect.width - translateX );
    int clipY = Math.min( hBarHeight, visibleRect.y + visibleRect.height - translateY );
    
    if ( clipY > 0 && clipX > 0 ) {
        g2.translate(translateX, translateY);
        if (!ltr) {
            g2.scale(-1, 1);
        }
        g2.setClip(0, 0, clipX, clipY);
        
        cornerPainter.paint(g2, c, 15, 15);
    }
}