java 图像在屏幕_java – 在任何屏幕分辨率中定位图像

我宁愿一直尝试在框架的边界内工作,从长远来看,它使生活变得更加容易.

Swing已经开始设计用于LayoutManagers,这意味着Swing更新它的组件并传达这些更改的方式是基于LayoutManagers的使用(或多或少).

下面的示例使用Java: maintaining aspect ratio of JPanel background image中的片段来缩放图像,以及一个PropertionalLayoutManager,它不仅可以根据父容器的大小尝试缩放组件的大小,还可以缩放组件的位置.

这里演示的PropertionalLayoutManager将尝试围绕父容器的中心布局组件.你可以改变它,但它看起来很奇怪 – 恕我直言

import java.awt.BorderLayout;

import java.awt.Component;

import java.awt.Container;

import java.awt.Dimension;

import java.awt.EventQueue;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.LayoutManager2;

import java.awt.RenderingHints;

import java.awt.Transparency;

import java.awt.image.BufferedImage;

import java.io.File;

import java.io.IOException;

import java.util.Map;

import java.util.WeakHashMap;

import javax.imageio.ImageIO;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.UIManager;

import javax.swing.UnsupportedLookAndFeelException;

public class Drums {

protected static BufferedImage SYMBOL;

protected static BufferedImage DRUM;

public static void main(String[] args) {

new Drums();

}

public Drums() {

EventQueue.invokeLater(new Runnable() {

@Override

public void run() {

try {

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {

}

JFrame frame = new JFrame("Testing");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setLayout(new BorderLayout());

frame.add(new TestPane());

frame.pack();

frame.setLocationRelativeTo(null);

frame.setVisible(true);

}

});

}

public class TestPane extends JPanel {

public TestPane() {

setLayout(new PropertionalLayoutManager(400, 400));

add(new Symbol(), new PropertionalConstraint(0f, 0));

add(new Symbol(), new PropertionalConstraint(0.67f, 0));

add(new Symbol(), new PropertionalConstraint(0f, 0.4675f));

add(new Symbol(), new PropertionalConstraint(0.67f, 0.4675f));

add(new Drum(), new PropertionalConstraint(0.205f, 0.1f));

add(new Drum(), new PropertionalConstraint(0.5f, 0.1f));

add(new Drum(), new PropertionalConstraint(0f, 0.33f));

add(new Drum(), new PropertionalConstraint(0.705f, 0.33f));

}

}

public class PropertionalConstraint {

private float x;

private float y;

public PropertionalConstraint(float x, float y) {

this.x = x;

this.y = y;

}

public float getX() {

return x;

}

public float getY() {

return y;

}

}

public class PropertionalLayoutManager implements LayoutManager2 {

private Map constraints;

private Dimension defaultSize;

public PropertionalLayoutManager(int defaultWidth, int defaultHeight) {

constraints = new WeakHashMap<>(25);

defaultSize = new Dimension(defaultWidth, defaultHeight);

}

@Override

public void addLayoutComponent(Component comp, Object constraint) {

if (constraint instanceof PropertionalConstraint) {

constraints.put(comp, ((PropertionalConstraint) constraint));

}

}

@Override

public Dimension maximumLayoutSize(Container target) {

return preferredLayoutSize(target);

}

@Override

public float getLayoutAlignmentX(Container target) {

return 0.5f;

}

@Override

public float getLayoutAlignmentY(Container target) {

return 0.5f;

}

@Override

public void invalidateLayout(Container target) {

}

@Override

public void addLayoutComponent(String name, Component comp) {

}

@Override

public void removeLayoutComponent(Component comp) {

constraints.remove(comp);

}

@Override

public Dimension preferredLayoutSize(Container parent) {

return defaultSize;

}

@Override

public Dimension minimumLayoutSize(Container parent) {

return preferredLayoutSize(parent);

}

@Override

public void layoutContainer(Container parent) {

int width = parent.getWidth();

int height = parent.getHeight();

double dScaleWidth = getScaleFactor(defaultSize.width, width);

double dScaleHeight = getScaleFactor(defaultSize.height, height);

double scaleSize = Math.min(dScaleHeight, dScaleWidth);

int minRange = Math.min(width, height);

if (width > 0 && height > 0) {

int maxY = 0;

int maxX = 0;

for (Component comp : parent.getComponents()) {

PropertionalConstraint p = constraints.get(comp);

if (p != null) {

Dimension prefSize = comp.getPreferredSize();

prefSize.width *= scaleSize;

prefSize.height *= scaleSize;

int x = Math.round(minRange * p.getX());

int y = Math.round(minRange * p.getY());

comp.setBounds(x, y, prefSize.width, prefSize.height);

maxX = Math.max(maxX, x + prefSize.width);

maxY = Math.max(maxY, y + prefSize.height);

} else {

comp.setBounds(0, 0, 0, 0);

}

}

for (Component comp : parent.getComponents()) {

System.out.println("maxX = " + maxX);

System.out.println("maxY = " + maxY);

if (comp.getWidth() > 0 && comp.getHeight() > 0) {

int x = ((width - maxX) / 2) + comp.getX();

int y = ((height - maxY) / 2) + comp.getY();

comp.setLocation(x, y);

}

}

} else {

for (Component comp : parent.getComponents()) {

comp.setBounds(0, 0, 0, 0);

}

}

}

}

public abstract class AbstractKitPiecePane extends JPanel {

private BufferedImage scaled;

public AbstractKitPiecePane() {

setOpaque(false);

}

public abstract BufferedImage getKitImage();

@Override

public Dimension getPreferredSize() {

return new Dimension(getKitImage().getWidth(), getKitImage().getHeight());

}

@Override

public void invalidate() {

super.invalidate();

if (getWidth() > 0 && getHeight() > 0) {

scaled = getScaledInstanceToFit(getKitImage(), getSize());

} else {

scaled = null;

}

}

@Override

protected void paintComponent(Graphics g) {

super.paintComponent(g);

if (scaled != null) {

int x = (getWidth() - scaled.getWidth()) / 2;

int y = (getHeight() - scaled.getHeight()) / 2;

g.drawImage(scaled, x, y, this);

}

}

}

public class Drum extends AbstractKitPiecePane {

@Override

public BufferedImage getKitImage() {

return DRUM;

}

}

public class Symbol extends AbstractKitPiecePane {

@Override

public BufferedImage getKitImage() {

return SYMBOL;

}

}

protected static BufferedImage getScaledInstance(BufferedImage img, double dScaleFactor) {

BufferedImage imgScale = img;

int iImageWidth = (int) Math.round(img.getWidth() * dScaleFactor);

int iImageHeight = (int) Math.round(img.getHeight() * dScaleFactor);

if (dScaleFactor <= 1.0d) {

imgScale = getScaledDownInstance(img, iImageWidth, iImageHeight, RenderingHints.VALUE_INTERPOLATION_BILINEAR);

} else {

imgScale = getScaledUpInstance(img, iImageWidth, iImageHeight, RenderingHints.VALUE_INTERPOLATION_BILINEAR);

}

return imgScale;

}

protected static BufferedImage getScaledDownInstance(BufferedImage img,

int targetWidth,

int targetHeight,

Object hint) {

// System.out.println("Scale down...");

int type = (img.getTransparency() == Transparency.OPAQUE)

? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB;

BufferedImage ret = (BufferedImage) img;

if (targetHeight > 0 || targetWidth > 0) {

int w, h;

w = img.getWidth();

h = img.getHeight();

do {

if (w > targetWidth) {

w /= 2;

if (w < targetWidth) {

w = targetWidth;

}

}

if (h > targetHeight) {

h /= 2;

if (h < targetHeight) {

h = targetHeight;

}

}

BufferedImage tmp = new BufferedImage(Math.max(w, 1), Math.max(h, 1), type);

Graphics2D g2 = tmp.createGraphics();

g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, hint);

g2.drawImage(ret, 0, 0, w, h, null);

g2.dispose();

ret = tmp;

} while (w != targetWidth || h != targetHeight);

} else {

ret = new BufferedImage(1, 1, type);

}

return ret;

}

protected static BufferedImage getScaledUpInstance(BufferedImage img,

int targetWidth,

int targetHeight,

Object hint) {

int type = BufferedImage.TYPE_INT_ARGB;

BufferedImage ret = (BufferedImage) img;

int w, h;

w = img.getWidth();

h = img.getHeight();

do {

if (w < targetWidth) {

w *= 2;

if (w > targetWidth) {

w = targetWidth;

}

}

if (h < targetHeight) {

h *= 2;

if (h > targetHeight) {

h = targetHeight;

}

}

BufferedImage tmp = new BufferedImage(w, h, type);

Graphics2D g2 = tmp.createGraphics();

g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, hint);

g2.drawImage(ret, 0, 0, w, h, null);

g2.dispose();

ret = tmp;

tmp = null;

} while (w != targetWidth || h != targetHeight);

return ret;

}

public static BufferedImage getScaledInstanceToFit(BufferedImage img, Dimension size) {

double scaleFactor = getScaleFactorToFit(img, size);

return getScaledInstance(img, scaleFactor);

}

public static double getScaleFactorToFit(BufferedImage img, Dimension size) {

double dScale = 1;

if (img != null) {

int imageWidth = img.getWidth();

int imageHeight = img.getHeight();

dScale = getScaleFactorToFit(new Dimension(imageWidth, imageHeight), size);

}

return dScale;

}

public static double getScaleFactorToFit(Dimension original, Dimension toFit) {

double dScale = 1d;

if (original != null && toFit != null) {

double dScaleWidth = getScaleFactor(original.width, toFit.width);

double dScaleHeight = getScaleFactor(original.height, toFit.height);

dScale = Math.min(dScaleHeight, dScaleWidth);

}

return dScale;

}

public static double getScaleFactor(int iMasterSize, int iTargetSize) {

double dScale = 1;

if (iMasterSize > iTargetSize) {

dScale = (double) iTargetSize / (double) iMasterSize;

} else {

dScale = (double) iTargetSize / (double) iMasterSize;

}

return dScale;

}

static {

try {

SYMBOL = ImageIO.read(new File("Symbol.png"));

} catch (IOException ex) {

ex.printStackTrace();

}

try {

DRUM = ImageIO.read(new File("Drum.png"));

} catch (IOException ex) {

ex.printStackTrace();

}

}

}

以及程序中使用的图像,供您享受.它们位于运行程序的同一目录中

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Java屏幕坐标单位是像素(pixel)。像素是计算机显示屏上的最小单位,每个像素都可以显示不同的颜色和亮度。在Java,使用像素来描述图形界面的组件位置和大小。例如,一个按钮可以在屏幕上被定义为宽度为100像素,高度为50像素的矩形区域。 ### 回答2: 在Java屏幕坐标单位使用像素(Pixel)作为度量单位。像素是屏幕上最小的显示单元,也是图像和界面元素的基本构成单元。每个像素都有一个唯一的坐标位置,通过X轴和Y轴的数值来确定。 屏幕的左上角是坐标原点,其坐标值为(0,0),X轴向右增加,Y轴向下增加。例如,(100, 200)表示在X轴上偏离原点100个像素,在Y轴上偏离原点200个像素,即为屏幕上的一个位置。 在Java,可以使用Graphics类的坐标系方法绘制图像或界面元素,例如通过调用drawImage()方法可以在指定的坐标位置绘制图像。 需要注意的是,屏幕的坐标单位是相对于屏幕分辨率的,不同显示设备的分辨率可能不同,因此在编写Java程序时需要考虑不同设备的屏幕分辨率,保证界面的适配性和兼容性。 总而言之,Java屏幕坐标单位是像素,通过X轴和Y轴的数值来确定在屏幕上的位置,左上角为坐标原点,X轴向右增加,Y轴向下增加。在编写程序时需要考虑不同设备的分辨率,以保证界面的适配性。 ### 回答3: 在Java屏幕坐标单位是以像素为基础的。像素是屏幕上显示的最小单位,它代表了图像上的一个点。在Java屏幕坐标的原点通常位于屏幕的左上角,x轴向右延伸,y轴向下延伸。每个像素的坐标值由其相对于原点的位置来确定。 Java提供了一些类和方法来使用屏幕坐标单位。例如,java.awt包的Graphics类和Graphics2D类提供了绘制图形和文本的方法,这些方法使用的参数就是以像素为单位的坐标值。通过这些方法,我们可以在屏幕上绘制直线、矩形、圆形等各种图形。 除了绘图,屏幕坐标单位还广泛应用于用户界面设计和图形计算等领域。在用户界面设计,我们可以使用像素单位来指定窗口的大小、按钮的位置等,这样可以精确地控制界面元素的布局。在图形计算,我们可以使用像素单位来进行图像处理、图像变换等操作,这样可以方便地对图像进行像素级别的操作。 总之,Java屏幕坐标单位是以像素为基础的,通过使用像素单位,我们可以在屏幕上进行图形绘制、界面设计和图像处理等各种操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值