下面列出了javax.swing.JPanel#getHeight ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 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));
}
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();
}
}
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;
}
/**
* 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);
}
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;
}
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;
}
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;
}
static private BufferedImage getScreenShot(JPanel panel) {
BufferedImage bi = new BufferedImage(
panel.getWidth(), panel.getHeight(), BufferedImage.TYPE_INT_ARGB);
panel.paint(bi.getGraphics());
return bi;
}
private Image createPanelImage(JPanel panel) {
return new BufferedImage(panel.getWidth(), panel.getHeight(),
BufferedImage.TYPE_INT_ARGB);
}