Results 1 to 20 of 20
Thread: Help Clearing BufferedImage
- 03-27-2010, 11:44 PM #1
Member
- Join Date
- Jun 2009
- Posts
- 11
- Rep Power
- 0
Help Clearing BufferedImage
Hello guys,
I have looked around every where to find how to do this and cant really figure out...
so i am working on a system that draws lines on bufferedImage and it draws different lines depending on the iteration so if i go from 5 to 1, the lines should be less... its kinda of hard to explain but if you can execute the code and change the iterations, you will know what i am talking about.
but really what i want to figure out is how to clear the bufferedImage before each iterations so when i start drawing, the bufferedImage is blank?
here is the code:
Java Code:import java.awt.*; import java.awt.event.*; import java.awt.geom.AffineTransform; import java.awt.image.BufferedImage; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import javax.swing.*; import javax.swing.event.*; public class LSystemTesting extends JApplet implements ActionListener, ChangeListener { int mx, my; Image buffer = null; int w, h; int userSelectedIterations; // the number of iterations user wants int userSelectedSystem; // the system which user wants to be drawn on the screen int userSelectedScale; // the user can scale the curve to fit on the screen int startX,startY; // the location where the drawing of the curve will start String inputStart="F"; String inputString="F+F-F-F+F"; double inputAngle=Math.PI/2; String systemName[] = new String [40]; BufferedImage grid; JLabel systemLabel; // label for L-System menu JLabel iterationLabel; // label for iteration menu JComboBox iterMenu; // ComboBox for iteration menu JComboBox selectSystemMenu; // ComboBox for system menu JLabel scaleLabel; // label for scale slider JSlider scaler; // actual slider public LSystemTesting() { setSize(800, 550); Dimension d = getSize(); w = d.width; h = d.height; buffer = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); } public void init() { resize(1200,600); //the screen size userSelectedIterations = 0; userSelectedScale = 20; startX= 20; startY= 20; JPanel printToScreen = new JPanel() { public void paintComponent(Graphics g) { super.paintComponent(g); if(userSelectedSystem==0) curveOneTRY(g,inputStart,inputString,inputAngle, userSelectedIterations, userSelectedScale, startX, startY); } }; this.getContentPane().add(printToScreen, BorderLayout.CENTER); JPanel optionPanel = new JPanel(); optionPanel.setLayout(new GridLayout(2,3)); systemLabel = new JLabel("Select the LSystem!",JLabel.CENTER); optionPanel.add(systemLabel); //add label to optionPanel iterationLabel = new JLabel("Select number of iterations!",JLabel.CENTER); optionPanel.add(iterationLabel); //add label to optionPanel scaleLabel = new JLabel("Slide to Scale",JLabel.CENTER); optionPanel.add(scaleLabel); //add label to optionPanel selectSystemMenu = new JComboBox(); selectSystemMenu.addItem("Trial and Error"); selectSystemMenu.addActionListener(this); optionPanel.add(selectSystemMenu); //*******************************Define the iteration Menu iterMenu = new JComboBox(); for(int i=0; i<6; i++) { iterMenu.addItem(""+i); }//end for loop to add iteration options from one to five iterMenu.setSelectedIndex(userSelectedIterations); iterMenu.addActionListener(this); optionPanel.add(iterMenu); //*******************************Define the scale slider scaler = new JSlider(1,25,userSelectedScale); scaler.setMajorTickSpacing(25); scaler.setMinorTickSpacing(5); scaler.setPaintTicks(true); scaler.addChangeListener(this); optionPanel.add(scaler); this.getContentPane().add(optionPanel, BorderLayout.SOUTH); }//end initiate public void curveOneTRY(Graphics g,String inLine,String inInput,double angle, int iter, int scale, int x, int y) { Graphics2D screengc = null; screengc = (Graphics2D)g; g = buffer.getGraphics(); //g.setColor(Color.blue); // g.fillRect(0, 0, w, h); g.setColor(Color.red); // for (int i = 0; i < w; i += gap) // g.drawLine(i, 0, w - i, h); //for (int i = 0; i < h; i += gap) //g.drawLine(0, i, w, h - i); // Graphics2D g2= (Graphics2D)g; String commandLine; // the string of variables that commands the "turtle movement" //commandLine = "F"; // defines the first iteration of the L-system commandLine = inLine; //String commandInput = "F+F-F-F+F"; // defines the Input update to the commandLine String commandInput = inInput; AffineTransform origin = ((Graphics2D) g).getTransform();//set the origin place holder to the original top-left corner of the applet int commandLength = commandLine.length(); //gets the number of commands in the commandLine String char current; //defines the current command being inspected when the string is read double turnAngle = angle;//Math.PI/2; //defines the constant number of radians a left or right turn is equal to int unit =scale;//4;// scale; //defines the constant length of the unit a forward move equals int iterations =iter;//5;// iter; //System.out.println(commandLength); //System.out.println(commandLine); //to test the performance of the code //g2.translate(x,y);//move origin of the drawing to point (x,y) which is the beginning point of the graphic g.translate(20,20); //loop that draws the L-system at the given number of iterations for(int i=0; i<iterations; i++) { commandLine= myReplace(commandLine, 'F', commandInput);//each iteration changes F's to the following new command commandLength = commandLine.length(); //System.out.println(commandLength); //System.out.println(commandLine); //to test the performance of the code }//finish with for i loop for(int z=0; z<=commandLength; z++) { if(z == commandLength) { ((Graphics2D) g).setTransform(origin);//reset origin to the original applet top left corner. } else if (z < commandLength) { //g.setColor(color); current = commandLine.charAt(z); //System.out.println(k); //System.out.println(current); //to test the performance of the code if(current == 'F') { g.drawLine(0, 0, unit, 0); //draw the line forward one unit from the translated origin along the x axist g.translate(unit, 0);// move the origin to the end of the line } else if(current == '-') { ((Graphics2D) g).rotate(-(turnAngle), 0, 0);//rotate around the start point counter clockwise } else if(current == '+') { ((Graphics2D) g).rotate(turnAngle, 0, 0);//rotate around the start point clockwise } } }//done with the z loop screengc.drawImage(buffer, 0, 0, null); }//finish w/ koch curve //this method takes tree arguments. //first one is the full string. //2nd is a character which will be replaced. //3rd is a string that will replace character. //so this method will change 'char letter' with //with a newString that changes every character (c) to with the replacement string(r) and leaves other characters alone private String myReplace(String oldString, char letter, String replacement) { char currentChar; //to keep track of current character being inspected String updatedString = null; //the updated string which will be returned. for(int i=0; i < oldString.length(); i++) { if(updatedString == null) //for the first iteration of the loop... { currentChar = oldString.charAt(i); if(currentChar == letter) updatedString = (replacement); //assign updatedString the value of replacement else updatedString = ""+ currentChar;//assign updatedString the value of currentChar } else{ //at least 2nd or more iterations if u reached here currentChar = oldString.charAt(i); if(currentChar == letter) updatedString = (updatedString + replacement);//add to the string (updatedString), the replacement line else updatedString = (updatedString + currentChar);//add to the string (updatedString), the currentChar } }//done with the loop return updatedString; }//close the myReplace method public void actionPerformed(ActionEvent e) { //when the iteration is changed on the menu by a user, get it and assign it to userSelectedIterations userSelectedIterations = iterMenu.getSelectedIndex(); //when the system is changed on the menu by a user, get it and assign it to userSelectedSystem userSelectedSystem = selectSystemMenu.getSelectedIndex(); //System.out.println(selectSystemMenu.getSelectedIndex()); //System.out.println(iterMenu.getSelectedIndex()); repaint(); } public void stateChanged(ChangeEvent e) { userSelectedScale = scaler.getValue(); repaint(); } }
-
What about getting the image's Graphics context, setting its color to the background color and calling fillRect(0, 0, imageWidth, imageHeight); with the background color.
For example:
Java Code:public void stateChanged(ChangeEvent e) { userSelectedScale = scaler.getValue(); Graphics g = buffer.getGraphics(); g.setColor(Color.black); g.fillRect(0, 0, buffer.getWidth(null), buffer.getHeight(null)); g.dispose(); repaint(); }Last edited by Fubarable; 03-28-2010 at 12:01 AM.
- 03-28-2010, 12:45 AM #3
Member
- Join Date
- Jun 2009
- Posts
- 11
- Rep Power
- 0
oh wow that was simple enough.... thank you very much... that works :)... ok so now i also plan saving this to a file.... what do you think is the best way of going about doing that?
-
You're quite welcome
Save what, the data? the parameters? the image? If the image, then I would look into possibly using the static write method from the ImageIO class.ok so now i also plan saving this to a file.... what do you think is the best way of going about doing that?
-
Also, I wonder if you're doing too much from within the JPanel's paintComponent method. Specifically, you call the curveOneTRY() method from within it, and this method may be doing some processor-intensive code. If so, you may wish to call a modified version of this method off of paintComponent and then call repaint when it's done.
- 03-28-2010, 01:12 AM #6
Member
- Join Date
- Jun 2009
- Posts
- 11
- Rep Power
- 0
ones again thank you for quick reply!
I am pretty new to java and dont really know what i am doing/talking about most of the time but am interested in learning.
Yes i am talking about saving the Image...
I will post the whole code here so you can see it... let me know if you there is something easier or better way of doing it.
Java Code:import java.awt.*; import java.awt.event.*; import java.awt.geom.AffineTransform; import java.awt.image.BufferedImage; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import javax.swing.*; import javax.swing.event.*; public class LSystemTesting extends JApplet implements ActionListener, ChangeListener { int mx, my; Image buffer = null; int w, h; int userSelectedIterations; // the number of iterations user wants int userSelectedSystem; // the system which user wants to be drawn on the screen int userSelectedScale; // the user can scale the curve to fit on the screen int startX,startY; // the location where the drawing of the curve will start String inputStart="F"; String inputString="F+F-F-F+F"; double inputAngle=Math.PI/2; String systemName[] = new String [40]; BufferedImage grid; JLabel systemLabel; // label for L-System menu JLabel iterationLabel; // label for iteration menu JComboBox iterMenu; // ComboBox for iteration menu JComboBox selectSystemMenu; // ComboBox for system menu JLabel scaleLabel; // label for scale slider JSlider scaler; // actual slider public LSystemTesting() { setSize(1200, 600); Dimension d = getSize(); w = d.width; h = d.height; buffer = new BufferedImage(w, h, BufferedImage.BITMASK); } public void init() { resize(1200,600); //the screen size userSelectedIterations = 0; userSelectedScale = 20; startX= 20; startY= 20; JPanel printToScreen = new JPanel() { public void paintComponent(Graphics g) { super.paintComponent(g); if(userSelectedSystem==0) curveOne(g,inputStart,inputString,inputAngle, userSelectedIterations, userSelectedScale, startX, startY); // Just draw the curveOne. if(userSelectedSystem==1) curveOne(g,"F","F+F-F-F+F",Math.PI/3, userSelectedIterations, userSelectedScale, startX, startY); // Just draw the curveOne. if(userSelectedSystem==2) curveOne(g,"F++F++F","F-F++F-F",Math.PI/3, userSelectedIterations, userSelectedScale, startX, 100); // Just draw the curveOne. if(userSelectedSystem==3) curveOneTRY(g,inputStart,inputString,inputAngle, userSelectedIterations, userSelectedScale, startX, startY); if(userSelectedSystem==4) Sierpinski(g, userSelectedIterations, userSelectedScale, startX, startY); } }; this.getContentPane().add(printToScreen, BorderLayout.CENTER); JPanel optionPanel = new JPanel(); optionPanel.setLayout(new GridLayout(2,3)); systemLabel = new JLabel("Select the LSystem!",JLabel.CENTER); optionPanel.add(systemLabel); //add label to optionPanel iterationLabel = new JLabel("Select number of iterations!",JLabel.CENTER); optionPanel.add(iterationLabel); //add label to optionPanel scaleLabel = new JLabel("Slide to Scale",JLabel.CENTER); optionPanel.add(scaleLabel); //add label to optionPanel selectSystemMenu = new JComboBox(); selectSystemMenu.addItem("Koch Curve"); selectSystemMenu.addItem("Curve Two"); selectSystemMenu.addItem("Curve Three"); selectSystemMenu.addItem("Trial and Error"); selectSystemMenu.addItem("Sierpinski Triangle"); selectSystemMenu.addActionListener(this); optionPanel.add(selectSystemMenu); //*******************************Define the iteration Menu iterMenu = new JComboBox(); for(int i=0; i<6; i++) { iterMenu.addItem(""+i); }//end for loop to add iteration options from one to five iterMenu.setSelectedIndex(userSelectedIterations); iterMenu.addActionListener(this); optionPanel.add(iterMenu); //*******************************Define the scale slider scaler = new JSlider(1,25,userSelectedScale); scaler.setMajorTickSpacing(10); scaler.setMinorTickSpacing(1); scaler.setPaintTicks(true); scaler.addChangeListener(this); optionPanel.add(scaler); this.getContentPane().add(optionPanel, BorderLayout.SOUTH); }//end initiate public void curveOne(Graphics g,String inLine,String inInput,double angle, int iter, int scale, int x, int y) { Graphics2D g2= (Graphics2D)g; String commandLine; // the string of variables that commands the "turtle movement" //commandLine = "F"; // defines the first iteration of the L-system commandLine = inLine; String commandInput = inInput;//"F+F-F-F+F"; // defines the Input update to the commandLine AffineTransform origin = g2.getTransform();//set the origin place holder to the original top-left corner of the applet int commandLength = commandLine.length(); //gets the number of commands in the commandLine String char current; //defines the current command being inspected when the string is read //double turnAngle = Math.PI/2; //defines the constant number of radians a left or right turn is equal to double turnAngle = angle; //System.out.println(""+Math.PI/2); int unit = scale; //defines the constant length of the unit a forward move equals int iterations = iter; //System.out.println(commandLength); //System.out.println(commandLine); //to test to see what we have in commandline g2.translate(x,y);//move origin of the drawing to point (x,y) so the graphic shows up on the screen //loop that draws the L-system at the given number of iterations for(int i=0; i<iterations; i++) { commandLine= myReplace(commandLine, 'F', commandInput);//each iteration changes F's to the following new command commandLength = commandLine.length(); //System.out.println(commandLength); //System.out.println(commandLine); //to test the performance of the code }//finish with for i loop for(int k=0; k<=commandLength; k++) { if(k == commandLength) { g2.setTransform(origin);//reset origin to the original applet top left corner. }else if (k < commandLength) { //g.setColor(color); current = commandLine.charAt(k); //System.out.println(k); //System.out.println(current); //to test the performance of the code if(current == 'F') { g.drawLine(0, 0, unit, 0); //draw the line forward one unit from the translated origin along the x axist g2.translate(unit, 0);// move the origin to the end of the line } else if(current == '-') { g2.rotate(-(turnAngle), 0, 0);//rotate around the start point counter clockwise } else if(current == '+') { g2.rotate(turnAngle, 0, 0);//rotate around the start point clockwise } } }//finish with for k loop }//finish w/ koch curve public void curveOne2(Graphics g, int iter, int scale, int x, int y) { Graphics2D g2= (Graphics2D)g; String commandLine; // the string of variables that commands the "turtle movement" commandLine = "F"; // defines the first iteration of the L-system String commandInput = "F+F-F-F+F"; // defines the Input update to the commandLine AffineTransform origin = g2.getTransform();//set the origin place holder to the original top-left corner of the applet int commandLength = commandLine.length(); //gets the number of commands in the commandLine String char current; //defines the current command being inspected when the string is read double turnAngle = Math.PI/2; //defines the constant number of radians a left or right turn is equal to int unit = scale; //defines the constant length of the unit a forward move equals int iterations = iter; //System.out.println(commandLength); //System.out.println(commandLine); //to test the performance of the code //g2.translate(x,y);//move origin of the drawing to point (x,y) which is the beginning point of the graphic g2.translate(100,100); //loop that draws the L-system at the given number of iterations for(int i=0; i<iterations; i++) { commandLine= myReplace(commandLine, 'F', commandInput);//each iteration changes F's to the following new command commandLength = commandLine.length(); //System.out.println(commandLength); //System.out.println(commandLine); //to test the performance of the code }//finish with for i loop for(int z=0; z<=commandLength; z++) { if(z == commandLength) { g2.setTransform(origin);//reset origin to the original applet top left corner. }else if (z < commandLength) { //g.setColor(color); current = commandLine.charAt(z); //System.out.println(k); //System.out.println(current); //to test the performance of the code if(current == 'F') { g.drawLine(0, 0, unit, 0); //draw the line forward one unit from the translated origin along the x axist g2.translate(unit, 0);// move the origin to the end of the line } else if(current == '-') { g2.rotate(-(turnAngle), 0, 0);//rotate around the start point counter clockwise } else if(current == '+') { g2.rotate(turnAngle, 0, 0);//rotate around the start point clockwise } } }//done with the z loop }//done with the curve public void Sierpinski(Graphics g, int iter, int scale, int x, int y) { Graphics2D g2= (Graphics2D)g; String commandLine; // the string of variables that commands the "turtle movement" //commandLine = "F"; // defines the first iteration of the L-system commandLine = "A"; String replaceA="B-A-B"; String replaceB="A+B+A"; String commandInput = "A";//"F+F-F-F+F"; // defines the Input update to the commandLine AffineTransform origin = g2.getTransform();//set the origin place holder to the original top-left corner of the applet int commandLength = commandLine.length(); //gets the number of commands in the commandLine String char current; //defines the current command being inspected when the string is read //double turnAngle = Math.PI/2; //defines the constant number of radians a left or right turn is equal to double turnAngle = Math.PI/3; //System.out.println(""+Math.PI/2); int unit = scale; //defines the constant length of the unit a forward move equals int iterations = iter; //System.out.println(commandLength); //System.out.println(commandLine); //to test to see what we have in commandline g2.translate(x,y);//move origin of the drawing to point (x,y) so the graphic shows up on the screen //loop that draws the L-system at the given number of iterations for(int i=0; i<iterations; i++) { if(iterations==5) turnAngle = -turnAngle; if(iterations==3) turnAngle = -turnAngle; if(iterations==1) turnAngle = -turnAngle; commandLine= myReplace2(commandLine, 'A', replaceA,'B', replaceB);//each iteration changes F's to the following new command commandLength = commandLine.length(); //System.out.println(commandLength); }//finish with for i loop for(int k=0; k<=commandLength; k++) { if(k == commandLength) { g2.setTransform(origin);//reset origin to the original applet top left corner. }else if (k < commandLength) { //g.setColor(color); current = commandLine.charAt(k); //System.out.println(k); //System.out.println(current); //to test the performance of the code if(current == 'A'|| current =='B') { g.drawLine(0, 0, unit, 0); //draw the line forward one unit from the translated origin along the x axist g2.translate(unit, 0);// move the origin to the end of the line } else if(current == '-') { g2.rotate(-(turnAngle), 0, 0);//rotate around the start point counter clockwise } else if(current == '+') { g2.rotate(turnAngle, 0, 0);//rotate around the start point clockwise } } }//finish with for k loop }//finish w/ koch curve public void curveOneTRY(Graphics g,String inLine,String inInput,double angle, int iter, int scale, int x, int y) { Graphics2D screengc = null; screengc = (Graphics2D)g; g = buffer.getGraphics(); //g.setColor(Color.blue); // g.fillRect(0, 0, w, h); g.setColor(Color.red); // for (int i = 0; i < w; i += gap) // g.drawLine(i, 0, w - i, h); //for (int i = 0; i < h; i += gap) //g.drawLine(0, i, w, h - i); // Graphics2D g2= (Graphics2D)g; String commandLine; // the string of variables that commands the "turtle movement" //commandLine = "F"; // defines the first iteration of the L-system commandLine = inLine; //String commandInput = "F+F-F-F+F"; // defines the Input update to the commandLine String commandInput = inInput; AffineTransform origin = ((Graphics2D) g).getTransform();//set the origin place holder to the original top-left corner of the applet int commandLength = commandLine.length(); //gets the number of commands in the commandLine String char current; //defines the current command being inspected when the string is read double turnAngle = angle;//Math.PI/2; //defines the constant number of radians a left or right turn is equal to int unit =scale;//4;// scale; //defines the constant length of the unit a forward move equals int iterations =iter;//5;// iter; //System.out.println(commandLength); //System.out.println(commandLine); //to test the performance of the code //g2.translate(x,y);//move origin of the drawing to point (x,y) which is the beginning point of the graphic g.translate(20,20); //loop that draws the L-system at the given number of iterations for(int i=0; i<iterations; i++) { commandLine= myReplace(commandLine, 'F', commandInput);//each iteration changes F's to the following new command commandLength = commandLine.length(); //System.out.println(commandLength); //System.out.println(commandLine); //to test the performance of the code }//finish with for i loop for(int z=0; z<=commandLength; z++) { if(z == commandLength) { ((Graphics2D) g).setTransform(origin);//reset origin to the original applet top left corner. } else if (z < commandLength) { //g.setColor(color); current = commandLine.charAt(z); //System.out.println(k); //System.out.println(current); //to test the performance of the code if(current == 'F') { g.drawLine(0, 0, unit, 0); //draw the line forward one unit from the translated origin along the x axist g.translate(unit, 0);// move the origin to the end of the line } else if(current == '-') { ((Graphics2D) g).rotate(-(turnAngle), 0, 0);//rotate around the start point counter clockwise } else if(current == '+') { ((Graphics2D) g).rotate(turnAngle, 0, 0);//rotate around the start point clockwise } } }//done with the z loop screengc.drawImage(buffer, 0, 0, null); }//finish w/ koch curve //this method takes tree arguments. //first one is the full string. //2nd is a character which will be replaced. //3rd is a string that will replace character. //so this method will change 'char letter' with //with a newString that changes every character (c) to with the replacement string(r) and leaves other characters alone private String myReplace(String oldString, char letter, String replacement) { char currentChar; //to keep track of current character being inspected String updatedString = null; //the updated string which will be returned. for(int i=0; i < oldString.length(); i++) { if(updatedString == null) //for the first iteration of the loop... { currentChar = oldString.charAt(i); if(currentChar == letter) updatedString = (replacement); //assign updatedString the value of replacement else updatedString = ""+ currentChar;//assign updatedString the value of currentChar } else{ //at least 2nd or more iterations if u reached here currentChar = oldString.charAt(i); if(currentChar == letter) updatedString = (updatedString + replacement);//add to the string (updatedString), the replacement line else updatedString = (updatedString + currentChar);//add to the string (updatedString), the currentChar } }//done with the loop return updatedString; }//close the myReplace method private String myReplace2(String oldString, char letterA, String replacementA,char letterB, String replacementB) { char currentChar; //to keep track of current character being inspected String updatedString = null; //the updated string which will be returned. for(int i=0; i < oldString.length(); i++) { if(updatedString == null) //for the first iteration of the loop... { currentChar = oldString.charAt(i); if(currentChar == letterA) updatedString = (replacementA); //assign updatedString the value of replacement else if(currentChar == letterB) updatedString = (replacementB); else updatedString = ""+ currentChar;//assign updatedString the value of currentChar } else{ //at least 2nd or more iterations if u reached here currentChar = oldString.charAt(i); if(currentChar == letterA) updatedString = (updatedString + replacementA);//add to the string (updatedString), the replacement line else if(currentChar == letterB) updatedString = (updatedString+ replacementB); else updatedString = (updatedString + currentChar);//add to the string (updatedString), the currentChar } }//done with the loop return updatedString; }//close the myReplace method public void actionPerformed(ActionEvent e) { //when the iteration is changed on the menu by a user, get it and assign it to userSelectedIterations userSelectedIterations = iterMenu.getSelectedIndex(); //when the system is changed on the menu by a user, get it and assign it to userSelectedSystem userSelectedSystem = selectSystemMenu.getSelectedIndex(); //System.out.println(selectSystemMenu.getSelectedIndex()); //System.out.println(iterMenu.getSelectedIndex()); Graphics g = buffer.getGraphics(); // g.setColor(Color.OPAQUE); buffer = new BufferedImage(w, h, BufferedImage.BITMASK); g.fillRect(0, 0, buffer.getWidth(null), buffer.getHeight(null)); g.dispose(); repaint(); } public void stateChanged(ChangeEvent e) { userSelectedScale = scaler.getValue(); Graphics g = buffer.getGraphics(); // g.setColor(Color.OPAQUE); buffer = new BufferedImage(w, h, BufferedImage.BITMASK); g.fillRect(0, 0, buffer.getWidth(null), buffer.getHeight(null)); g.dispose(); repaint(); } public void readSystemName() { int d=0; try { BufferedReader reader = new BufferedReader(new FileReader("C:/Users/Ketan/Desktop/Master Project/MS PROJECT/bin/input.txt")); String line=null; while (reader.readLine()!=null) { line = reader.readLine(); if(line.startsWith("Name:")) { systemName[d]=line; System.out.println("Printing from an array: " + systemName[d]); d++; } } } catch (FileNotFoundException ex) { ex.printStackTrace(); } catch (IOException ex) { ex.printStackTrace(); } } }
-
Again, you're welcome.
I'm sorry, did you have a specific question that needs answering? What you've stated is a bit vague, and most of us don't have the time or inclination to do "here's my code, what needs to be fixed?" type posts, but are happy to do, "I've got such-and-such error on line XXX, what am I doing wrong?"I will post the whole code here so you can see it... let me know if you there is something easier or better way of doing it.
Much luck!
- 03-28-2010, 01:29 AM #8
Member
- Join Date
- Jun 2009
- Posts
- 11
- Rep Power
- 0
oh thank you... u were just stating something out me having too much work being done on Paint()... but thats fine... i appropriate your help.
I am going to give the "saving image" thing a try and i will come post a question if i need more help.
Thank you very much! :)
-
OK, regarding that issue, pretty much everything below super.paintComponent(g) shouldn't be called in paintComponent or from a method that's called in paintComponent:
Instead, call these methods from within some action listener, in these methods update your BufferedImage but don't call drawImage from these methods. Instead call repaint on the printToScreen panel and let this JPanel draw the image. The panel's paintComponent method should instead look something like this:Java Code:public void paintComponent(Graphics g) { super.paintComponent(g); if(userSelectedSystem==0) curveOne(g,inputStart,inputString,inputAngle, userSelectedIterations, userSelectedScale, startX, startY); // Just draw the curveOne. if(userSelectedSystem==1) curveOne(g,"F","F+F-F-F+F",Math.PI/3, userSelectedIterations, userSelectedScale, startX, startY); // Just draw the curveOne. if(userSelectedSystem==2) curveOne(g,"F++F++F","F-F++F-F",Math.PI/3, userSelectedIterations, userSelectedScale, startX, 100); // Just draw the curveOne. if(userSelectedSystem==3) curveOneTRY(g,inputStart,inputString,inputAngle, userSelectedIterations, userSelectedScale, startX, startY); if(userSelectedSystem==4) Sierpinski(g, userSelectedIterations, userSelectedScale, startX, startY); }
The reason this is important is that you don't want to do anything that has a chance of slowing Swing's painting down else your app will appear to the user to be very slow and non-responsive.Java Code:public void paintComponent(Graphics g) { super.paintComponent(g); if (buffer != null) { g.drawImage(buffer, 0, 0, null); } }
- 03-28-2010, 04:42 PM #10
Or, for better expressing the intention of the code, set the Graphics' background and call clearRect(0, 0, width, height).
dbJava Code:public void stateChanged(ChangeEvent e) { userSelectedScale = scaler.getValue(); Graphics2D g = buffer.createGraphics(); // not getGraphics -- see the API g.setBackground(Color.black); g.clearRect(0, 0, buffer.getWidth(), buffer.getHeight()); g.dispose(); repaint(); }
- 03-29-2010, 01:01 AM #11
Member
- Join Date
- Jun 2009
- Posts
- 11
- Rep Power
- 0
ok i need to spend some time and work on those things...
but for now, i have two problems...
The first one is that... i can only get image to save as "PNG" format and the other formats dont save....
the send problem is that my savepicture part of the code is being excuted when i change number of iterations or the system... when it only should be called when saveformat is changed.... i know the problem but i was wondering how i can track when "saveAsMenu" CombBox value changes.
thanks in advance :)
Java Code:public void actionPerformed(ActionEvent e) { //when the iteration is changed on the menu by a user, get it and assign it to userSelectedIterations userSelectedIterations = iterMenu.getSelectedIndex(); //when the system is changed on the menu by a user, get it and assign it to userSelectedSystem userSelectedSystem = selectSystemMenu.getSelectedIndex(); //System.out.println(selectSystemMenu.getSelectedIndex()); //System.out.println(iterMenu.getSelectedIndex()); Graphics g = buffer.getGraphics(); buffer = new BufferedImage(w, h, BufferedImage.BITMASK); g.fillRect(0, 0, buffer.getWidth(null), buffer.getHeight(null)); g.dispose(); // g.setColor(Color.OPAQUE); String format = (String)saveAsMenu.getSelectedItem(); System.out.println(format); if(format.equalsIgnoreCase("bmp")||format.equalsIgnoreCase("gif")|| format.equalsIgnoreCase("jpeg")||format.equalsIgnoreCase("jpg")|| format.equalsIgnoreCase("png")||format.equalsIgnoreCase("wbmp")) { File saveFile = new File("LSystem." + format); JFileChooser chooser = new JFileChooser(); chooser.setSelectedFile(saveFile); int returnedValue = chooser.showSaveDialog(saveAsMenu); if (returnedValue == JFileChooser.APPROVE_OPTION) { saveFile = chooser.getSelectedFile(); try { ImageIO.write((BufferedImage)buffer, format, saveFile); format=""; } catch (IOException ex) { } } } repaint(); }
- 03-29-2010, 01:34 AM #12
Member
- Join Date
- Jun 2009
- Posts
- 11
- Rep Power
- 0
ok so i figured out one of the question... i figured out how to track the combobox changes... but now i still need help with saving the image.
-
You really never want to throw away exceptions like this:
Else, how would you know what's wrong? At least print the stack trace.Java Code:try { ImageIO.write((BufferedImage)buffer, format, saveFile); format=""; } catch (IOException ex) { }
- 03-29-2010, 02:20 AM #14
Member
- Join Date
- Jun 2009
- Posts
- 11
- Rep Power
- 0
yeah you are right... i have been posting stripped down version of the code on here so ppl dont have to spend hours to read it...
this is what i have in my full code
but anyways i find this very weird that only PNG works and others dont... cant seem to figure it out... it tried different things as far as casting image to bufferedimage.... changing the type Image to bufferedImage... its probably something very stupid im doing.Java Code:catch (IOException ex) { System.out.println("Problem saving image: " +ex); }
-
In situations like this, I often find that the best thing to do is to work on the problem in isolation in a small program. I suggest you create a very simple program, it doesn't have to be a GUI, that has no user interaction code, and that only creates a BufferedImage, draws on it, and tries to save it as a JPG or whatever, and try to get it to work. If it doesn't then post this small program here, for it will likely more easily show the core of your problem -- likely for you, and you will likely then solve it. If not, we'll have an easier time solving it. This is called creating an SSCCE.
- 03-29-2010, 02:39 AM #16
Member
- Join Date
- Jun 2009
- Posts
- 11
- Rep Power
- 0
ill give it a try... thanks!
-
An example of a small bit of code that works for me:
Java Code:import java.awt.*; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import javax.swing.*; public class LSystemTestingSave { private static final int SIDE = 400; private static final Color LIGHT_BLUE = new Color(200, 200, 255); private static final int COUNT = 20; BufferedImage buffer = new BufferedImage(SIDE, SIDE, BufferedImage.TYPE_INT_RGB); public LSystemTestingSave() { Graphics g = buffer.getGraphics(); Graphics2D g2 = (Graphics2D) g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g.setColor(LIGHT_BLUE); g.fillRect(0, 0, SIDE, SIDE); g.setColor(Color.red); for (int i = 0; i <= COUNT; i++) { int y2 = (i * SIDE) / COUNT; g.drawLine(0, 0, SIDE, y2); g.drawLine(SIDE, 0, 0, y2); g.drawLine(0, SIDE, SIDE, y2); g.drawLine(SIDE, SIDE, 0, y2); } g.dispose(); // show it ImageIcon icon = new ImageIcon(buffer); JLabel label = new JLabel(icon); JOptionPane.showMessageDialog(null, label); // save it String format = "gif"; // this works //String format = "jpg"; // and so does this File saveFile = new File("LSystem." + format); JFileChooser chooser = new JFileChooser(); chooser.setSelectedFile(saveFile); int returnedValue = chooser.showSaveDialog(null); if (returnedValue == JFileChooser.APPROVE_OPTION) { saveFile = chooser.getSelectedFile(); try { ImageIO.write(buffer, format, saveFile); } catch (IOException ex) { ex.printStackTrace(); } } } public static void main(String[] args) { new LSystemTestingSave(); } }
- 03-29-2010, 03:23 AM #18
Member
- Join Date
- Jun 2009
- Posts
- 11
- Rep Power
- 0
oh wow you didnt have to go that far... but thank you very much... i figured out the problem was line:
"buffer = new BufferedImage(w, h, BufferedImage.BITMASK);"
i changed it to:
"buffer = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);"
and it seems to be working fine but i really dont like the black background... is there anyway i can get ride of it?
also what would be the best way to get the image centered on the buffer?
-
Nice going!
I'm not a 2D graphics pro, so there may be a better answer, but you could always fill it with a Color as I do above or clear it as Darryl shows in this thread.i changed it to:
"buffer = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);"
and it seems to be working fine but i really dont like the black background... is there anyway i can get ride of it?
That's strictly a geometry question more so than a Java question.also what would be the best way to get the image centered on the buffer?
- 03-29-2010, 07:45 PM #20
Member
- Join Date
- Jun 2009
- Posts
- 11
- Rep Power
- 0
Similar Threads
-
BufferedImage rgb
By Bill87 in forum New To JavaReplies: 2Last Post: 02-25-2010, 06:31 PM -
Using BufferedImage
By timkd127 in forum New To JavaReplies: 5Last Post: 12-19-2009, 09:17 PM -
BufferedImage through FTP
By dudejonne in forum New To JavaReplies: 7Last Post: 11-05-2009, 05:36 PM -
Help needed Clearing the contents of a File
By sandeepsai17 in forum New To JavaReplies: 5Last Post: 07-02-2009, 02:31 PM -
MouseListener - clearing a textField when clicked on?!
By sailor_girl in forum AWT / SwingReplies: 4Last Post: 03-01-2009, 05:01 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks