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

The following examples show how to use javax.swing.JComponent#getSize() . 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: CloseableModernTabbedPaneUI.java    From SikuliX1 with MIT License 6 votes vote down vote up
@Override
public void paint(Graphics g, JComponent c) {
  int tabPlacement = tabPane.getTabPlacement();
  Graphics2D g2d = (Graphics2D) g;
  g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

  Insets insets = c.getInsets();
  Dimension size = c.getSize();

  if (tabPane.getTabPlacement() == TOP) {
    g2d.drawImage(tab, insets.left, insets.top,
            size.width - insets.right - insets.left - 1,
            calculateTabAreaHeight(tabPlacement, runCount,
                    maxTabHeight),
            null);
    g2d.setColor(Color.gray);
    g2d.drawLine(size.width - 1, insets.top + 2, size.width - 1, insets.top
            + calculateTabAreaHeight(tabPlacement, runCount,
            maxTabHeight));
  }
/*System.out.println("Tab Height"+calculateTabAreaHeight(tabPlacement, runCount,
 maxTabHeight));*/
  super.paint(g2d, c);
}
 
Example 2
Source File: ComponentMorpher2.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private Image getComponentImage(JComponent component) {
    // Initial component sizing & layout
    component.setSize((getClientSize().width == 0) ? component.getPreferredSize() : getClientSize()); // try to fit the component to ComponentMorpher
    component.doLayout(); // layout component

    // Correct component sizing & layout
    component.setSize(new Dimension(getClientSize().width, component.getPreferredSize().height)); // Width of component is fixed, update height
    component.doLayout(); // layout component

    // One more iteration because of nested JTextAreas
    component.setSize(new Dimension(getClientSize().width, component.getPreferredSize().height)); // Width of component is fixed, update height
    component.doLayout(); // layout component

    // Paint component into BufferedImage
    BufferedImage componentImage = new BufferedImage(component.getSize().width, component.getSize().height,
                                                     BufferedImage.TYPE_INT_RGB);
    component.printAll(componentImage.getGraphics());

    return componentImage;
}
 
Example 3
Source File: MultiDiffPanelController.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private void updateSplitLocation () {
    JComponent parent = (JComponent) panel.getParent();
    Dimension dim = parent == null ? new Dimension() : parent.getSize();
    if (dim.width <= 0 || dim.height <= 0) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                updateSplitLocation();
            }
        });
        return;
    }
    FileViewComponent fileComp = getActiveFileComponent();
    int preferredHeaderHeight = fileComp.getPreferredHeaderHeight();
    int preferredHeight = fileComp.getPreferredHeight();
    int optimalLocation = preferredHeight + preferredHeaderHeight + 5;
    if (optimalLocation > dim.height / 3) {
        optimalLocation = dim.height / 3;
    }
    if (optimalLocation <= preferredHeaderHeight) {
        optimalLocation = preferredHeaderHeight * 3;
    }
    if (dividerSet && panel.splitPane.getDividerLocation() <= optimalLocation) return;
    panel.splitPane.setDividerLocation(optimalLocation);
    dividerSet = true;
}
 
Example 4
Source File: ComponentMorpher2.java    From visualvm with GNU General Public License v2.0 6 votes vote down vote up
private Image getComponentImage(JComponent component) {
    // Initial component sizing & layout
    component.setSize((getClientSize().width == 0) ? component.getPreferredSize() : getClientSize()); // try to fit the component to ComponentMorpher
    component.doLayout(); // layout component

    // Correct component sizing & layout
    component.setSize(new Dimension(getClientSize().width, component.getPreferredSize().height)); // Width of component is fixed, update height
    component.doLayout(); // layout component

    // One more iteration because of nested JTextAreas
    component.setSize(new Dimension(getClientSize().width, component.getPreferredSize().height)); // Width of component is fixed, update height
    component.doLayout(); // layout component

    // Paint component into BufferedImage
    BufferedImage componentImage = new BufferedImage(component.getSize().width, component.getSize().height,
                                                     BufferedImage.TYPE_INT_RGB);
    component.printAll(componentImage.getGraphics());

    return componentImage;
}
 
Example 5
Source File: MultiLineToolTipUI.java    From mzmine2 with GNU General Public License v2.0 6 votes vote down vote up
public void paint(Graphics g, JComponent c) {
  Font font = c.getFont();
  JToolTip mtt = (JToolTip) c;
  FontMetrics fontMetrics = mtt.getFontMetrics(font);
  Dimension dimension = c.getSize();
  int fontHeight = fontMetrics.getHeight();
  int fontAscent = fontMetrics.getAscent();
  String tipText = ((JToolTip) c).getTipText();
  if (tipText == null)
    return;
  String lines[] = tipText.split("\n");
  int num_lines = lines.length;
  int height;
  int i;

  g.setColor(c.getBackground());
  g.fillRect(0, 0, dimension.width, dimension.height);
  g.setColor(c.getForeground());
  for (i = 0, height = 2 + fontAscent; i < num_lines; i++, height += fontHeight) {
    g.drawString(lines[i], inset, height);
  }
}
 
Example 6
Source File: AquaSeparatorUI.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void paint( Graphics g, JComponent c ) {
    Dimension s = c.getSize();

    if (((JSeparator) c).getOrientation() == JSeparator.HORIZONTAL) {
        g.setColor(lineColorHorizontal);
        g.drawLine(1, 5, s.width - 2, 5);
    } else {
        g.setColor(lineColorVertical);
        g.drawLine(0, 1, 0, s.height - 2);
    }
}
 
Example 7
Source File: FlatTitlePane.java    From FlatLaf with Apache License 2.0 5 votes vote down vote up
protected void addJBRHitTestSpot( JComponent c, boolean subtractMenuBarMargins, List<Rectangle> hitTestSpots ) {
	Dimension size = c.getSize();
	if( size.width <= 0 || size.height <= 0 )
		return;

	Point location = SwingUtilities.convertPoint( c, 0, 0, window );
	Rectangle r = new Rectangle( location, size );
	if( subtractMenuBarMargins )
		r = FlatUIUtils.subtractInsets( r, UIScale.scale( getMenuBarMargins() ) );
	// slightly increase rectangle so that component receives mouseExit events
	r.grow( 2, 2 );
	hitTestSpots.add( r );
}
 
Example 8
Source File: MockComponent.java    From pumpernickel with MIT License 5 votes vote down vote up
/**
 * Creates a MockComponent that resembles the argument component.
 * <P>
 * Note this method will traverse c and its subcomponents and may
 * temporarily change properties of inner components: such as the focused
 * state, the visibility, etc.
 * <P>
 * The goal is of this component is not to mirror the exact state of a
 * component, but rather to provide a sample image of this component in its
 * plain, unmodified, unused state.
 * 
 * @param c
 */
public MockComponent(JComponent c) {
	Dimension preferredSize = c.getPreferredSize();
	Dimension currentSize = c.getSize();

	Dimension d = new Dimension(Math.max(preferredSize.width,
			currentSize.width), Math.max(preferredSize.height,
			currentSize.height));

	if (currentSize.width == 0 || currentSize.height == 0) {
		// if the component isn't visible yet
		c.setSize(d);
		c.doLayout();
	}

	storeState(c);

	image = new BufferedImage(d.width, d.height,
			BufferedImage.TYPE_INT_ARGB);

	Graphics2D g = image.createGraphics();
	g.setComposite(AlphaComposite.Clear);
	g.fillRect(0, 0, d.width, d.height);
	g.setComposite(AlphaComposite.SrcOver);

	c.paint(g);
	g.dispose();
	setPreferredSize(d);
	setMinimumSize(d);
	setMaximumSize(d);
	setOpaque(c.isOpaque());
	setName(c.getName());
	setToolTipText(c.getToolTipText());

	restoreState(c);
}
 
Example 9
Source File: LuckPopupMenuSeparatorUI.java    From littleluck with Apache License 2.0 5 votes vote down vote up
@Override
public void paint(Graphics g, JComponent c)
{
    Dimension s = c.getSize();

    g.setColor(UIManager.getColor(LuckPopupMenuUIBundle.SEPEREATOR_COLOR));

    g.drawLine(0, 0, s.width, 0);
}
 
Example 10
Source File: MultiLineToolTip.java    From pdfxtk with Apache License 2.0 5 votes vote down vote up
public void paint(Graphics g, JComponent c) {
     FontMetrics metrics = g.getFontMetrics();
     Dimension size = c.getSize();
     g.setColor(c.getBackground());
     g.fillRect(0, 0, size.width, size.height);
     g.setColor(c.getForeground());
     int y = metrics.getAscent() + 1;
     int h = metrics.getHeight();
     if(lines != null)
for (int i = 0; i < lines.length; i++) {
  g.drawString(lines[i], 3, y);
  y += h;
}
   }
 
Example 11
Source File: DrawFrame.java    From java-experiments with Apache License 2.0 5 votes vote down vote up
private void drawLines(final JComponent canvas) {
	final Dimension size = canvas.getSize(); 
	int unit = size.height / 5; 
	for (int i = 0; i < size.height; i += unit) {
		final int y = i; 
		new Thread(new Runnable() {
			public void run() {
				drawLine(canvas, size.width, y); 
			}
		}).start(); 
	}
}
 
Example 12
Source File: BESeparatorUI.java    From beautyeye with Apache License 2.0 5 votes vote down vote up
@Override
	public void paint( Graphics g, JComponent c )
	{
		//** 绘制border的底线
		//虚线样式
		Stroke oldStroke = ((Graphics2D)g).getStroke();
		Stroke sroke = new BasicStroke(1, BasicStroke.CAP_BUTT,
				BasicStroke.JOIN_BEVEL, 0, new float[]{2, 2}, 0);//实线,空白
		((Graphics2D)g).setStroke(sroke);//
//		super.paint(g, c);
		
		Dimension s = c.getSize();

        if ( ((JSeparator)c).getOrientation() == JSeparator.VERTICAL )
        {
//        	System.out.println("c.getBackground()c.getBackground()c.getBackground()="+c.getBackground());
          g.setColor( c.getForeground() );
          g.drawLine( 0, 0, 0, s.height );

          g.setColor( c.getBackground() );
          g.drawLine( 1, 0, 1, s.height );
        }
        else  // HORIZONTAL
        {
          g.setColor( c.getForeground() );
          g.drawLine( 0, 0, s.width, 0 );

          g.setColor( c.getBackground() );
          g.drawLine( 0, 1, s.width, 1 );
        }
		
		((Graphics2D)g).setStroke(oldStroke);
	}
 
Example 13
Source File: SeaGlassTextFieldUI.java    From seaglass with Apache License 2.0 5 votes vote down vote up
/**
 * @see javax.swing.plaf.basic.BasicTextUI#getPreferredSize(javax.swing.JComponent)
 */
public Dimension getPreferredSize(JComponent c) {
    // The following code has been derived from BasicTextUI.
    Document  doc      = ((JTextComponent) c).getDocument();
    Insets    i        = c.getInsets();
    Dimension d        = c.getSize();
    View      rootView = getRootView((JTextComponent) c);

    if (doc instanceof AbstractDocument) {
        ((AbstractDocument) doc).readLock();
    }

    try {
        if ((d.width > (i.left + i.right)) && (d.height > (i.top + i.bottom))) {
            rootView.setSize(d.width - i.left - i.right, d.height - i.top - i.bottom);
        } else if (d.width == 0 && d.height == 0) {
            // Probably haven't been layed out yet, force some sort of
            // initial sizing.
            rootView.setSize(Integer.MAX_VALUE, Integer.MAX_VALUE);
        }

        d.width  = (int) Math.min((long) rootView.getPreferredSpan(View.X_AXIS) + (long) i.left + (long) i.right, Integer.MAX_VALUE);
        d.height = (int) Math.min((long) rootView.getPreferredSpan(View.Y_AXIS) + (long) i.top + (long) i.bottom, Integer.MAX_VALUE);
    } finally {
        if (doc instanceof AbstractDocument) {
            ((AbstractDocument) doc).readUnlock();
        }
    }

    // Fix: The preferred width is always two pixels too small on a Mac.
    d.width += 2;

    // We'd like our heights to be odd by default.
    if ((d.height & 1) == 0) {
        d.height--;
    }

    return d;
}
 
Example 14
Source File: DrawFrame.java    From java-experiments with Apache License 2.0 5 votes vote down vote up
private void drawLines(final JComponent canvas) {
	final Dimension size = canvas.getSize(); 
	int unit = size.height / 5; 
	for (int i = 0; i < size.height; i += unit) {
		final int y = i; 
		new Thread(new Runnable() {
			public void run() {
				drawLine(canvas, size.width, y); 
			}
		}).start(); 
	}
}
 
Example 15
Source File: DrawFrame.java    From java-experiments with Apache License 2.0 5 votes vote down vote up
private void drawLines(final JComponent canvas) {
	final Dimension size = canvas.getSize(); 
	int unit = size.height / 5; 
	for (int i = 0; i < size.height; i += unit) {
		final int y = i; 
		drawLine(canvas, size.width, y); 
	}
}
 
Example 16
Source File: MultiLineToolTipUI.java    From osp with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void paint(Graphics g, JComponent c) {
  Dimension size = c.getSize();
  textArea.setBackground(c.getBackground());
	rendererPane.paintComponent(g, textArea, c, 1, 1, size.width-1, size.height-1, true);
}
 
Example 17
Source File: BEToolBarSeparatorUI.java    From beautyeye with Apache License 2.0 4 votes vote down vote up
public void paint( Graphics g, JComponent c ) 
	{
		boolean vertical = ((JSeparator)c).getOrientation() == SwingConstants.VERTICAL;
		Dimension size = c.getSize();
		
		//虚线样式
		Stroke oldStroke = ((Graphics2D)g).getStroke();
		Stroke sroke = new BasicStroke(1, BasicStroke.CAP_BUTT,
				BasicStroke.JOIN_BEVEL, 0, new float[]{2, 2}, 0);//实线,空白
		((Graphics2D)g).setStroke(sroke);//

		Color temp = g.getColor();
		UIDefaults table = UIManager.getLookAndFeelDefaults();
		Color shadow = table.getColor("ToolBar.shadow");
		Color highlight = table.getColor("ToolBar.highlight");

		// TODO BUG_001:不知何故,垂直分隔条并不能像水平分隔条一样,拥有默认设置的new Dimension(6, 6)
		// 而只有new Dimension(1, ...),而当它floating时却能正常表现(只能绘出hilight而不能绘出shadow)
		//,有待深入研究,垂直的分隔条则不会有此种情况
		if (vertical) 
		{
			int x = (size.width / 2) - 1;
			
			//* 当BUG_001存在时,暂时使用以下代码解决:把本该显示hilight的
			//* 线条用shadow颜色绘制,最大可能保证ui的正常展现
//			g.setColor(shadow);
//			g.drawLine(x, 2, x, size.height - 2);
			g.setColor(shadow);//highlight);
			g.drawLine(x + 1, 2, x + 1, size.height - 2);
			
			//* 当BUG_001不存在时,应该使用以下代码
//			g.setColor(shadow);
//			g.drawLine(x, 2, x, size.height - 2);
//			g.setColor(highlight);
//			g.drawLine(x + 1, 2, x + 1, size.height - 2);
		} 
		else 
		{
			int y = (size.height / 2) - 1;
			g.setColor(shadow);
			g.drawLine(2, y, size.width - 2, y);

			g.setColor(highlight);
			g.drawLine(2, y + 1, size.width - 2, y + 1);
		}
		g.setColor(temp);

		//
		((Graphics2D)g).setStroke(oldStroke);
	}
 
Example 18
Source File: ComboBoxUI.java    From rapidminer-studio with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * Draws the combobox itself.
 */
private void paintBox(Graphics g, JComponent c) {
	int w = c.getWidth();
	int h = c.getHeight() - 1;
	if (w <= 0 || h <= 0) {
		return;
	}

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

	if (c.isEnabled()) {
		if (Boolean.parseBoolean(String.valueOf(c.getClientProperty(RapidLookTools.PROPERTY_INPUT_BACKGROUND_DARK)))) {
			g2.setColor(Colors.COMBOBOX_BACKGROUND_DARK);
		} else {
			g2.setColor(Colors.COMBOBOX_BACKGROUND);
		}
	} else {
		g2.setColor(Colors.COMBOBOX_BACKGROUND_DISABLED);
	}

	g2.fillRoundRect(0, 0, w - 1, h, RapidLookAndFeel.CORNER_DEFAULT_RADIUS, RapidLookAndFeel.CORNER_DEFAULT_RADIUS);

	// arrow
	int ny = c.getSize().height / 2 - 3;
	int nx = c.getWidth() - 15;

	if (isDown && c.isEnabled()) {
		nx++;
		ny++;
	}
	g2.translate(nx, ny);

	if (c.isEnabled()) {
		g2.setColor(Colors.COMBOBOX_ARROW);
	} else {
		g2.setColor(Colors.COMBOBOX_ARROW_DISABLED);
	}

	w = 14;
	Polygon arrow = new Polygon(new int[] { 0, 4, 8 }, new int[] { 0, 6, 0 }, 3);
	g2.fillPolygon(arrow);

	g2.translate(-nx, -ny);
}
 
Example 19
Source File: ZoomManager.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/**
 * Set the zoom factor for the Scene mananged by this ZoomManager
 * instance. The value represents a percentage (e.g. 100%) and
 * must be a positive number. Any value outside of the defined
 * range (<tt>MIN_ZOOM_PERCENT</tt> and <tt>MAX_ZOOM_PERCENT</tt>)
 * will be forced into that range.
 *
 * @param  percent  the percent value (e.g. 50 for half-size,
 *                  200 for double-size).
 * @param  center   the point at which to zoom in and keep centered.
 */
public void setZoom(int percent, Point center) {
    if (percent < MIN_ZOOM_PERCENT) {
        percent = MIN_ZOOM_PERCENT;
    } else if (percent > MAX_ZOOM_PERCENT) {
        percent = MAX_ZOOM_PERCENT;
    }

    // Find the current center point prior to zooming.
    Point sceneCenter = scene.convertViewToScene(center);
    zoomPercentage = percent;
    // Convert the percent value to the zoom factor Scene is expecting
    // (a double that acts as the multiplier to the component sizes and
    // locations, such that 0.5 is 50%, 1.0 is 100%, and 2.0 is 200%.
    double factor = ((double) percent) / 100.0d;
    scene.setZoomFactor(factor);
    // Setting the zoom factor alone is not enough, must force
    // validation and repainting of the scene for it to work.
    scene.validate();
    scene.repaint();

    // Find the new center point and scroll the view after zooming.
    Point newViewCenter = scene.convertSceneToView(sceneCenter);
    JComponent view = scene.getView();
    Rectangle visRect = view.getVisibleRect();
    visRect.x = newViewCenter.x - (center.x - visRect.x);
    visRect.y = newViewCenter.y - (center.y - visRect.y);
    Dimension viewSize = view.getSize();
    if (visRect.x + visRect.width > viewSize.width) {
        visRect.x = viewSize.width - visRect.width;
    }
    if (visRect.y + visRect.height > viewSize.height) {
        visRect.y = viewSize.height - visRect.height;
    }
    if (visRect.x < 0) {
        visRect.x = 0;
    }
    if (visRect.y < 0) {
        visRect.y = 0;
    }
    view.scrollRectToVisible(visRect);
    view.revalidate();
    view.repaint();

    // Notify registered listeners so they may update their state.
    fireZoomEvent(percent);
}