How to create a jpg file for a desired  part of the canvas

i hav to create a jpg file from the canvas which contains basic shapes which are drawn using methods like drawOval(),drawRectangle(). i need only a part of the canvas to be saved as a jpg file i.e for example from xpos 50-100 & ypos 200-250.i need all the portions of the shapes already drawn in that area as a jpg file..
can anyone help me?

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
public class CanvasClip extends Canvas implements ActionListener
    Rectangle clip;
    boolean showClip;
    public CanvasClip()
        clip = new Rectangle(50, 50, 150, 150);
        showClip = false;
    public void paint(Graphics g)
        super.paint(g);
        Graphics2D g2 = (Graphics2D)g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                            RenderingHints.VALUE_ANTIALIAS_ON);
        int w = getWidth();
        int h = getHeight();
        int dia = Math.min(w,h)/4;
        g2.setPaint(getBackground());
        g2.fillRect(0,0,w,h);
        g2.setPaint(Color.blue);
        g2.drawRect(w/16, h/16, w*7/8, h*7/8);
        g2.setPaint(Color.red);
        g2.fillOval(w/8, h/12, w*3/4, h*5/6);
        g2.setPaint(Color.green.darker());
        g2.fillOval(w/2-dia/2, h/2-dia/2, dia, dia);
        g2.setPaint(Color.orange);
        g2.drawLine(w/16+1, h/16+1, w*15/16-1, h*15/16-1);
        if(showClip)
            g2.setPaint(Color.magenta);
            g2.draw(clip);
    public void actionPerformed(ActionEvent e)
        Button button = (Button)e.getSource();
        String ac = button.getActionCommand();
        if(ac.equals("show"))
            showClip = !showClip;
            repaint();
        if(ac.equals("save"))
            save();
    private void save()
        int w = clip.width;
        int h = clip.height;
        BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = img.createGraphics();
        g2.translate(-clip.x, -clip.y);
        paint(g2);
        g2.dispose();
        String ext = "jpg";  // or "png"; "bmp" okay in j2se 1.5
        try
            ImageIO.write(img, "jpg", new File("canvasClip.jpg"));
        catch(IOException ioe)
            System.err.println("write error: " + ioe.getMessage());
    private Panel getUIPanel()
        Button show = new Button("show clip");
        Button save = new Button("save");
        show.setActionCommand("show");
        save.setActionCommand("save");
        show.addActionListener(this);
        save.addActionListener(this);
        Panel panel = new Panel();
        panel.add(show);
        panel.add(save);
        return panel;
    public static void main(String[] args)
        CanvasClip cc = new CanvasClip();
        Frame f = new Frame();
        f.addWindowListener(new WindowAdapter()
            public void windowClosing(WindowEvent e)
                System.exit(0);
        f.add(cc);
        f.add(cc.getUIPanel(), "South");
        f.setSize(400,400);
        f.setLocation(200,200);
        f.setVisible(true);
}

Similar Messages

  • HOw to create a Batch file for java application and whats the use of this ?

    HI,
    How to create a Batch file for java application ?
    And whats the use of creating batch file ?
    Thanks in advance

    First of all, you're OT.
    Second, you can find this everywhere in the net.
    If you got a manifest declaring main class (an classpath if needed), just create a file named whatever.bat, within same directory of jar file, containing:
    javaw -jar ./WhateverTheNameOfYourJarIs.jar %*By the way, assuming a Windows OS, you can just double click the jar file (no batch is needed).
    Otherwise use:
    javaw -cp listOfJarsAndDirectoriesSeparedBySemiColon country/company/application/package/className %*Where 'country/company/application/package/' just stands for a package path using '/' as separator instead of '.'
    Don't specify the .class extension.
    Javaw only works on Windows (you asked for batch, I assumed .BAT, no .sh), in Linux please use java.exe (path may be needed, Windows doesn't need it 'cause java's executables are copied to system32 folder in order to be always available, see PATH environment variable if you don't know what I'm talking about) and use ':' as classpath (cp) separator.
    The '%***' tail is there in order to pass all parameters, it only works on Windows, refer to your shell docs for other OSs (something like $* may work).
    This way you have a command you can call to launch your code (instead of opening NetBeans just to see your app working). You could schedule tasks on it or just call it in any command prompt (hope you know what it is 'cause there have been people in this very same forum with no clue about it, if not just hold the 'Windows button' and press 'R', then type 'cmd' and run it).
    Finally add dukes and give 'hem away.
    Bye.

  • How to create different log files for each of web applications deployed in OC4J

    Hi All,
    I am using OC4J(from Oracle) v1.0.2.2 and Windows2000. Now I want to know
    1. how to create different log files for each of my deployed web applications ?
    2. what are the advantages in running multiple instances of oc4j and in what case we should run
    multiple instances of OC4J ?
    3. how to run OC4J as Windows2000 Service rather than Windows2000 Application ?
    Thanks and Regards,
    Kumar.

    Hi Avi,
    First of all I have given a first reading to log4j and I think there will some more easy way of logging debugging messages than log4j (If you could provide me a detailed explanation of a servlet,jsp,java bean that uses log4j and how to use log4j then it will be very helpful for me). The other easy ways (if I am not using log4j) to my problem i.e creating different log files for each of web applications deployed in oc4j are
    I have created multiple instances of OC4J that are configured to run on different ports and so on each instance I have deployed a single web application . And I started the 2 oc4j instances by transferring thier error/log messages to a file. And the other way is ..
    I have download from jakarta site a package called servhelper . This servhelper is a thread that is started in a startup servlet and stopped in the destroy method of that startup servlet. So this thread will automatically capture all the system.out.println's and will print those to a file. I believe that this thread program is synchronized. So in this method I need not run multiple instances of OC4J instead each deployed web application on single instance of oc4j uses the same thread program (ofcourse a copy of thread program is put in each of the deployed web applications directories) to log messages on to different log files.
    Can you comment on my above 2 approached to logging debugging messages and a compartive explanation to LOG4J and how to use LOG4J using a simple servlet, simple jsp is appreciated ...
    Thanks and Regards,
    Ravi.

  • How to create a Batch file for java application  ?

    HI,
    How to create a Batch file for java application ?
    And whats the use of creating batch file ?
    Thanks in advance

    [http://static.springsource.org/spring-batch/]
    Assuming you want to develop a batch application rather than actually just create a .bat file ..

  • How to create a password file for executing psadmin command to deploy portl

    how to create a password file for executing psadmin command to deploy portlet

    What you have done is perfectly right. The password file doesn't have anything else apart from the password
    for example in your case
    $echo password > /tmp/password.txt
    However I remember that in windows install, the Application server used to wait for a user's input when a deploy was to be done for the first time. So Can you read the Release notes or the Readme file which has come with windows.
    The solution was,
    manually use asadmin command of application server to deploy some war (any webapp will do), at this time, a question will be prompted to accept a certificate. once this is done, deploy portlet should work fine!!!
    HTH

  • How to create an inspection lot for each line item of the Purchase order?

    Hi,
    How to create an inspection lot for each line item of the Purchase order ?
    In detail if possible.

    Hi
       please check this
    [thread|Create Inspection Lot;

  • How to create a .jar file for the BPEL project

    hi all,
    how can i create a .jar file for my BPEL project, i am trying to deploy the project but getting an error :
    "Error: [Error ORABPEL-10902]: compilation failed [Description]: in "C:\OraBPELPM\integration\jdev\jdev\mywork\Workspace_BPELDemo\BPEL_Report\BPEL_Report.bpel", XML parsing failed because "". [Potential fix]: n/a. "
    and when doubel cliking on this error Jdev take me to the beginning of the ".bpel" file.
    I 'm really stuck in here i am not able to deploy the process!
    Thanks,
    Rana

    Rana:
    If you haven't already, I would post this question in the BPEL forum. I would expect a much better turnaround there.
    Johnny Lee

  • How to create an XML File for Gantt?

    Hi Everybody.
    I need a Gantt in Web Dynpro ABAP. I need it for the boss to do a Forecast for Vacation. I Hope this is understanding.
    Did Anyone know how can i create a XML File for the Gantt at runtime?
    Thanks
    MSi

    Hi Marcus,
    the following link is to the JNet/JGantt developer doco
    [JNet/JGantt Developer Documentation|http://www.sdn.sap.com/irj/sdn/go/portal/prtroot/docs/lw/uuid/f010ec31-9658-2910-3c83-c6e62904eceb]
    included in this is the:
    [Schema for XML|http://www.sdn.sap.com/irj/sdn/go/portal/prtroot/com.sap.km.cm.docs/lw/webdynpro/jnet_jgantt%20developer%20documentation/schema/xml-spy/jnet-schema.html]
    You can use this to build XML to represent a gantt like chart.
    I did this to build XML to represent employee avaliablity - although we fronted the gantt through WD Java not ABAP - the java applet called is the same (I believe).
    Don't underestimate the work required to tweek your data into a decent layout. It took me days.
    There may be tools/api to help generate the XML - I don't know of them - I'd look forward to seeing any other replies to this thread from people who have used/built any.  An example of a class that generates JNet XML - CL_SPI_UI_JNET - but this does not seem to be a Gantt display.
    Cheers,
    Chris

  • How to Create a Dump file  for a MaxDB database

    Hi
      I have to create a dump file for a database in MaxDB.
    IN MaxDB How to Create a DataBase and How to Connect to
    MaxDB.

    Hi,
    what exactly do you mean with a 'dump file'? Are you referring to a backup?
    You should be able to find your answers here: http://dev.mysql.com/doc/maxdb/maxdb_faq.html.
    If not, please let me know.
    Regards,
    Roland

  • How to create different log files for different users in log4j

    I want to create different logs for different users, using different appenders for each user so that logs are created in his file only.
    Confusion:How to direct them to different files in my logger class

    Hi Avi,
    First of all I have given a first reading to log4j and I think there will some more easy way of logging debugging messages than log4j (If you could provide me a detailed explanation of a servlet,jsp,java bean that uses log4j and how to use log4j then it will be very helpful for me). The other easy ways (if I am not using log4j) to my problem i.e creating different log files for each of web applications deployed in oc4j are
    I have created multiple instances of OC4J that are configured to run on different ports and so on each instance I have deployed a single web application . And I started the 2 oc4j instances by transferring thier error/log messages to a file. And the other way is ..
    I have download from jakarta site a package called servhelper . This servhelper is a thread that is started in a startup servlet and stopped in the destroy method of that startup servlet. So this thread will automatically capture all the system.out.println's and will print those to a file. I believe that this thread program is synchronized. So in this method I need not run multiple instances of OC4J instead each deployed web application on single instance of oc4j uses the same thread program (ofcourse a copy of thread program is put in each of the deployed web applications directories) to log messages on to different log files.
    Can you comment on my above 2 approached to logging debugging messages and a compartive explanation to LOG4J and how to use LOG4J using a simple servlet, simple jsp is appreciated ...
    Thanks and Regards,
    Ravi.

  • How to create a dll file for lab view?

    Hello
       I want to create a DLL file for LabView. Can anyone help me in this issue?
    Regards
    Vivek 

    Please explain.
    Do you want to create a dll in another language (which one?) for use in LabVIEW or do you want to create a dll out of your LabVIEW code?
    LabVIEW Champion . Do more with less code and in less time .

  • How to create a .exe file for a java application

    Hi, I want to create a .exe file for a java application I am developing so that I can schedule it to run at a particular time using Windows Scheduler on WinNT. Is there any way to accomplish this? Or is there any other way in which a Java application can be scheduled to run at a particular time on Windows everyday?

    Create a .bat file and run that. Just have, in the .bat file:
    java YourClassName Alternatively, you can use javaw YourClassName (without *.bat, just put it into Windows Scheduler like suggested above).
    using javaw won't pop up any window...

  • How to create an exe file for single class

    Hi all,
    l have one some program with only one class and want to create one exe file for
    easily execute for the user.
    I've use exe4j and another tool to do but all failure, the error message always shown me that main class not found.
    Any idea??
    Thanks a lot!

    I suspect your class has somewhat invalid thing.
    Have you verified:
    - The class you wish to be executed contains public static void main(String args[])- The package declaration on your executable-wannabe class is valid
    - In case you need to import another class, make sure the import statement(s) is valid
    No matter what kinda program you use to create .exe from java class, the above things must be valid. AFAIK.
    Regards,
    Farid Herman

  • Is there a way to create a .dmg file for a DVD inserted into the system

    I am wondering if Snow Leopard has integrated any tools that can create a .dmg file for the CD or DVD inserted into the system?
    If not, any recommendations for freeware 3rd party tools? Thanks

    Insert the disc you want to create the .dmg from, then choose the disc in the left hand pane of Disk Utility. From the File menu, choose New > Disk Image from (name of disc).
    EDIT: This is basically how it's done; however, there may be other steps involved, depending on the type of disc you're trying to copy and what your goals are with the .dmg. If you provide more details about what you're trying to achieve, maybe we can help you further.
    Message was edited by: Tuttle

  • How to create separate log files for each deployed web application in oc4j

    Hi All,
    I am using Windows2000, Oracle9iAS(OC4J). Say I have deployed 3 web applications onto this oc4j server. Then how to create 3 different log files so that I can see the log messages(System.out.println's) of each of these web appliations in a different log file.
    Thanks and Regards,
    Ravi.

    Where do the messages printed via ServletContext.log() go? Is this configurable separately by web application? If so, you could at least replace your System.out.println() with sc.log() statements. For exceptions, you could trap them and log them since the log() method takes a throwable as well as a String.
    John H.

Maybe you are looking for

  • Problem while configuring webutil on oracle application server 10g(9.0.4)

    hi, I have configured webutil with oracle application server 10g(9.0.4).JInitiator 1.3.1.17 is installed. I did everything according to webutil manual.Then i developed a form using developer suit 10g (9.0.4). and successfully compiled and attached we

  • I need some help with my server's permissions asap!

    Well, I am here for a reason as you can tell from my subject. Before I ask my question, I will explain my setup so you have a better understanding of what I am trying to do. I have an intel iMac running Mac OS X Server 10.5. It is setup at home with

  • .oam in indesign for ibooks author – does not work help

    Hi I am creating an epub3 in indesign for ipad to read in ibooks. I am trying to integrate an .oam file but it doesn't work. It will show up in indesign as adobe promised but it is not shown in ibooks on ipad. Can anyone help me? Thanks for help.

  • Outbound Interface(Applicaion server)

    Hi all, currently iam working on outbound interface,in which i have the data in one internal table,now i need to download this to application server with this path '/usr/sap/ED1/file.txt',can anyone give me the logic or code to do this. Points assure

  • Subform minimum height property minH not working

    Hi, I am trying to set the minH property of a subform, so that it is always the same size as the content area that it is sitting in.  This is because it has a border around it - I want the resulting box to be the same size regardless of whether the t