图像平移的java代码 图像平移的java代码有哪些
用JAVA语言写一个二维平面图形(如长方形)的平移,旋转,缩放的程序.
import javax.swing.*;
在巨鹿等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供网站设计制作、成都网站制作 网站设计制作定制网站开发,公司网站建设,企业网站建设,品牌网站制作,网络营销推广,外贸网站制作,巨鹿网站建设费用合理。
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);//平移一段距离。
}
}
Android如何纯java代码实现字体阴影效果
Android实现纯java代码字体阴影效果,主要是通过activity的draw方法,进行重新绘制,如下代码:
package canvas.test;
import android.app.Activity;
import android.os.Bundle;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.view.View;
public class ShaderEffect extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new ImageEffect(this));
}
class ImageEffect extends View{
Paint paint;
public ImageEffect (Context context){
super(context);
paint = new Paint();//初始化画笔,为后面阴影效果使用。
paint.setAntiAlias(true);//去除锯齿。
paint.setShadowLayer(5f, 5.0f, 5.0f, Color.BLACK);//设置阴影层,这是关键。
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
}
@Override
public void onDraw(Canvas canvas){
super.onDraw(canvas);
int posX = 20;
int posY = 50;
int PicWidth,PicHegiht;
Drawable drawable = getResources().getDrawable(R.drawable.button);
Drawable dbe = getResources().getDrawable(R.drawable.button).mutate();//如果不调用mutate方法,则原图也会被改变,因为调用的资源是同一个,所有对象是共享状态的。
Drawable drawTest = getResources().getDrawable(R.drawable.button);
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.button);
PicWidth = drawable.getIntrinsicWidth();
PicHegiht = drawable.getIntrinsicHeight();
drawTest.setBounds(posX, (2 * posY) + PicHegiht, posX + PicWidth, (2 * posY) + 2 * PicHegiht );
drawable.setBounds(posX,posY,posX+PicWidth,posY+PicHegiht);
dbe.setBounds(0, 0, PicWidth, PicHegiht);
canvas.drawColor(Color.WHITE);//设置画布颜色
canvas.save(Canvas.MATRIX_SAVE_FLAG);
dbe.setColorFilter(0x7f000000,PorterDuff.Mode.SRC_IN);
canvas.translate(posX + (int)(0.9 * PicWidth/2), posY + PicHegiht/2);//图像平移为了刚好在原图后形成影子效果。
canvas.skew(-0.9F, 0.0F);//图像倾斜效果。
canvas.scale(1.0f, 0.5f);//图像(其实是画布)缩放,Y方向缩小为1/2。
dbe.draw(canvas);//此处为画原图像影子效果图,比原图先画,则会在下层。
drawable.clearColorFilter();
canvas.restore();
canvas.save(Canvas.MATRIX_SAVE_FLAG);
drawable.draw(canvas);//此处为画原图像,由于canvas有层次效果,因此会盖在影子之上。
canvas.restore();
//默认无效果原图
canvas.save(Canvas.MATRIX_SAVE_FLAG);
drawTest.draw(canvas);
canvas.restore();
//图片阴影效果
canvas.save(Canvas.MATRIX_SAVE_FLAG);
//Rect rect = new Rect(2*posX + PicWidth, 2*posY + PicHegiht, 2*posX + 2*PicWidth, 2*posY + 2*PicHegiht);//此为理论上的阴影图坐标
Rect rect = new Rect(2*posX + PicWidth + 3, 2*posY + PicHegiht + 3, 2*posX + 2*PicWidth - 2, 2*posY + 2*PicHegiht - 2);
//由于图片的实际尺寸比显示出来的图像要大一些,因此需要适当更改下大小,以达到较好的效果
RectF rectF = new RectF(rect);
canvas.drawRoundRect(rectF, 10f, 10f, paint);//在原有矩形基础上,画成圆角矩形,同时带有阴影层。
canvas.drawBitmap(bmp, 2*posX + PicWidth, 2*posY + PicHegiht, null);//画上原图。
canvas.restore();
}
}
}
本文标题:图像平移的java代码 图像平移的java代码有哪些
文章网址:http://ybzwz.com/article/ddopcsp.html