Results 1 to 7 of 7
Thread: draw to file with GDC
- 12-03-2011, 01:38 PM #1
Member
- Join Date
- Aug 2009
- Posts
- 13
- Rep Power
- 0
draw to file with GDC
Dear all,
I have the following code (this is one of menu options):
The difficulty for me is range of variables parent (and, in analogical way, width and height):Java Code:else if (menuOpen.equals(cmd)) { //http://docs.oracle.com/javase/1.4.2/docs/api/java/awt/image/MemoryImageSource.html //======== DRAWING IN THE MEMORY ======== int w = 100; int h = 100; int pix[] = new int[w * h]; int index = 0; for (int y = 0; y < h; y++) { int red = (y * 255) / (h - 1); for (int x = 0; x < w; x++) { int blue = (x * 255) / (w - 1); pix[index++] = (255 << 24) | (red << 16) | blue; } } Image img = createImage(new MemoryImageSource(w, h, pix, 0, w)); //http://docs.oracle.com/javase/7/docs/api/javax/swing/JFileChooser.html //=== CHOOSING WHERE TO SAVE THE FILE ====== JFileChooser chooser = new JFileChooser(); FileNameExtensionFilter filter = new FileNameExtensionFilter( "JPG & GIF Images", "jpg", "gif"); chooser.setFileFilter(filter); int returnVal = chooser.showOpenDialog(parent); if(returnVal == JFileChooser.APPROVE_OPTION) { System.out.println("You chose to open this file: " + chooser.getSelectedFile().getName()); } //===== EDITING THE PICTURE ======= int type = BufferedImage.TYPE_INT_ARGB; // or another one BufferedImage bi = new BufferedImage(width, height, type); Graphics2D g2d = bi.createGraphics(); // first write the existing image g2d.drawImage(image, 0, 0, null); // now draw anything else // and when done, dispose g2d.dispose(); }
and using drawRect and drawString in the context of the code above.parent is not public in java.awt.Component; cannot be accessed from outside package
Regards!
- 12-03-2011, 02:28 PM #2
Member
- Join Date
- Dec 2011
- Posts
- 19
- Rep Power
- 0
Re: draw to file with GDC
Can you explain briefly what your code does, and where is the problem exactly, and ill see what i can do
- 12-03-2011, 02:44 PM #3
Re: draw to file with GDC
Why do they call it rush hour when nothing moves? - Robin Williams
- 12-04-2011, 11:26 AM #4
Member
- Join Date
- Aug 2009
- Posts
- 13
- Rep Power
- 0
Re: draw to file with GDC
OK, I have mentioned on the other forum that I'm going to reply here. In the future I'm going to include link to other forums in case of crossposting or even avoid crossposting at all.
What I need to do is to create some kind of tree. I've got class which represents an object and a class which represents collection of those objects and how those are connected with each other. After creating such a representation of the tree in the memory I'd like to draw it and save to a file like jpg, gif, png, bmp. I'd like to show its preview on the screen too.
However, such a tree may be big drawing so I thought at first to create it in bitmap file and then showing this file in smaller version as a preview in the application. The drawing will consist on rectangles with little text inside those and some arrows connecting the rectangles. The height of drawing will be about three to ten rows, however width may be rather long, like hundread of rectangles.
The whole code is in the menu, one position creates the tree in the memory, the next draws everything. I've got a difficulty with the latter. At this moment I'd like to create examplary picture with rectangle, string and arrow (all what I need) and save it to the file (different file extensions, at least bmp which does not use any conversion of bitmap to use less disc space). And the difficulty which I have is especially with drawing rectangles and strings (the method which I used previously don't want to work) and range of the variables (as mentioned in my first post).
So drawing and saving is the most important issue which I need to solve at first. Then I will move to choosing the directory of destination (where to save the file), converting between different types of the file and showing the preview.
Regards!Last edited by johnyjj2; 12-04-2011 at 11:33 AM. Reason: adding info about difficulties which I have
- 12-04-2011, 12:24 PM #5
Senior Member
- Join Date
- Jan 2011
- Location
- Belgrade, Serbia
- Posts
- 227
- Rep Power
- 3
Re: draw to file with GDC
Where have you declared your parent variable, what container is parent to JFileChooser?
- 12-04-2011, 02:13 PM #6
Member
- Join Date
- Aug 2009
- Posts
- 13
- Rep Power
- 0
Re: draw to file with GDC
Thank you for your reply!
My code can be summarized to (I have written only some of most important functions below, omitted others):
MainFileInProject.java
MyDataBase.javaJava Code:public class MainFileInProject extends Frame implements WindowListener, ActionListener, MouseListener { public MainFileInProject() { ... } public static void main(String[] args) { ... } public void paint(Graphics gDC) { ... } public void actionPerformed(ActionEvent e) { ... } }
ElementOfDatabase.javaJava Code:public class MyDataBase extends Vector { ... }
In fact, I am not sure whre I should declare parent. The whole function of drawing is in MainFileInProject, in actionPerformed(ActionEvent e), which is responsible for options from menu. I can access my "database", i.e. elements from memory, in my menu.Java Code:public class ElementOfDatabase { public void draw(Graphics gDC) { ... } }
Regards!
- 12-04-2011, 05:03 PM #7
Member
- Join Date
- Aug 2009
- Posts
- 13
- Rep Power
- 0
Re: draw to file with GDC
Hello,
at this moment I can open the window to choose where to save the file => I had to change from
toJava Code:int returnVal = chooser.showOpenDialog(parent);
However, even if I can choose the directory and name of the file, it does not create any file :(. I need to somehow connect returnVal with bi.Java Code:int returnVal = chooser.showOpenDialog(NameOfMyApplication.this);
Regards!Last edited by johnyjj2; 12-04-2011 at 05:04 PM. Reason: adding code tags
Similar Threads
-
why cant i draw this box???
By stefandanielsen in forum New To JavaReplies: 2Last Post: 05-12-2011, 02:53 PM -
Complicated Draw
By Desdenova in forum New To JavaReplies: 9Last Post: 05-27-2010, 08:44 PM -
how to draw an arc
By Baker in forum New To JavaReplies: 1Last Post: 04-16-2009, 09:05 PM -
SWT Draw 2D Demonstration
By Java Tip in forum SWTReplies: 0Last Post: 06-28-2008, 09:22 PM -
help me draw... please...
By kureikougaiji in forum New To JavaReplies: 1Last Post: 01-28-2008, 12:22 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks