Mirror an image

P

pgmbento93

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. Coloquea
* 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
 
J

Jeff Higgins

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.
 
J

Jeff Higgins

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);
}
});
}
}
 
J

Jeff Higgins

//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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top