日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java只更新部分图像,java – 绘制从角落偏移的图像的某些部分...

發布時間:2023/12/20 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java只更新部分图像,java – 绘制从角落偏移的图像的某些部分... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

public abstract boolean drawImage(Image img,

int dx1,

int dy1,

int dx2,

int dy2,

int sx1,

int sy1,

int sx2,

int sy2,

ImageObserver observer)

Parameters:

img – the specified image to be drawn. This method does nothing if img is null.

dx1 – the x coordinate of the first corner of the destination rectangle.

dy1 – the y coordinate of the first corner of the destination rectangle.

dx2 – the x coordinate of the second corner of the destination rectangle.

dy2 – the y coordinate of the second corner of the destination rectangle.

sx1 – the x coordinate of the first corner of the source rectangle.

sy1 – the y coordinate of the first corner of the source rectangle.

sx2 – the x coordinate of the second corner of the source rectangle.

sy2 – the y coordinate of the second corner of the source rectangle.

observer – object to be notified as more of the image is scaled and converted.

ds是目的地,表示您希望在表面上繪制圖像的位置. ss是源圖像的坐標.對于兩者,第一個角是左上角,第二個角是右下角.

因此,您可以將源圖像視為要繪制的圖像的焦點部分.您可以使用源坐標確定要提取的矩形區域.

目標坐標布局實際繪制源區域的方式/位置.因此,如果您只想沿x軸移動繪制的圖像,則只需移動dx1和dx2即可.實際上,您可以通過指定大于或小于源矩形的坐標矩形來使用坐標來調整繪制區域的大小

Source Image Destination panel

sx1, sy1

+---------------+---------+ +-----------------------------+

| | | | |

| region to | | | dx1, dy1 |

| draw | | | +----------+ |

| | | | | | |

+---------------+ | | | | |

| sx2, sy2 | | +----------+ |

| | | dx2, dy2 |

| | | |

+-------------------------+ +-----------------------------+

這是一個運行的例子.

免責聲明:我的計算可能有點偏差.不確定我是否正在捕捉圖像的每個部分.只是快速地掀起了這個.

import java.awt.Dimension;

import java.awt.Graphics;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.image.BufferedImage;

import java.io.IOException;

import java.util.logging.Level;

import java.util.logging.Logger;

import javax.imageio.ImageIO;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.SwingUtilities;

import javax.swing.Timer;

public class NerdGirl extends JPanel {

private static final int SPRITE_ROWS = 5;

private static final int SPRITE_COLUMNS = 2;

private static final int DELAY = 150;

private int DIM_W;

private int DIM_H;

private int x1Src;

private int y1Src;

private int x2Src;

private int y2Src;

private BufferedImage img;

public NerdGirl() {

try {

img = ImageIO.read(getClass().getResource("/resources/nerd-girl.jpg"));

} catch (IOException ex) {

Logger.getLogger(NerdGirl.class.getName()).log(Level.SEVERE, null, ex);

}

DIM_W = img.getWidth() / SPRITE_ROWS;

DIM_H = img.getHeight() / SPRITE_COLUMNS;

x1Src = 0;

y1Src = 0;

x2Src = x1Src + DIM_W;

y2Src = y1Src + DIM_H;

Timer timer = new Timer(DELAY, new ActionListener() {

public void actionPerformed(ActionEvent e) {

if (x1Src >= img.getWidth() - DIM_H - 5) { // 5 to take care of precision loss

x1Src = 0;

x2Src = x1Src + DIM_W;

if (y1Src >= DIM_H - 5) { // 5 to take care of precision loss

y1Src = 0;

y2Src = y1Src + DIM_H;

} else {

y1Src += DIM_H;

y2Src = y1Src + DIM_H;

}

} else {

x1Src += DIM_W;

x2Src = x1Src + DIM_W;

}

repaint();

}

});

timer.start();

}

@Override

protected void paintComponent(Graphics g) {

super.paintComponent(g);

g.drawImage(img, 0, 0, getWidth(), getHeight(), x1Src, y1Src, x2Src, y2Src, this);

}

@Override

public Dimension getPreferredSize() {

return (img == null) ? new Dimension(300, 300) : new Dimension(DIM_W, DIM_H);

}

public static void main(String[] args) {

SwingUtilities.invokeLater(new Runnable() {

public void run() {

JFrame frame = new JFrame();

frame.add(new NerdGirl());

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.pack();

frame.setLocationRelativeTo(null);

frame.setVisible(true);

}

});

}

}

總結

以上是生活随笔為你收集整理的java只更新部分图像,java – 绘制从角落偏移的图像的某些部分...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。