Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #27930 > unrolled thread

Mirror an image

Started bypgmbento93@gmail.com
First post2014-04-01 07:44 -0700
Last post2014-04-13 10:21 -0400
Articles 8 — 4 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  Mirror an image pgmbento93@gmail.com - 2014-04-01 07:44 -0700
    Re: Mirror an image Jeff Higgins <jeff@invalid.invalid> - 2014-04-01 11:09 -0400
      Re: Mirror an image Jeff Higgins <jeff@invalid.invalid> - 2014-04-01 11:15 -0400
    Re: Mirror an image Roedy Green <see_website@mindprod.com.invalid> - 2014-04-01 11:56 -0700
      Re: Mirror an image Wanja Gayk <brixomatic@yahoo.com> - 2014-04-06 20:18 +0200
        Re: Mirror an image Jeff Higgins <jeff@invalid.invalid> - 2014-04-06 18:01 -0400
          Re: Mirror an image Wanja Gayk <brixomatic@yahoo.com> - 2014-04-13 15:57 +0200
            Re: Mirror an image Jeff Higgins <jeff@invalid.invalid> - 2014-04-13 10:21 -0400

#27930 — Mirror an image

Frompgmbento93@gmail.com
Date2014-04-01 07:44 -0700
SubjectMirror an image
Message-ID<2299213c-18fc-4e55-ab39-22dce863b255@googlegroups.com>
hi, i was confronted with this problem. i was given a code where i have to mirror an image at the middle. so the image needs to be divided and then i have to mirror the image. this is the code:

package tps.tp1.pack3Arrays;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;

/**
 * Esta classe visa aplicar uma transformada ao pixel a uma imagem. Coloque a
 * imagem na raiz do seu projecto java.
 * 
 * @author ateofilo
 * 
 */
public class C07BitmapTransform {

	// TODO este é o método para implementar o espelhamento
	// só devem utilizar os métodos de image.getRGB e image.SetRGB
	public static void transformImage(BufferedImage image) {
		// obter a altura e largura da imagem em pixels
		int height = image.getHeight();
		int width = image.getWidth();
		
		// percorrer todos os pixels da imagem
		for (int y = 0; y < height; y++) {
			for (int x = 0; x < width; x++) {
				int pixelRGB = image.getRGB(x, y);
				int newPixelColor = pixelRGB;
				// TODO retirar o comentário da linha seguinte, para teste
				newPixelColor = pixelRGB + 0xFF;
				newPixelColor = pixelRGB & 0xFF;
				newPixelColor = pixelRGB & 0xFF00;
				newPixelColor = pixelRGB & 0xFF0000;


				image.setRGB(x, y, newPixelColor);
			}
		}
	}

	/**
	 * Método de inicialização da frame
	 * 
	 * @throws IOException
	 */
	public static void init() throws IOException {
		// criar uma JFrame
		JFrame frame = new JFrame();
		// colocar umas dimensões simpáticas
		frame.setSize(1000, 900);
		// centrá-la
		frame.setLocationRelativeTo(null);

		// Ler a imagem para uma BufferedImage
		BufferedImage image = ImageIO.read(new File("image1.jpg")); 

		// Executar a transformação à imagem
		transformImage(image);

		// mostrar a imagem no centro de um JLabel
		ImageIcon img = new ImageIcon(image);
		JLabel label = new JLabel(img, JLabel.CENTER);
		// Adicionar a label à frame
		frame.add(label);

		// colocar a frame visível
		frame.setVisible(true);
	}

	/**
	 * Método main
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		// enviar a acção para execução
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				try {
					// init() é a acção a executar
					init();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		});
	}

}

dont pay attention to the the comments.. what can i do? im new in java and i dont know what to do

[toc] | [next] | [standalone]


#27931

FromJeff Higgins <jeff@invalid.invalid>
Date2014-04-01 11:09 -0400
Message-ID<lhel09$sqv$1@dont-email.me>
In reply to#27930
On 04/01/2014 10:44 AM, pgmbento93@gmail.com wrote:
> hi, i was confronted with this problem. i was given a code where i have to mirror an image at the middle. so the image needs to be divided and then i have to mirror the image. this is the code:

Use BufferedImage.createGraphics()
and then Graphics2D.scale(x,y) to flip the image.
No need for the getRGB loop.

[toc] | [prev] | [next] | [standalone]


#27932

FromJeff Higgins <jeff@invalid.invalid>
Date2014-04-01 11:15 -0400
Message-ID<lhelb6$vgu$1@dont-email.me>
In reply to#27931
On 04/01/2014 11:09 AM, Jeff Higgins wrote:
> On 04/01/2014 10:44 AM, pgmbento93@gmail.com wrote:
>> hi, i was confronted with this problem. i was given a code where i
>> have to mirror an image at the middle. so the image needs to be
>> divided and then i have to mirror the image. this is the code:
>
> Use BufferedImage.createGraphics()
> and then Graphics2D.scale(x,y) to flip the image.
> No need for the getRGB loop.
>
>
See the 2D Graphics trail in the Java Tutorials.
Specifically:
<http://docs.oracle.com/javase/tutorial/2d/advanced/transforming.html>

[toc] | [prev] | [next] | [standalone]


#27933

FromRoedy Green <see_website@mindprod.com.invalid>
Date2014-04-01 11:56 -0700
Message-ID<jt2mj9t2uv0jdos76t4otlb8lgfoc1udnn@4ax.com>
In reply to#27930
On Tue, 1 Apr 2014 07:44:03 -0700 (PDT), pgmbento93@gmail.com wrote,
quoted or indirectly quoted someone who said :

>dont pay attention to the the comments.. what can i do? im new in java and =
>i dont know what to do

The trick I suspect is to use an Affine transform to map each point to
its corresponding new position.

See http://mindprod.com/jgloss/affinetransform.html
-- 
Roedy Green Canadian Mind Products http://mindprod.com
"The question of whether machines can think is about as relevant 
as the question of whether submarines can swim"
~ Edsger Wybe Dijkstra (born: 1930-05-11 died: 2002-08-06 at age: 72)

[toc] | [prev] | [next] | [standalone]


#27962

FromWanja Gayk <brixomatic@yahoo.com>
Date2014-04-06 20:18 +0200
Message-ID<MPG.2da540be1aa5795117@202.177.16.121>
In reply to#27933
In article <jt2mj9t2uv0jdos76t4otlb8lgfoc1udnn@4ax.com>, Roedy Green 
(see_website@mindprod.com.invalid) says...
> 
> On Tue, 1 Apr 2014 07:44:03 -0700 (PDT), pgmbento93@gmail.com wrote,
> quoted or indirectly quoted someone who said :
> 
> >dont pay attention to the the comments.. what can i do? im new in java and =
> >i dont know what to do
> 
> The trick I suspect is to use an Affine transform to map each point to
> its corresponding new position.
> 
> See http://mindprod.com/jgloss/affinetransform.html

Mirroring is actually much easier: Just use a negative width or height 
for the Graphics#drawImage method.

Without any test, this would be my first, rough idea:

The image:
+-----+
+AAAAA+
+BBBBB+
+CCCCC+
+DDDDD+
+-----+

public void paintComponent(Graphics _g){
 final Graphics2D g = (Graphics2D)_g;
 g.setClip( /* upper half, covering lines A and B */)
 g.drawImage( image, 0, 0 , width, height); //height > 0!
 g.setClip( /* lower half, covering lines C and D */)
 g.drawImage( image, bottomLeftCorner, width, -height); //height < 0!
}

Should result in:
+-----+
+AAAAA+
+BBBBB+
+BBBBB+
+AAAAA+
+-----+

Not sure if that's bottom half corner or middle left, but that's just 
one attempt away, if it doesn't work as expected.

Have fun.

Kind regards,
-Wanja-

-- 
..Alesi's problem was that the back of the car was jumping up and down 
dangerously - and I can assure you from having been teammate to 
Jean Alesi and knowing what kind of cars that he can pull up with, 
when Jean Alesi says that a car is dangerous - it is. [Jonathan Palmer]

--- news://freenews.netfront.net/ - complaints: news@netfront.net ---

[toc] | [prev] | [next] | [standalone]


#27967

FromJeff Higgins <jeff@invalid.invalid>
Date2014-04-06 18:01 -0400
Message-ID<lhsj1p$i60$1@dont-email.me>
In reply to#27962
On 04/06/2014 02:18 PM, Wanja Gayk wrote:
> Without any test, this would be my first, rough idea:
> Have fun.

Thanks.
Here's another, not exactly per my first post
but the must be 25 ways of doing this task.

package scratch;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JScrollPane;

@SuppressWarnings("serial")
public class Scratch extends JComponent {

   private BufferedImage image;
   private int width, height;

   public Scratch() {
     try {
       image = ImageIO.read(new File("/path/to/image"));
       width = image.getWidth();
       height = image.getHeight();
       setPreferredSize(new Dimension(width, 2 * height));
     } catch (IOException e) {
       e.printStackTrace();
     }
   }

   @Override
   protected void paintComponent(Graphics g) {
     super.paintComponent(g);
     Graphics2D g2d = (Graphics2D) g;
     AffineTransform restore = g2d.getTransform();
     g2d.drawRenderedImage(image, null);
     g2d.translate(0, 2 * height);
     g2d.scale(1, -1);
     g2d.drawRenderedImage(image, null);
     g2d.setTransform(restore);
   }

   public static void main(String[] args) {
     javax.swing.SwingUtilities.invokeLater(new Runnable() {
       public void run() {
         Scratch scratch = new Scratch();
         JScrollPane scroller = new JScrollPane();
         scroller.getViewport().add(scratch);
         JFrame frame = new JFrame("Scratch");
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         frame.add(scroller);
         frame.pack();
         frame.setVisible(true);
       }
     });
   }
}

[toc] | [prev] | [next] | [standalone]


#28062

FromWanja Gayk <brixomatic@yahoo.com>
Date2014-04-13 15:57 +0200
Message-ID<MPG.2db4b6049cca8fca19@202.177.16.121>
In reply to#27967
In article <lhsj1p$i60$1@dont-email.me>, Jeff Higgins 
(jeff@invalid.invalid) says...
> 
> On 04/06/2014 02:18 PM, Wanja Gayk wrote:
> > Without any test, this would be my first, rough idea:
> > Have fun.
> 
> Thanks.
> Here's another, not exactly per my first post
> but the must be 25 ways of doing this task.
> 
> package scratch;
> 
> import java.awt.Dimension;
> import java.awt.Graphics;
> import java.awt.Graphics2D;
> import java.awt.geom.AffineTransform;
> import java.awt.image.BufferedImage;
> import java.io.File;
> import java.io.IOException;
> 
> import javax.imageio.ImageIO;
> import javax.swing.JComponent;
> import javax.swing.JFrame;
> import javax.swing.JScrollPane;
> 
> @SuppressWarnings("serial")
> public class Scratch extends JComponent {
> 
>    private BufferedImage image;

//you don't need to store the width and the height here, just get it 
//again in the paint method.
//the less variables/fields you have to track in your mind the code, the 
//better you will be able to read it later.

>    private int width, height;
> 
>    public Scratch() {
>      try {
>        image = ImageIO.read(new File("/path/to/image"));
>        width = image.getWidth();
>        height = image.getHeight();
>        setPreferredSize(new Dimension(width, 2 * height));
>      } catch (IOException e) {
>        e.printStackTrace();
>      }
>    }
> 
>    @Override
>    protected void paintComponent(Graphics g) {
>      super.paintComponent(g);
>      Graphics2D g2d = (Graphics2D) g;
>      AffineTransform restore = g2d.getTransform();

//you should set a clip here, so you don't need to paint the image in 
//full height.

>      g2d.drawRenderedImage(image, null);
>      g2d.translate(0, 2 * height);
>      g2d.scale(1, -1);
>      g2d.drawRenderedImage(image, null);
>      g2d.setTransform(restore);
>    }

//note: using g.drawImage(img, x,y,w,h,.. ) you could 
//automatically scale the image to the panel size, which may come handy. 

Kind regards,
Wanja

-- 
..Alesi's problem was that the back of the car was jumping up and down 
dangerously - and I can assure you from having been teammate to 
Jean Alesi and knowing what kind of cars that he can pull up with, 
when Jean Alesi says that a car is dangerous - it is. [Jonathan Palmer]

--- news://freenews.netfront.net/ - complaints: news@netfront.net ---

[toc] | [prev] | [next] | [standalone]


#28063

FromJeff Higgins <jeff@invalid.invalid>
Date2014-04-13 10:21 -0400
Message-ID<lie6n9$hdq$1@dont-email.me>
In reply to#28062
On 04/13/2014 09:57 AM, Wanja Gayk wrote:

> //you don't need to store the width and the height here,
> //you should set a clip here,
> //note: using g.drawImage(img, x,y,w,h,.. ) you could
Great. Thanks for the tips.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web