Image rotation. Flip. To zoom in and

Image rotation, flip, zoom in and out

Actually rewrite paint (Graphics g) method

drawImage (Image img, int x, int y, int width, int height, ImageObserver observer)

Scaling picture only need to change the type of parameter can be a few int

Class Graphics2D method rotate (double theta, double x, double y) can be used to rotate a picture

The first parameter: rotation angle in radians as the unit value

Second, the three parameters: rotation, respectively X and Y coordinates of the origin

import java.awt.*;
import javax.swing.*;

public class RotatePicture extends JLabel implements Runnable{

        static Image img = new ImageIcon("D:/Backup/ My documents/water in  .jpg").getImage ();
        static double[] rad = {0,Math.PI/2,Math.PI,-Math.PI/2};
        double theta = 0; 

        public void paintComponent(Graphics g){
                Graphics2D g2d = (Graphics2D)g;
                g2d.rotate(theta,250,250);
                g2d.drawImage(img,100,100,300,300,null);

        }

        public void run(){

                for(int i=0;;i++){
                         try{
                        Thread.sleep (10);
                        }catch(InterruptedException ie){}

                        // You can sign to change the direction of rotation
                        rotate(Math.toRadians(i));// Radian values
                }
        }

        public void rotate(double d){
                theta = d;
                repaint();
        }

        public static void main(String[] args){
                JFrame jf = new JFrame();
                jf.setSize (500,530);
                RotatePicture rp = new RotatePicture();
                RotatePicture rp1 = new RotatePicture();
                MyPicture mp = new MyPicture(img,jf);
                JTabbedPane tabbedPane = new JTabbedPane();
                tabbedPane.add(" Any angle rotation  ", rp);
                tabbedPane.add("90 Degree flip  ", rp1);
                tabbedPane.add(" Zoom in and out  ", mp);
                jf.add(tabbedPane);
                jf.setTitle (" Picture of the rotate flipping.  . Zoom in and out  ");
                jf.setLocationRelativeTo (null);
                jf.setVisible(true);
                jf.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
                Thread t1 = new Thread(rp);
                t1.start ();
                Thread t2 = new Thread(mp);
                t2.start ();
                for(int i=0;i<4;i++){
                         try{
                        Thread.sleep (1000);
                        }catch(InterruptedException ie){}
                        rp1.rotate(rad[i]);
                        if(i==3)i=-1;
                }
        }

}

class MyPicture extends Canvas implements Runnable{
        Image img ;
        int w;
        int h;
        int width;
        int height;
        int rate;
        boolean flag;

        public MyPicture(Image image,Container c){
                img = image;
                w = 50;
                h = 50;
                width = c.getWidth();
                height = c.getHeight()-50;
                rate = 10;
                flag = false;
                System.out.println(width+","+height);
        }

        public void run(){

                while(true){

                        if(w<=0||h<=0)
                                flag = true;
                        if(w>=width||h>=height)
                                flag = false;

                        try{
                                Thread.sleep (100);

                        }catch(InterruptedException ie){
                                ie.printStackTrace ();
                        }

                        if(flag){
                                w+=10;
                                h+=10;
                        }
                        if(!flag){
                                w-=10;
                                h-=10;
                        }
                        repaint();
                }
        }

        public void paint(Graphics g){

                // The upper-left corner of the coordinate set a picture
                int x = (width-w)/2;
                int y = (height-h)/2;
                g.drawImage(img, x, y, w, h,null); 

        }
}
分类:Java 时间:2010-05-28 人气:262
分享到:
blog comments powered by Disqus

相关文章

  • Java to determine whether the string contains characters 2010-11-19

    Java to determine whether the string contains characters import java.util.regex.Matcher; import java.util.regex.Pattern; public class IfHanZi { public static void main(String[] args) { // Method a : String s1 = " I am a Chinese "; String s2 = &q

  • Java is the Chinese character string replacement 2010-06-26

    public static int getLength (String str) ( str = str.replaceAll ("[^ x00-xff] *", "**"); System.out.print (str); return str.length (); ) public static void main (String [] args) throws UnsupportedEncodingException ( String str = "

  • java to get the main information page of the three: html to tree (rpm) 2010-12-04

    1. Required Documents param.txt: storing the path need to extract information page TestPage: need to extract the information stored pages Out.txt: page content output 2. Test program package test; import java.io.*; import Source.*; // Extract the mai

  • public static void main (String args []) parameters 2010-05-15

    String args [] is the main function of main parameters, namely array of strings. Using eclipse in the editing, right-click to run the Java files in the Run As option, select Run Configurations. Main class in the Project and complete the project, resp

  • Why public static void main (String args []){} 2010-11-06

    main () is a Java program entry, program execution is the beginning of this entry. Class is a class member, main () method must be public members. So that it can be called in the execution environment. main () method does not produce the object can b

  • How the eclipse of the main the String [] args parameter passed, and then export it 2010-12-26

    How the eclipse of the main the String [] args parameter passed, and then export it? In the Run menu to find Open Run dialog opens Select Java Application in the program need to run Select the Arguments Tab Enter the parameters in the Programe Argume

  • 浅析C#中的Main(String[] args)参数输入问题 2014-07-20

    本篇文章主要是对C#中的Main(String[] args)参数输入问题进行了详细的介绍,需要的朋友可以过来参考下,希望对大家有所帮助 指定相关的测试代码 首先,写一个用于测试的关于Main(String[] args)参数输入有关的代码类,如下: using System; public class Hello { public static void Main(String[] args) { switch (args[0]) { case "老板": Console.Write

  • java data type conversion Integer String Long Float Double Date 2010-07-24

    1 How to convert integer string String int? A. There are two ways: 1). Int i = Integer.parseInt ([String]); or i = Integer.parseInt ([String], [int radix]); 2). Int i = Integer.valueOf (my_str). IntValue (); Note: The string converted to Double, Floa

  • Java, how to convert a string into a date format 2010-10-05

    Java, convert string into date format is commonly used as a function of Among many projects, will use the drops, is also useful prior to, but I am a bad memory So, decided to write this blog, frequently used functions to facilitate the later can be u

  • java source code of the String 2010-12-07

    1.String Source Squadron String class description: The String class represents character strings. All string literals in Java programs, such as "abc", are implemented as instances of this class. Strings are constant; their values cannot be chang

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 黔ICP备15002463号-1

processed in 0.582 (s). 12 q(s)