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

The following examples show how to use javax.swing.JPanel#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: ResourceImageFactory.java    From triplea with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Returns button with resource amounts and given text. If resources is empty then returns button
 * with just the text.
 */
public JButton getResourcesButton(final ResourceCollection resources, final String text) {
  if (resources.isEmpty()) {
    return new JButton(text);
  }
  final JPanel panel = getResourcesPanel(resources);
  panel.setOpaque(false);
  panel.add(new JLabel(text));
  panel.setSize(panel.getPreferredSize());
  panel.doLayout();
  final BufferedImage image =
      new BufferedImage(panel.getWidth(), panel.getHeight(), BufferedImage.TYPE_INT_ARGB);
  final Graphics2D g = image.createGraphics();
  panel.paint(g);
  g.dispose();
  return new JButton(new ImageIcon(image));
}
 
Example 2
Source File: WordCloudMenuBar.java    From swcv with MIT License 6 votes vote down vote up
private void exportPNG(final JPanel panel, String selectedFile)
{
    BufferedImage img = new BufferedImage(panel.getWidth(), panel.getHeight(), BufferedImage.TYPE_INT_RGB);
    Graphics2D g2d = img.createGraphics();
    panel.printAll(g2d);
    g2d.dispose();
    
    try
    {
        ImageIO.write(img, "png", new File(selectedFile));
    }
    catch (IOException ex)
    {
        ex.printStackTrace();
    }
}
 
Example 3
Source File: ViewBasic.java    From CyberBiology with GNU General Public License v3.0 5 votes vote down vote up
public Image paint(World world,JPanel canvas) {
    	int w = canvas.getWidth();
    	int h = canvas.getHeight();
    	//Создаем временный буфер для рисования
    	Image buf = canvas.createImage(w, h);
    	//подеменяем графику на временный буфер
    	Graphics g = buf.getGraphics();
    	
        g.drawRect(0, 0, world.width * World.BOTW + 1, world.height * World.BOTH + 1);

        world.population = 0;
        world.organic = 0;
        for (int y = 0; y < world.height; y++) {
            for (int x = 0; x < world.width; x++) {
                if (world.matrix[x][y] == null) {
                    g.setColor(Color.WHITE);
                    g.fillRect(x * World.BOTW,y * World.BOTH, World.BOTW, World.BOTH);
                } else if ((world.matrix[x][y].alive == 1) || (world.matrix[x][y].alive == 2)) {
                    g.setColor(new Color(200, 200, 200));
                    g.fillRect(x * World.BOTW, y * World.BOTH, World.BOTW, World.BOTH);
                    world.organic = world.organic + 1;
                } else if (world.matrix[x][y].alive == 3) {
                    g.setColor(Color.BLACK);
                    g.drawRect(x * World.BOTW, y * World.BOTH, World.BOTW, World.BOTH);

//                    g.setColor(new Color(matrix[x][y].c_red, matrix[x][y].c_green, matrix[x][y].c_blue));
                    int green = (int) (world.matrix[x][y].c_green - ((world.matrix[x][y].c_green * world.matrix[x][y].health) / 2000));
                    if (green < 0) green = 0;
                    if (green > 255) green = 255;
                    int blue = (int) (world.matrix[x][y].c_blue * 0.8 - ((world.matrix[x][y].c_blue * world.matrix[x][y].mineral) / 2000));
                    g.setColor(new Color(world.matrix[x][y].c_red, green, blue));
//                    g.setColor(new Color(matrix[x][y].c_red, matrix[x][y].c_green, matrix[x][y].c_blue));
                    g.fillRect(x * World.BOTW + 1, y * World.BOTH + 1,World.BOTW-1, World.BOTH-1);
                    world.population = world.population + 1;
                }
            }
        }
        return buf;
    }
 
Example 4
Source File: PumpernickelShowcaseApp.java    From pumpernickel with MIT License 5 votes vote down vote up
/**
 * Capture a screenshot based on the position of the given panel.
 * <p>
 * This uses a Robot to actually capture the real screenshot in case other
 * floating layers/windows are meant to be captured.
 */
private BufferedImage getScreenshot(JPanel panel) throws Exception {
	Robot robot = new Robot();
	Point p = panel.getLocationOnScreen();
	Rectangle screenRect = new Rectangle(p.x, p.y, panel.getWidth(),
			panel.getHeight());
	return robot.createScreenCapture(screenRect);
}
 
Example 5
Source File: QPanelUI.java    From pumpernickel with MIT License 5 votes vote down vote up
private Outline getOutline(JPanel c, boolean ideal) {
	String key = ideal ? PROPERTY_CACHED_SCRATCH_OUTLINE
			: PROPERTY_CACHED_REAL_OUTLINE;
	Outline outline = (Outline) c.getClientProperty(key);
	int effectiveWidth = ideal ? 1000 : c.getWidth();
	int effectiveHeight = ideal ? 1000 : c.getHeight();

	if (outline == null
			|| !outline.isValid(effectiveWidth, effectiveHeight)) {
		outline = new Outline(effectiveWidth, effectiveHeight);
		c.putClientProperty(key, outline);
	}

	return outline;
}
 
Example 6
Source File: ImageWriter.java    From SciGraph with Apache License 2.0 5 votes vote down vote up
private static BufferedImage renderImage(JPanel panel) {
  JFrame frame = new JFrame();
  frame.setUndecorated(true);
  frame.getContentPane().add(panel);
  frame.pack();
  BufferedImage bi = new BufferedImage(panel.getWidth(), panel.getHeight(), BufferedImage.TYPE_INT_ARGB);
  Graphics2D graphics = bi.createGraphics();
  panel.print(graphics);
  graphics.dispose();
  frame.dispose();
  return bi;
}
 
Example 7
Source File: ViewMultiCell.java    From CyberBiology with GNU General Public License v3.0 4 votes vote down vote up
public Image paint(World world,JPanel canvas) {
	int w = canvas.getWidth();
	int h = canvas.getHeight();
	//Создаем временный буфер для рисования
	Image buf = canvas.createImage(w, h);
	//подеменяем графику на временный буфер
	Graphics g = buf.getGraphics();
	
    g.drawRect(0, 0, world.width * World.BOTW + 1, world.height * 4 + 1);

    world.population = 0;
    world.organic = 0;
    for (int y = 0; y < world.height; y++) {
        for (int x = 0; x < world.width; x++) {
            if (world.matrix[x][y] == null) {
                g.setColor(Color.WHITE);
                g.fillRect(x * World.BOTW,y * World.BOTH, World.BOTW, World.BOTH);
            } else if ((world.matrix[x][y].alive == 1) || (world.matrix[x][y].alive == 2)) {
                g.setColor(new Color(200, 200, 200));
                g.fillRect(x * World.BOTW, y * World.BOTH, World.BOTW, World.BOTH);
                world.organic = world.organic + 1;
            } else if (world.matrix[x][y].alive == 3) {
            	g.setColor(Color.BLACK);
                g.drawRect(x * World.BOTW, y * World.BOTH, World.BOTW, World.BOTH);
            	switch(world.matrix[x][y].isMulti())
        		{
       			
        			case 1:// - есть MPREV,
	                    g.setColor(Color.MAGENTA);
	                    g.fillRect(x * World.BOTW + 1, y * World.BOTH + 1, World.BOTW-1, World.BOTH-1);
        				break;
        			case 2:// - есть MNEXT,
	                    g.setColor(Color.BLACK);
	                    g.fillRect(x * World.BOTW + 1, y * World.BOTH + 1, World.BOTW-1, World.BOTH-1);
        				break;
        			case 3:// есть MPREV и MNEXT
	                    g.setColor(Color.MAGENTA);
	                    g.fillRect(x * World.BOTW + 1, y * World.BOTH + 1, World.BOTW-1, World.BOTH-1);
        				break;
        			default:
	                    int green = (int) (world.matrix[x][y].c_green - ((world.matrix[x][y].c_green * world.matrix[x][y].health) / 2000));
	                    if (green < 0) green = 0;
	                    if (green > 255) green = 255;
	                    int blue = (int) (world.matrix[x][y].c_blue * 0.8 - ((world.matrix[x][y].c_blue * world.matrix[x][y].mineral) / 2000));
	                    g.setColor(new Color(world.matrix[x][y].c_red, green, blue));
	                    g.fillRect(x * World.BOTW + 1, y * World.BOTH + 1, World.BOTW-1, World.BOTH-1);
        					break;
        		}
                
                world.population = world.population + 1;
            }
        }
    }
    return buf;
}
 
Example 8
Source File: DrawTest.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
static private BufferedImage getScreenShot(JPanel panel) {
    BufferedImage bi = new BufferedImage(
            panel.getWidth(), panel.getHeight(), BufferedImage.TYPE_INT_ARGB);
    panel.paint(bi.getGraphics());
    return bi;
}
 
Example 9
Source File: AbstractAdapterEditor.java    From snap-desktop with GNU General Public License v3.0 4 votes vote down vote up
void addComboField(JPanel parent, String labelText, String propertyName, List<String> values) {
    JLabel jLabel = new JLabel(labelText);
    parent.add(jLabel);

    PropertyDescriptor propertyDescriptor = propertyContainer.getDescriptor(propertyName);
    propertyDescriptor.setNotEmpty(true);

    values.sort(Comparator.naturalOrder());

    propertyDescriptor.setValueSet(new ValueSet(values.toArray()));
    PropertyEditor editor = PropertyEditorRegistry.getInstance().findPropertyEditor(propertyDescriptor);
    JComponent editorComp = editor.createEditorComponent(propertyDescriptor, bindingContext);
    if (editorComp instanceof JComboBox) {
        JComboBox comboBox = (JComboBox)editorComp;
        comboBox.setEditable(true);
    }
    editorComp.setMaximumSize(new Dimension(editorComp.getMaximumSize().width, controlHeight));

    customMenuLocation = new JTextField();
    customMenuLocation.setInputVerifier(new RequiredFieldValidator(Bundle.MSG_Empty_MenuLocation_Text()));
    customMenuLocation.setEnabled(false);

    JPanel subPanel = new JPanel(new SpringLayout());
    subPanel.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY));
    JRadioButton rbExistingMenu = new JRadioButton(Bundle.CTL_Label_RadioButton_ExistingMenus(), true);
    rbMenuNew = new JRadioButton(Bundle.CTL_Label_RadioButton_NewMenu());
    ButtonGroup rbGroup = new ButtonGroup();
    rbGroup.add(rbExistingMenu);
    rbGroup.add(rbMenuNew);
    // this radio button should be able to capture focus even when the validator of the rbMenuNew says otherwise
    rbExistingMenu.setVerifyInputWhenFocusTarget(false);
    rbExistingMenu.addItemListener(e -> {
        editorComp.setEnabled(e.getStateChange() == ItemEvent.SELECTED);
        customMenuLocation.setEnabled(e.getStateChange() == ItemEvent.DESELECTED);
    });
    subPanel.add(rbExistingMenu);
    subPanel.add(rbMenuNew);
    jLabel.setLabelFor(editorComp);
    subPanel.add(editorComp);
    subPanel.add(customMenuLocation);

    Dimension dimension = new Dimension(parent.getWidth() / 2, controlHeight);
    editorComp.setPreferredSize(dimension);
    customMenuLocation.setPreferredSize(dimension);

    subPanel.setPreferredSize(new Dimension(subPanel.getWidth(), (int)(2.5 * controlHeight)));
    subPanel.setMaximumSize(new Dimension(subPanel.getWidth(), (int) (2.5 * controlHeight)));

    makeCompactGrid(subPanel, 2, 2, DEFAULT_PADDING, DEFAULT_PADDING, DEFAULT_PADDING, DEFAULT_PADDING);

    parent.add(subPanel);
}
 
Example 10
Source File: RenderManager.java    From jts with GNU Lesser General Public License v2.1 4 votes vote down vote up
private Image createPanelImage(JPanel panel) {
  return new BufferedImage(panel.getWidth(), panel.getHeight(),
      BufferedImage.TYPE_INT_ARGB);
}