IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

2D Java Discussion :

Java2D : translation, rotation et changement de taille


Sujet :

2D Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Profil pro
    Inscrit en
    Mars 2006
    Messages
    97
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2006
    Messages : 97
    Par défaut Java2D : translation, rotation et changement de taille
    Salut,

    Je me mets à la programmation graphique sous Java.
    Et j'avais envie de partager les quelques lignes
    de code que j'ai réussi à ecrire

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
     
    import static java.lang.Math.PI;
    import java.util.Vector;
    import java.lang.Thread;
    import java.lang.InterruptedException;
    import java.awt.Toolkit;
    import java.awt.geom.AffineTransform;
    import java.awt.geom.Point2D;
    import java.awt.geom.Point2D.Double;
    import java.awt.Polygon;
    import java.awt.Container;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.GraphicsConfiguration;
    import java.awt.GraphicsDevice;
    import java.awt.GraphicsEnvironment;
    import java.awt.Rectangle;
    import javax.swing.JPanel;
    import javax.swing.JFrame;
     
    public class SimpleFrame
    {
    	/**
             * @param args
             */
    	public static void main (String[] args)
    	{
    		/**
                     */
    		Toolkit tk = Toolkit.getDefaultToolkit ();
    		Dimension d = tk.getScreenSize();
    		int screenH = d.height;
    		int screenW = d.width;
     
    		/**
                     */
    		MyFrame simpleFrame = new MyFrame ();
    	}
    }
     
    class MyFrame extends JFrame
    {
    	public MyFrame ()
    	{
    		/**
                     */
    		GraphicsEnvironment graphicEnv = GraphicsEnvironment.getLocalGraphicsEnvironment();
    		GraphicsDevice[] graphicDevices = graphicEnv.getScreenDevices ();
    		GraphicsConfiguration graphicConf = graphicDevices[0].getDefaultConfiguration();
    		Rectangle screenBounds = graphicConf.getBounds ();
     
    		/**
                     */		
    		setTitle ("Simple Frame");
    		setSize (screenBounds.width/3, screenBounds.height/3);
    		setLocationRelativeTo (null);
    		setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
     
    		/**
                     */
    		Container container = getContentPane ();
    		MyPanel panel = new MyPanel ();
    		container.add (panel);
    		setVisible (true);
    		panel.anime ();
    	}
     
    	private static final long serialVersionUID = 0;
    }
     
    class MyPanel extends JPanel
    {
    	public void anime ()
    	{
    		while (true)
    		{
    			for (int i = 0; i < 100; i++)
    			{
    				polygon.translate (1, 1);
    				polygon.rotate (2);
    				repaint ();
    				try
    				{
    					Thread.sleep (5);
    				}
    				catch (InterruptedException e)
    				{
    					e.printStackTrace ();
    				}
    			}
    			for (int i = 0; i < 200; i++)
    			{
    				polygon.scale (0.99, 0.99);
    				repaint ();
    				try
    				{
    					Thread.sleep (5);
    				}
    				catch (InterruptedException e)
    				{
    					e.printStackTrace ();
    				}
    			}
    			for (int i = 0; i < 200; i++)
    			{
    				polygon.scale (1.0101, 1.0101);
    				repaint ();
    				try
    				{
    					Thread.sleep (5);
    				}
    				catch (InterruptedException e)
    				{
    					e.printStackTrace ();
    				}
    			}
    			for (int i = 0; i < 100; i++)
    			{
    				polygon.translate (1, -1);
    				polygon.rotate (2);
    				repaint ();
    				try
    				{
    					Thread.sleep (5);
    				}
    				catch (InterruptedException e)
    				{
    					e.printStackTrace ();
    				}
    			}	
    			for (int i = 0; i < 200; i++)
    			{
    				polygon.translate (-1, 0);
    				polygon.rotate (2);
    				repaint ();
    				try
    				{
    					Thread.sleep (5);
    				}
    				catch (InterruptedException e)
    				{
    					e.printStackTrace ();
    				}
    			}
    		}
    	}
     
    	public void paintComponent (Graphics g)
    	{
    		super.paintComponent (g);
    		Graphics2D g2 = (Graphics2D) g;
    		AffineTransform affineBackup = g2.getTransform ();
    		AffineTransform rotate45 = AffineTransform.getRotateInstance (PI/4.0, 0.0, 0.0);
     
    		g2.drawPolygon (polygon);
    		g2.drawArc (polygon.getCenterX(), polygon.getCenterY(), 5, 5, 0, 360);
     
    		g2.setTransform (affineBackup);
    	}
     
    	public MyPanel ()
    	{
    		setBackground (Color.white);
    		setForeground (Color.blue);
     
    		polygon = new MyPolygon (new Point2D.Double (50, 50),
    			new Point2D.Double (70, 10),
    			new Point2D.Double (80, 30),
    			new Point2D.Double (90, 10),
    			new Point2D.Double (130, 50),
    			new Point2D.Double (90, 130),
    			new Point2D.Double (70, 70),
    			new Point2D.Double (50, 90)
    			);		
    	}
     
    	private MyPolygon polygon = null;
    	private static final long serialVersionUID = 0;
    }
     
    class MyPolygon extends Polygon
    {
    	public MyPolygon (Point2D.Double... points)
    	{
    		super ();
    		this.points = new Vector<Point2D.Double> ();
     
    		for (int i = 0; i < points.length; i++)
    		{
    			this.points.add (points[i]);
    			addPoint ((int) points[i].getX(), (int) points[i].getY());
    		}
    		updateCenter ();
    	}
     
     
    	public void scale (double sx, double sy)
    	{
    		AffineTransform rotation = AffineTransform.getScaleInstance (sx, sy);
    		for (Point2D.Double point : points)
    		{
    			rotation.transform (point, point);
    		}
    		reset ();
    		for (Point2D.Double point : points)
    		{
    			addPoint ((int) point.getX(), (int) point.getY());
    		}
     
    		double previousX = centerX;
    		double previousY = centerY;
     
    		updateCenter ();
    		double currentX = centerX;
    		double currentY = centerY;
    		double deltaX = previousX - currentX;
    		double deltaY = previousY - currentY;
     
    		translate ((int) deltaX, (int) deltaY);
    		centerX = previousX;
    		centerY = previousY;
    	}
     
    	public void rotate (double degree)
    	{
    		AffineTransform rotation = AffineTransform.getRotateInstance (Math.toRadians (degree), centerX, centerY);
    		for (Point2D.Double point : points)
    		{
    			rotation.transform (point, point);
    		}
    		reset ();
    		for (Point2D.Double point : points)
    		{
    			addPoint ((int) point.getX(), (int) point.getY());
    		}
    	}
     
    	public void translate (int dx, int dy)
    	{
    		double x = 0;
    		double y = 0;
    		for (Point2D.Double point : points)
    		{
    			x = point.getX() + dx;
    			y = point.getY() + dy;
    			point.setLocation (x, y);
    		}
    		reset ();
    		for (Point2D.Double point : points)
    		{
    			addPoint ((int) point.getX(), (int) point.getY());
    		}
    		updateCenter ();
    	}
     
    	public void updateCenter ()
    	{		
    		process :
    		{
    			if (points.size () <= 0)
    			{
    				centerX = 0;
    				centerY = 0;
    				break process;
    			}
     
    			double xSum = 0;
    			double ySum = 0;
    			for (Point2D.Double point : points)
    			{
    				xSum += point.getX();
    				ySum += point.getY();
    			}
    			centerX = xSum / points.size ();
    			centerY = ySum / points.size ();
    		}
    	}
     
    	public int getCenterX ()
    	{
    		return (int) centerX;
    	}
     
    	public int getCenterY ()
    	{
    		return (int) centerY;
    	}
     
    	private Vector<Point2D.Double> points = null;
    	private double centerX = 0;
    	private double centerY = 0;
    	private static final long serialVersionUID = 0;
    }
    Peut être pourrez-vous m'apprendre des optimisations
    ou des manières differentes de coder cet exemple...


  2. #2
    Membre confirmé
    Profil pro
    Inscrit en
    Mars 2006
    Messages
    97
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2006
    Messages : 97
    Par défaut
    J'ai relu mon code et j'ai apporté quelques petites modifs....

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
     
    import static java.lang.Math.sin;
    import static java.lang.Math.cos;
    import static java.lang.Math.toRadians;
    import java.util.Vector;
    import java.lang.Thread;
    import java.lang.InterruptedException;
    import java.awt.Toolkit;
    import java.awt.geom.AffineTransform;
    import java.awt.geom.Point2D;
    import java.awt.geom.Point2D.Double;
    import java.awt.Polygon;
    import java.awt.Container;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.GraphicsConfiguration;
    import java.awt.GraphicsDevice;
    import java.awt.GraphicsEnvironment;
    import java.awt.Rectangle;
    import javax.swing.JPanel;
    import javax.swing.JFrame;
     
    public class SimpleFrame
    {
    	/**
             * @param args
             */
    	public static void main (String[] args)
    	{
    		/**
                     */
    		Toolkit tk = Toolkit.getDefaultToolkit ();
    		Dimension d = tk.getScreenSize();
    		int screenH = d.height;
    		int screenW = d.width;
     
    		/**
                     */
    		MyFrame simpleFrame = new MyFrame ();
    	}
    }
     
    class MyFrame extends JFrame
    {
    	public MyFrame ()
    	{
    		/**
                     */
    		GraphicsEnvironment graphicEnv = GraphicsEnvironment.getLocalGraphicsEnvironment();
    		GraphicsDevice[] graphicDevices = graphicEnv.getScreenDevices ();
    		GraphicsConfiguration graphicConf = graphicDevices[0].getDefaultConfiguration();
    		Rectangle screenBounds = graphicConf.getBounds ();
     
    		/**
                     */		
    		setTitle ("Simple Frame");
    		setSize (screenBounds.width/2, screenBounds.height/2);
    		setLocationRelativeTo (null);
    		setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
     
    		/**
                     */
    		Container container = getContentPane ();
    		MyPanel panel = new MyPanel ();
    		container.add (panel);
    		setVisible (true);
    		panel.anime ();
    	}
     
    	private static final long serialVersionUID = 0;
    }
     
    class MyPanel extends JPanel
    {
    	public void anime ()
    	{
    		while (true)
    		{
    			for (int i = 0; i < 100; i++)
    			{
    				polygon.translate (1, 1);
    				polygon.rotate (2);
    				polygon.reset ();
    				repaint ();
    				try
    				{
    					Thread.sleep (50);
    				}
    				catch (InterruptedException e)
    				{
    					e.printStackTrace ();
    				}
    			}
    			for (int i = 0; i < 200; i++)
    			{
    				polygon.scale (0.99, 0.99);
    				polygon.reset ();
    				repaint ();
    				try
    				{
    					Thread.sleep (50);
    				}
    				catch (InterruptedException e)
    				{
    					e.printStackTrace ();
    				}
    			}
    			for (int i = 0; i < 200; i++)
    			{
    				polygon.scale (1.0101, 1.0101);
    				polygon.reset ();
    				repaint ();
    				try
    				{
    					Thread.sleep (50);
    				}
    				catch (InterruptedException e)
    				{
    					e.printStackTrace ();
    				}
    			}
    			for (int i = 0; i < 100; i++)
    			{
    				polygon.translate (1, -1);
    				polygon.rotate (-2);
    				polygon.reset ();
    				repaint ();
    				try
    				{
    					Thread.sleep (50);
    				}
    				catch (InterruptedException e)
    				{
    					e.printStackTrace ();
    				}
    			}	
    			for (int i = 0; i < 200; i++)
    			{
    				polygon.translate (-1, 0);
    				polygon.rotate (2);
    				polygon.reset ();
    				repaint ();
    				try
    				{
    					Thread.sleep (50);
    				}
    				catch (InterruptedException e)
    				{
    					e.printStackTrace ();
    				}
    			}
    		}
    	}
     
    	public void paintComponent (Graphics g)
    	{
    		super.paintComponent (g);
    		Graphics2D g2 = (Graphics2D) g;
    		g2.drawPolygon (polygon);
    		g2.drawArc (polygon.getCenterX(), polygon.getCenterY(), 5, 5, 0, 360);
    	}
     
    	public MyPanel ()
    	{
    		setBackground (Color.white);
    		setForeground (Color.blue);
     
    		polygon = new MyPolygon (new Point2D.Double (50, 50),
    			new Point2D.Double (70, 10),
    			new Point2D.Double (80, 30),
    			new Point2D.Double (90, 10),
    			new Point2D.Double (130, 50),
    			new Point2D.Double (90, 130),
    			new Point2D.Double (70, 70),
    			new Point2D.Double (50, 90)
    			);		
    	}
     
    	private MyPolygon polygon = null;
    	private static final long serialVersionUID = 0;
    }
     
    class MyPolygon extends Polygon
    {
    	public MyPolygon (Point2D.Double... points)
    	{
    		super ();
    		this.points = new Vector<Point2D.Double> ();
     
    		for (int i = 0; i < points.length; i++)
    		{
    			this.points.add (points[i]);
    			addPoint ((int) points[i].getX(), (int) points[i].getY());
    		}
    		updateCenter ();
    	}
     
    	public void scale (double sx, double sy)
    	{
    		double previousX = centerX;
    		double previousY = centerY;
    		double x = 0;
    		double y = 0;
    		for (Point2D.Double point : points)
    		{
    			x = point.getX() * sx;
    			y = point.getY() * sy;
    			point.setLocation (x, y);
    		}
    		updateCenter ();
     
    		double deltaX = previousX - centerX;
    		double deltaY = previousY - centerY;
    		translate ((int) deltaX, (int) deltaY);
    		centerX = previousX;
    		centerY = previousY;
    	}
     
    	public void rotate (double degree)
    	{
    		double x = 0;
    		double y = 0;	
    		for (Point2D.Double point : points)
    		{			
    			x = (point.getX()-centerX)*cos(toRadians(degree))
    			- (point.getY()-centerY)*sin(toRadians(degree))
    			+ centerX;
     
    			y = (point.getX()-centerX)*sin(toRadians(degree))
    			+ (point.getY()-centerY)*cos(toRadians(degree))
    			+ centerY;
     
    			point.setLocation (x, y);
    		}
    	}
     
    	public void translate (int dx, int dy)
    	{
    		double x = 0;
    		double y = 0;
    		for (Point2D.Double point : points)
    		{
    			x = point.getX() + dx;
    			y = point.getY() + dy;
    			point.setLocation (x, y);
    		}
    		centerX += dx;
    		centerY += dy;
    	}
     
    	public void updateCenter ()
    	{		
    		process :
    		{
    			if (points.size() <= 0)
    			{
    				centerX = 0;
    				centerY = 0;
    				break process;
    			}
     
    			double xSum = 0;
    			double ySum = 0;
    			for (Point2D.Double point : points)
    			{
    				xSum += point.getX();
    				ySum += point.getY();
    			}
    			centerX = xSum / points.size();
    			centerY = ySum / points.size();
    		}
    	}
     
    	public void reset ()
    	{
    		super.reset ();
    		for (Point2D.Double point : points)
    		{
    			addPoint ((int) point.getX(), (int) point.getY());
    		}
    	}
     
    	public int getCenterX ()
    	{
    		return (int) centerX;
    	}
     
    	public int getCenterY ()
    	{
    		return (int) centerY;
    	}
     
    	private Vector<Point2D.Double> points = null;
    	private double centerX = 0;
    	private double centerY = 0;
    	private static final long serialVersionUID = 0;
    }

Discussions similaires

  1. Réponses: 1
    Dernier message: 08/08/2012, 13h01
  2. [javascript][IE6] changement automatique taille du texte
    Par MatMeuh dans le forum Général JavaScript
    Réponses: 5
    Dernier message: 04/12/2008, 12h05
  3. Animation d'une image, translation, rotation en javascript ?
    Par Ptit_Mouss dans le forum Général JavaScript
    Réponses: 7
    Dernier message: 15/09/2006, 11h21
  4. [Photoshop] Changement de taille d'une image
    Par sole dans le forum Imagerie
    Réponses: 4
    Dernier message: 29/04/2005, 16h13
  5. Réponses: 2
    Dernier message: 18/12/2003, 23h12

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo