Results 1 to 7 of 7
  1. #1
    johnyjj2 is offline Member
    Join Date
    Aug 2009
    Posts
    13
    Rep Power
    0

    Default draw to file with GDC

    Dear all,

    I have the following code (this is one of menu options):

    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();  
                }
    The difficulty for me is range of variables parent (and, in analogical way, width and height):

    parent is not public in java.awt.Component; cannot be accessed from outside package
    and using drawRect and drawString in the context of the code above.

    Regards!

  2. #2
    Rewry is offline Member
    Join Date
    Dec 2011
    Posts
    19
    Rep Power
    0

    Default 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

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,197
    Rep Power
    19

    Default Re: draw to file with GDC

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    johnyjj2 is offline Member
    Join Date
    Aug 2009
    Posts
    13
    Rep Power
    0

    Default 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

  5. #5
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default Re: draw to file with GDC

    Where have you declared your parent variable, what container is parent to JFileChooser?

  6. #6
    johnyjj2 is offline Member
    Join Date
    Aug 2009
    Posts
    13
    Rep Power
    0

    Default 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

    Java 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) { ... }
        
    }
    MyDataBase.java

    Java Code:
    public class MyDataBase extends Vector { ... }
    ElementOfDatabase.java

    Java Code:
    public class ElementOfDatabase {
    
        public void draw(Graphics gDC) { ... }
        
    }
    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.

    Regards!

  7. #7
    johnyjj2 is offline Member
    Join Date
    Aug 2009
    Posts
    13
    Rep Power
    0

    Default 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

    Java Code:
    int returnVal = chooser.showOpenDialog(parent);
    to

    Java Code:
    int returnVal = chooser.showOpenDialog(NameOfMyApplication.this);
    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.

    Regards!
    Last edited by johnyjj2; 12-04-2011 at 05:04 PM. Reason: adding code tags

Similar Threads

  1. why cant i draw this box???
    By stefandanielsen in forum New To Java
    Replies: 2
    Last Post: 05-12-2011, 02:53 PM
  2. Complicated Draw
    By Desdenova in forum New To Java
    Replies: 9
    Last Post: 05-27-2010, 08:44 PM
  3. how to draw an arc
    By Baker in forum New To Java
    Replies: 1
    Last Post: 04-16-2009, 09:05 PM
  4. SWT Draw 2D Demonstration
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 06-28-2008, 09:22 PM
  5. help me draw... please...
    By kureikougaiji in forum New To Java
    Replies: 1
    Last Post: 01-28-2008, 12:22 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •