「java扭曲图片」java图片翻转代码
本篇文章给大家谈谈java扭曲图片,以及java图片翻转代码对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
- 1、怎么用java切割出不规则图形的图片
- 2、Java的Graphics2D的rotate这函数来旋转图片,转动后导致锯齿
- 3、java如何实现图片拖动,放大缩小,旋转。
- 4、java 怎么让一个图形绕一个点旋转360度
- 5、Java如何绘制扭曲的图像?
怎么用java切割出不规则图形的图片
//用基本图形拼呀
import java.awt.Graphics;
import javax.swing.*;
public class IrregulaShape extends JPanel{
@Override
public void paint(Graphics g) {
super.paint(g);
g.drawRect(100, 50,100, 100);
g.drawArc(100, 25, 50, 50, 0, 180);
// g.fillRect(100, 50, 100, 100);
// g.fillOval(100, 25, 50, 50);
}
public static void main(String[] args) {
JFrame jFrame = new JFrame("不规则的!");
IrregulaShape j = new IrregulaShape();
jFrame.add(j);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.setSize(500,500);
jFrame.setLocationRelativeTo(null);
jFrame.setVisible(true);
}
}
Java的Graphics2D的rotate这函数来旋转图片,转动后导致锯齿
用原始图旋转各个角度,而不用旋转后的已损图再转..
可以避免。。通常就够了。
实在效果不好可以放大八倍再转再缩小....
java如何实现图片拖动,放大缩小,旋转。
这个只是实现了移动,你参考以下吧 !
public class MoveImage {
static int x,y;
private static int num=0;
private static Icon icon=null;
public static void main(String[] args) throws Exception{
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().setLayout(null);//这个要设置成 null
//图片
icon = new ImageIcon("d:/test.gif");//d:/test.gif本地一张图片
JLabel l = new JLabel(icon); //创建具有指定图像的 JLabel 实例。
l.setSize(icon.getIconWidth(),icon.getIconHeight());//设置面板的宽度和高度
l.setBorder(BorderFactory.createLineBorder(Color.red));//给图片加上红色外框
f.getContentPane().add(l);
f.setSize(900,700);
f.setVisible(true);
l.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
x=e.getX();
y=e.getY();
}
});
l.addMouseMotionListener(new MouseMotionListener(){
public void mouseDragged(MouseEvent e) {
JLabel l = (JLabel)e.getSource();
l.setLocation(l.getX()+e.getX()-x,l.getY()+e.getY()-y);
}
public void mouseMoved(MouseEvent e) {}
});
}
java 怎么让一个图形绕一个点旋转360度
import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
/**
* @author ZhengYesheng
*/
public class RotateImageCanvas extends Canvas implements Runnable
{
private static final long serialVersionUID = -1997487731464495923L;
BufferedImage img;
BufferedImage rotatedImg;
int degress = 0;
public RotateImageCanvas(BufferedImage img)
{
super();
this.img = img;
new Thread(this).start();
}
@Override
public void run()
{
while (true)
{
//A,与B的代码配合决定旋转的速度
degress += 1;
degress %= 360;
repaint();
try
{
if (degress == 0)
{
//绕一周后等待的时间在这里设置
Thread.sleep(3 * 1000);
}
else
{
//考虑到视觉平滑,这里不应大约40。
Thread.sleep(30);
}
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Override
public void paint(Graphics graphics)
{
super.paint(graphics);
//获取旋转指定角度后的图片。为了避免累计误差,这里是用原始图像旋转的
rotatedImg = rotateImage(img, degress);
//绘制旋转后的图片
graphics.drawImage(rotatedImg, 0, 0, this);
}
/**
* 旋转图片为指定角度。
* 注意:1、这个方法实现了图像的基于中点的旋转,要想绕指定点,需要配合Matrix类
* 2、为避免图像被裁切,结果图片的尺寸也需要动态计算
* 3、现在旋转后有黑色背景,如果不需要这个效果,需要设置结果图片的Alpha模式
*
* @param bufferedimage
* 目标图像
* @param degree
* 旋转角度
* @return
*/
private BufferedImage rotateImage(BufferedImage bufferedimage, int degree)
{
int w = bufferedimage.getWidth();
int h = bufferedimage.getHeight();
int type = bufferedimage.getColorModel().getTransparency();
BufferedImage img;
Graphics2D graphics2d;
(graphics2d = (img = new BufferedImage(w, h, type)).createGraphics())
.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2d.rotate(Math.toRadians(degree), w / 2, h / 2);
graphics2d.drawImage(bufferedimage, 0, 0, null);
graphics2d.dispose();
return img;
}
}
Java如何绘制扭曲的图像?
先给你个最简单的例子,你就明白应该怎么做了:
如果叫你用 graphics2d 去画一条线,你知道是 x1, y1, --- x2, y2 两个点,然后使用 drawline 方法就可以实现画这一条直线,
如果叫你用 graphics2d 去画一条正弦曲线,你就知道不是两点的方法能画出来的了。
所以,上面的例子其实是说,你在网上找一个 jar 类库专门用来实现这种功能的就可以了。
java扭曲图片的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java图片翻转代码、java扭曲图片的信息别忘了在本站进行查找喔。