Results 1 to 10 of 10
Thread: Expand an Image using Pixmap
- 04-21-2016, 05:51 PM #1
Member
- Join Date
- Apr 2016
- Posts
- 5
- Rep Power
- 0
Expand an Image using Pixmap
My goal is to expand both the height and width of an image by filling in an expanded image with a copy of the colors from the original image. Here is my code:
Java Code:import java.awt.Dimension; import java.awt.Color; import java.awt.*; import javax.swing.*; public class Expand extends Command { private static SizeInputDialog ourDialog = new SizeInputDialog("Resize Scale Input", " scale width by", "scale height by"); public static final int MAX_COLOR_LEVEL = 255; public Expand () { super("Resize"); } // public void execute (Pixmap target) { ourDialog.show(); Dimension scale = ourDialog.getSize(); Dimension oldSize = target.getSize(); target.setSize(oldSize.width * scale.width, oldSize.height * scale.height); // TODO: fill in enlarged pixmap by copying current colors into empty space // so each takes up scale space in the new pixmap //Gets dimensions of new scaled Pixmap Dimension newSize=target.getSize(); //creates new scaled Pixmap to copy colors into Pixmap newPix=new Pixmap(newSize.width,newSize.height); //parses through width and height of original image for(int i=0;i<oldSize.width;i++){ for (int j=0;j>oldSize.height;j++){ //gets color values of original image Color tmp=target.getColor(i,j); //sets starting point to top left corner int counterWidth; int counterHeight; //starts from top left and goes down for(counterHeight=newSize.height;counterHeight>0;counterHeight++){ // starts from top left and goes to the right for(counterWidth=0;counterWidth<oldSize.width*scale.width;counterWidth++){ //copies the colors to the new Pixmap newPix.setColor(counterWidth,counterHeight,tmp); } } } } } }
I think there is something wrong with my inner for loops. Can someone let me know where I might be going wrong. Thanks. Any advice would be helpful.
- 04-21-2016, 06:06 PM #2
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 13
Re: Expand an Image using Pixmap
How are you reading in the image? Where is the Pixmap class? And specifically, why not just use the Java API to scale the image?
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 04-21-2016, 09:29 PM #3
Member
- Join Date
- Apr 2016
- Posts
- 5
- Rep Power
- 0
Re: Expand an Image using Pixmap
I guess it is important to note that I have a file called Main.java which calls the Expand instance method, which looks like this:
Java Code:public class Main { public static final Dimension SIZE = new Dimension(800, 600); public static final String TITLE = "Pixmap!"; public static void main (String[] args) { // create container that will work with Window manager JFrame frame = new JFrame(TITLE); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // create GUI components PixmapCanvas canvas = new PixmapCanvas(frame); ButtonPanel commands = new ButtonPanel(canvas); // add commands to test here commands.add(new Reader()); commands.add(new Writer()); commands.add(new Negative()); //TODO: /*Add new instatantiations of Expand, MirrorVertically, MirrorHorizontally, and Blur Objects. Copy the above pattern for the command to add a Negative Object */ commands.add(new MirrorVertically()); commands.add(new MirrorHorizontally()); commands.add(new Expand()); // add our container to Frame and show it frame.getContentPane().add(canvas, BorderLayout.CENTER); frame.getContentPane().add(commands, BorderLayout.NORTH); frame.pack(); // start the GUI frame.setVisible(true); } }
- 04-21-2016, 09:42 PM #4
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 13
Re: Expand an Image using Pixmap
You simply have too many classes that are unknowns and not part of the Java API (Pixmap, PixmapCanvas, MirrorVertically, MirrorHorizontally,
SizeInputDialog, Command)
You need to create a Short, Self Contained, Correct Example to demonstrate the problem. Keep it very small. It must be compilable and executable (which means you need to include the image).
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 04-21-2016, 09:50 PM #5
Member
- Join Date
- Apr 2016
- Posts
- 5
- Rep Power
- 0
Re: Expand an Image using Pixmap
I'm reading in the file with another file called Reader. Here is the code for that:
Java Code:import javax.swing.JFileChooser; public class Reader extends Command { private static final JFileChooser ourChooser = new JFileChooser("."); public Reader () { super("Open"); } public void execute (Pixmap target) { String fileName = getFileName(); if (fileName != null) { target.read(fileName); } } protected String getFileName () { int response = ourChooser.showOpenDialog(null); if (response == JFileChooser.APPROVE_OPTION) { return ourChooser.getSelectedFile().getPath(); } return null; } }
- 04-21-2016, 10:06 PM #6
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 13
Re: Expand an Image using Pixmap
I still don't know what Pixmap does. I recommend you read the section on working with images in the Java Tutorials (see signature for link). There
are methods for scaling images as well as doing other stuff with them.
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 04-22-2016, 02:28 AM #7
Member
- Join Date
- Apr 2016
- Posts
- 5
- Rep Power
- 0
Re: Expand an Image using Pixmap
Pixmap is basically just a 2D array/rectangular frame for which each of the pixels go.
Here is the code for Pixmap:
Java Code:public class Pixmap { public static final Dimension DEFAULT_SIZE = new Dimension(300, 300); public static final Color DEFAULT_COLOR = Color.BLACK; public static final String DEFAULT_NAME = "Default"; public static final String FILE_OUTPUT_FORMAT = "jpeg"; // force file writing in .jpg format private String myFileName; private BufferedImage myImage; private Dimension mySize; /** * Create a default pixmap (300x300 black) */ public Pixmap () { this(DEFAULT_SIZE.width, DEFAULT_SIZE.height, DEFAULT_COLOR); } /** * Create a black pixmap with given width and height */ public Pixmap (int width, int height) { this(width, height, DEFAULT_COLOR); } /** * Create a pixmap with given width and height and filled with given initial color */ public Pixmap (int width, int height, Color color) { createImage(width, height, color); } /** * Create this image as a copy of the given image */ public Pixmap (Pixmap other) { myFileName = other.myFileName; mySize = other.getSize(); myImage = copyImage(mySize, mySize, other.myImage); } /** * Create a pixmap from the given local file * @param filename complete pathname of local file */ public Pixmap (String fileName) { if (fileName == null) { createImage(DEFAULT_SIZE.width, DEFAULT_SIZE.height, DEFAULT_COLOR); } else { read(fileName); } } public String getName () { int index = myFileName.lastIndexOf(File.separator); if (index >= 0) return myFileName.substring(index + 1); else return myFileName; } public Dimension getSize () { return new Dimension(mySize); } public Color getColor (int x, int y) { return new Color(myImage.getRGB(x, y)); } public void setColor (int x, int y, Color value) { myImage.setRGB(x, y, value.getRGB()); } public void setSize (int width, int height) { if (width != mySize.width || height != mySize.height) { Dimension newSize = new Dimension(width, height); if (width > mySize.width || height > mySize.height) { myImage = copyImage(mySize, newSize, myImage); } else { // BUGBUG: scale image down instead? myImage = myImage.getSubimage(0, 0, width, height); } mySize = newSize; } } public void read (String fileName) { try { myFileName = fileName; myImage = ImageIO.read(new File(myFileName)); mySize = new Dimension(myImage.getWidth(), myImage.getHeight()); } catch (IOException e) { System.out.println(e); } } public void write(String fileName) { try { ImageIO.write(myImage, FILE_OUTPUT_FORMAT, new File(fileName)); } catch (IOException e) { System.out.println(e); } } public void paint (Graphics pen) { pen.drawImage(myImage, 0, 0, mySize.width, mySize.height, null); } private void createImage (int width, int height, Color color) { myFileName = DEFAULT_NAME; myImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); mySize = new Dimension(width, height); } private BufferedImage copyImage (Dimension from, Dimension to, BufferedImage original) { int[] data = new int[from.width * from.height]; original.getRGB(0, 0, from.width, from.height, data, 0, from.width); BufferedImage result = new BufferedImage(to.width, to.height, BufferedImage.TYPE_INT_RGB); result.setRGB(0, 0, from.width, from.height, data, 0, from.width); return result; } public static void main (String[] args) { Pixmap p = new Pixmap(200, 100); p.setSize(400, 200); p.setSize(400, 200); p.setSize(200, 400); }
- 04-22-2016, 10:35 AM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 26
Re: Expand an Image using Pixmap
First thing, you pass the 'target' into your execute method.
I assume this is the original image?
If so, then why are you resizing the target?
You then copy all the pixels from the target to a newPixMap, but you do nothing with that new PixMap.
You exit the execute method and it just vanishes.
These are the root cause.
I suspect you ought to return a resized PixMap (the newPixMap) and not change the target at all.Please do not ask for code as refusal often offends.
** This space for rent **
- 04-22-2016, 08:27 PM #9
Member
- Join Date
- Apr 2016
- Posts
- 5
- Rep Power
- 0
Re: Expand an Image using Pixmap
Could I fix this by changing Target, where I don't need to create a new Pixmap, I just change Target. Also, can I display the new image without using a return statement?
Here is my updated code:
Java Code:ourDialog.show(); Dimension scale = ourDialog.getSize(); Dimension oldSize = target.getSize(); target.setSize(oldSize.width * scale.width, oldSize.height * scale.height); // TODO: fill in enlarged pixmap by copying current colors into empty space // so each takes up scale space in the new pixmap //parses through width and height of original image for(int y=0;y<oldSize.height * scale.height;y++){ for (int x=0;x<oldSize.width * scale.width;x++){ target.setColor(x,y,target.getColor(x/scale.width,y/scale.height));
- 04-22-2016, 08:47 PM #10
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 13
Re: Expand an Image using Pixmap
You could. But I have found out that when one continually modifies a graphic object over and over again (like
making a copy of a copy), errors are introduced which can distort the object over time. So scaling the original
object is probably best. And there is a pretty dominant philosophy in OOP which strives for immutable objects.
So the less state you have and need to modify, the better.
And you can probably repaint the target from anywhere as long as your logic supports it. But does it make sense
in terms of what the class is supposed to do. By that I mean putting unrelated operations in a class which don't
make sense.
Regards,
JimLast edited by jim829; 04-22-2016 at 08:49 PM.
The JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
Similar Threads
-
Applet Expand Collpse Help Required
By sarfrazasghar in forum Java AppletsReplies: 0Last Post: 11-07-2012, 09:44 AM -
Expand & Shrink Buttons
By umaza in forum AWT / SwingReplies: 1Last Post: 10-31-2012, 05:22 AM -
Compress/Expand data visualization
By susieferrari in forum JavaFXReplies: 4Last Post: 07-23-2012, 11:55 AM -
How to expand the JPanels dynamically?...
By Vin in forum New To JavaReplies: 6Last Post: 01-27-2010, 01:58 PM -
SwT Expand items problem in Solaris.
By Sureshgurram in forum SWT / JFaceReplies: 0Last Post: 01-27-2009, 11:23 AM
Bookmarks