java图像平移代码 java实现图像的平移
用JAVA语言写一个二维平面图形(如长方形)的平移,旋转,缩放的程序.
import javax.swing.*;
创新互联建站专注于企业营销型网站、网站重做改版、梓潼网站定制设计、自适应品牌网站建设、H5页面制作、成都商城网站开发、集团公司官网建设、外贸网站制作、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为梓潼等各大城市提供网站开发制作服务。
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.geom.*;
/**
* @author Hardneedl
*/
class RectTransform extends JFrame {
private static final Dimension minSize = new Dimension(300, 200);
private static final Dimension maxSize = new Dimension(1024, 768);
private static final Dimension preferredSize = new Dimension(600, 400);
public Dimension getMaximumSize() {return maxSize;}
public Dimension getMinimumSize() {return minSize;}
public Dimension getPreferredSize() {return preferredSize;}
public String getTitle() {return "Frame Title";}
private AffineTransform af = new AffineTransform();
private Stroke stroke = new BasicStroke(2.0f);
private Rectangle rct = new Rectangle(40,140,200,140);
private JComponent canvas = new JComponent(){
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLACK);
g.fillRect(0,0,getWidth(),getHeight());
Graphics2D g2d = (Graphics2D)g.create();
g2d.setColor(Color.YELLOW);
g2d.setTransform(af);
g2d.setStroke(stroke);
g2d.draw(rct);
g2d.dispose();
}
};
RectTransform() throws HeadlessException {
init();
doLay();
attachListeners();
}
private void init() {
}
private void doLay() {
Container container = getContentPane();
JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER,30,5));
panel.add(new JButton( new AfAction("move", AffineTransform.getTranslateInstance(100,100)) ));
panel.add(new JButton( new AfAction("rotate", AffineTransform.getRotateInstance(Math.PI/3,40,120) ) ));
panel.add(new JButton( new AfAction("zoomIn", AffineTransform.getScaleInstance(2,2)) ));
panel.add(new JButton( new AfAction("ZoomOut", AffineTransform.getScaleInstance(.5d,.5d)) ));
panel.add(new JButton( new AfAction("Original", new AffineTransform()) ) );
container.add(panel,BorderLayout.NORTH);
container.add(canvas,BorderLayout.CENTER);
pack();
}
private void attachListeners() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private class AfAction extends AbstractAction {
private AffineTransform aff;
private AfAction(String n,AffineTransform af) {
super(n);
this.aff = af;
}
public void actionPerformed(ActionEvent e) {
af.setTransform(aff);
if(canvas.isVisible()) canvas.paintImmediately(0,0,getWidth(),getHeight());
}
}
public static void main(String[] args) {
new RectTransform().setVisible(true);
}
}
用java怎样编写一个二维坐标平移程序
java编写二维坐标平移程序,主要是通过类继承Point2D,使用里面的方法来平移,如下代码:
class Point2D
{
int x, y;
Point2D(){ }
Point2D(int i,int j)
{
x=i;
y=j;
}
void offset(int a, int b)
{
x=x+a;
y=y+b;
}
void distance(Point2D a,Point2D b)
{
float m;
m=(float)Math.sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
System.out.print("二维空间两点之间的距离:");
System.out.println("m="+m);
}
}
public class Point3D extends Point2D
{
int x,y,z;
Point3D(int x,int y,int z)
{
this.x=x;
this.y=y;
this.z=z;
}
Point3D(Point2D p,int z)
{
x=p.x;
y=p.y;
this.z=z;
}
void offset(int a, int b,int c)
{
x=x+a;
b=x+b;
c=x+c;
}
void distance(Point3D a,Point3D b)
{
float n;
n=(float)Math.sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)+(a.y-b.y)*(a.y-b.y));//计算两点之间的距离。
System.out.print("三维空间两点之间的距离:");
System.out.println("n="+n);
}
public static void main(String[] args)
{
Point2D p2d1=new Point2D(2,3);
Point2D p2d2=new Point2D(3,6);
Point3D p2d3=new Point3D(1,2,3);
Point3D p2d4=new Point3D(p2d1,3);
p2d1.distance(p2d1,p2d2);
p2d3.distance(p2d3,p2d4);//平移一段距离。
}
}
java里面添加图片,并对图片进行翻转处理
(注,下面的(x,y)就是你的图片所有的点围绕旋转的中心,为(0,0)即为绕原点进行旋转
当然是用矩阵啦,左乘一个三阶矩阵
(cosA, -sinA, x,sinA, cosA, y,0, 0 , s)
其中a代表你要旋转的角度 ;旋转变幻(绕原点,绕其他点直接平移即可)即为 X’ = X * cosA - Y * sinA; Y' = X * sinA + Y * cosA;
可以用极坐标轻松证明之,建议你自己推导一遍,,,,,,
(x,y)代表平移的坐标,不平移就为0, s代表缩放的倍数,,不缩放为1;
如果不会用矩阵或者不想用的话就直接用上面那个给出的公式进行变幻,,,,,,
还是用矩阵吧,,方便一点。。。。
文章标题:java图像平移代码 java实现图像的平移
网页地址:http://myzitong.com/article/dodpcoh.html