Cutting and moving around - g2d

Hello java people!
I'm still fooling around with this example program here:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
public class DrawOnImage extends JFrame implements ActionListener
    DrawingPanel mdp;
    JPanel bp;
    JButton redButton,clearButton;
    public DrawOnImage()
        mdp = new DrawingPanel();
        bp = new JPanel();
        bp.setLayout(new GridLayout(2, 4, 2, 2));
        redButton = addButton(Color.red);
        clearButton = addButton(null);
        clearButton.setText("New");
        getContentPane().add("Center", mdp);
        getContentPane().add("South", bp);
    private JButton addButton(Color color)
        JButton b = new JButton();
        if (color != null)b.setBackground(color);
        b.setText("Paint");
        bp.add(b);
        b.addActionListener(this);
        return(b);
    public void actionPerformed(ActionEvent e)
        String s = e.getActionCommand();
        if (s.equals("Paint"))
            JButton button = (JButton)e.getSource();
            mdp.setPaintColor(button.getBackground());
        if (s.equals("New"))
            mdp.clearPaint();
    public static void main(String[] args)
        JFrame frame = new DrawOnImage();
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible( true );
    public class DrawingPanel extends JPanel implements MouseListener, MouseMotionListener
        Image image;
        Graphics2D g2d;
        int brushSize = 5;
        Point lastPoint;
        public DrawingPanel()
            setPreferredSize( new Dimension(300, 300) );
            addMouseListener(this);
            addMouseMotionListener(this);
        public void mouseClicked(MouseEvent e) {}
        public void mouseEntered(MouseEvent e) {}
        public void mouseExited(MouseEvent e) {}
        public void mouseReleased(MouseEvent e) {}
        public void mouseMoved(MouseEvent e) {}
        public void mousePressed(MouseEvent e)
            lastPoint = e.getPoint();
            draw( lastPoint );
        public void mouseDragged(MouseEvent e)
            double xDelta = e.getX() - lastPoint.getX();
            double yDelta = e.getY() - lastPoint.getY();
            double delta = Math.max(Math.abs(xDelta), Math.abs(yDelta));
            double xIncrement = xDelta / delta;
            double yIncrement = yDelta / delta;
            double xStart = lastPoint.getX();
            double yStart = lastPoint.getY();
            for (int i = 0; i < delta; i++)
                Point interpolated = new Point((int)xStart, (int)yStart);
                draw( interpolated );
                xStart += xIncrement;
                yStart += yIncrement;
            draw(e.getPoint());
            lastPoint = e.getPoint();
        private void draw(Point start)
            int x = start.x - (brushSize/2) + 1;
            int y = start.y - (brushSize/2) + 1;
            g2d.fillOval(x, y, brushSize, brushSize);
            repaint(x, y, brushSize, brushSize);
        public void setPaintColor(Color color)
            g2d.setColor(color);
        public void clearPaint()
            g2d.setColor( Color.white );
            g2d.fillRect(0, 0, getWidth(), getHeight());
            repaint();
            g2d.setColor( Color.black );
        public void paintComponent(Graphics g)
            super.paintComponent(g);
            if (image == null)
                image = createImage(getWidth(), getHeight());
                g2d = (Graphics2D)image.getGraphics();
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                g2d.setColor(Color.white);
                g2d.fillRect(0, 0, getWidth(), getHeight());
                g2d.setColor(Color.black);
            Rectangle r = g.getClipBounds();
            g.drawImage(image, r.x, r.y, r.width+r.x, r.height+r.y, r.x, r.y, r.width+r.x, r.height+r.y, null);
}My version got a tidbit more complicated, but that's besides the point. What I want to do now is mark a certain selection of the image, and move it around, place it somewhere else. Maybe even put it in the clipboard.
Unfortunately, the internet is not helping me much with this task.
Help would be appreciated!
I'm fairly new to java, but I'd be willing to try to do it myself if you point me in the right direction, naturally.
Thanks.

Kyosys wrote:
My version got a tidbit more complicated, but that's besides the point. What I want to do now is mark a certain selection of the image, and move it around, place it somewhere else. Maybe even put it in the clipboard.
Unfortunately, the internet is not helping me much with this task.
Help would be appreciated!This should help: [http://java.sun.com/docs/books/tutorial/2d/index.html]
I'm fairly new to java, but I'd be willing to try to do it myself if you point me in the right direction, naturally.
Thanks.It looks like you're starting to run before being able to crawl. I'm not sure what "fairly new to Java" means in your book, but in mine, it means that you don't master the basics of Java. I recommend starting with non-GUI stuff before delving into the deep!
[http://java.sun.com/docs/books/tutorial/]
Anyway, best of luck!

Similar Messages

  • InDesign CC (CS7) Scrolling and moving around so slow and choppy!!

    Adobe - please send out an update for CC!! Me and IT dept checked my Mac system requirements and all is fine there. Moving around on my pages (e.g. hand tool) and scrolling is so choppy, slow, and lags. And sometimes my art disappears and I'm looking at blank pages until I stop or let refresh for a bit.
    I also noticed that when I'm in "preview mode," CPUs go down and much less choppy and smoother moving around, but I can't work in preview mode. (e.g. I need hidden characters on for Indexing markers, and my frames and guides on).
    CS6 works MUCH smoother, but I can't save client files down if that's how they provide them to me. Please recommend any other suggestions or hopefully an update will help this issue.

    I also hop on the issue to give it some more attention.
    InDesign scolling lags badly, Tools are slow, Program crashes. Equal problems using Illustrator/Photoshop.
    My Mac is not the newest – but CS6 was running like a charm before.
    Some TechSpecs:
    OSX Mavericks (Clean Install)
    Core2Duo 2.4 GhZ
    8GB 1067MhZ DDR3 RAM
    OCZ Vertex SSD 256GB
    Currently using the Test-Period of CC on this machine to figure out how it works – but that should not be a problem as the Programs are the same. As a student I would prefer the current discount offer (the normal rate is al little too much for me right now). Please fix this problem, otherwise I can't buy it the next year. Btw. Subscription method is great compared to the old payment method.

  • It sounds like something is loose and Moving around in side my Mac book. Like a screw rolling around. What should I do?

    It sounds like something has come loose and is moving around inside my Mac book what should I do?

    Under warranty? Take it to the Bar for assessment.
    No warranty? Get an appropriate screwdriver and pop the bottom lid to take a peek. Select the appropriate howto here: http://www.ifixit.com/Device/MacBook_Pro

  • When replying to a discussion, the images and text are being cutted and moved over

    Hi, using a community site in SP2013, when you post a discussion (ask and discuss) you have all the text and images displayed correctly. But, when you reply to the post and then, your name is added to the post, the image and text are somehow cutted. I played
    with the options like lock ratio and everything, but it doesn't seem to work, I also played with the webpart options, but, it is not working, the idea is to feed the text and images into the whole respond to the post. Have you seen this issue or behavior before?.
    See below,
    When you reply to the post,
    Can you see how the image is moved over.

    Hi Javi,
    I checked the discussions list in my environment, and the image showed as below when replying to a discussion:
    It seems that the page has been customized.
    I recommend to check the things below:
    Check if the page with issue has been customized.
    Add the site to trusted sites and add to compatibility settings in Internet Explorer.
    Use another browser to access the page to see if the issue still occurs.
    Best regards,
    Victoria
    TechNet Community Support
    Please remember to mark the replies as answers if they help, and unmark the answers if they provide no help. If you have feedback for TechNet Support, contact
    [email protected]

  • Work-around for lack of cut and paste for files in finder

    I am new to Mac and have observed some past but closed discussions about the annoying lack of cut and paste in Finder. I too find it very time-consuming to move a file into the folder where I want it to live - especially from downloads. Using a MacBook Pro 13", my open windows are naturally on top of each other and using drag and drop with a touch-pad for file organisation is very cumbersome. Four finger behaviour on touch pad only helps so far.
    The approach I adopt is to use cmd c to copy the files or folders in question. cmd ` to flick between finder windows (usually more than one is open) and cmd v to place the files or folders where I want them after navigating through to the destination folder. Shift cmd N gives me a new folder if required. cmd ` takes me back to source folder (usually downloads) so I can cmd delete the files and keep things tidy. This is lengthy to write down. It is however reasonably quick if your fingers know the keyboard shortcuts.
    Is there a more efficient way for me to work around this inhibitory feature of Finder? "Save as" is only good if you know you have a folder in your file structure already created and named and suitable to receive the file you want to file away (usually an attachment to an email).

    This is in reply to your last paragraph...
    This is the Mac OS default Save As... dialog:
    Notice that there is a downward pointing disclosure triangle to the right of the Save As: field. If you click on the disclosure triangle, a full Finder window will be exposed, and this will enable you to place downloaded files wherever you like.
    Notice, also, that in the bottom left hand corner of the exposed finder window, you are able to create a new folder (in any location on your system) to which you are able to download files.
    This works the same way in any Save As situation, using any app, and the app you are in will remember where you last chose to save a file so that repetitive navigation in these dialog boxes is somewhat reduced.

  • IPad mini keeps clicking when open and green bars highlight and keep moving around the screen

    MY iPad mini when open keeps clicking and a green bar keeps moving around the screen highlighting things, how do I stop it?

    Go to Settings > General > Accessibility > Switch Control and turn this OFF.

  • Ipad mini screen keeps moving around and switching screens as though someone else is using it? any ideas what is going on?

    Ipad mini screen keeps moving around as though someone else is fiddling with it while I am using it. Any ideas what is goi g on?

    Hi Deeveeay,
    Try resetting the device (nothing will be lost): Hold down the Home and Power buttons at the same time and continue to hold them down until the Apple appears (up to 30 seconds). When the Home screen redisplays, see if the issue has resolved itself.
    Cheers,
    GB

  • I created an album from photos taken off three different cameras. I am sorting the photos manually and when I close iPhoto, the photos move back either by date or by camera import time. How can I keep the photos from moving around?

    I created an album from photos taken off three different cameras. I am sorting the photos manually and when I close iPhoto, the photos move back either by date or by camera import time. How can I keep the photos from moving around?

    Don't tell us, tell them
    http://www.apple.com/feedback/macosx.html is the place for feature requests and feedback

  • When I connect my canon Hd vixia to upload media. My windows start moving around franticly and I hear this ticking sound. What am I doing wrong?

    Loging & Transfering
    When I connect my canon Hd vixia to upload media. My windows start moving around franticly and I hear this ticking sound. What am I doing wrong?

    Hard to tell from here. This would be a first.
    How is the camera connected and is it or anything else touching your keyboard?
    x

  • How do I cut and paste?  I want to change the background around a Christmas tree

    How do I cut and paste in Elements 10.  I want to change the background around a Christmas tree.

    One way would be to open the background image, then go to File<Place and select your christmas tree image.
    Now you have two layers.
    Make a selection of the christmas tree with the Quick Selection Tool and then add a layer mask.
    Now you can see the background around the tree.
    Of course your selection may still need some tweaking, so you could paint on the layer mask to further refine the selection.
    What version of photoshop elements?
    example before: (didn't have a chritmas tree image handy)
    after:

  • My daughters Macbook's (3 years old) computer screen sometimes will waver and freeze and then turn white if not moved around. She was told that the replacement part would cost $600.  Do you know what's going on, and what is the replacement part?

    Mydaughters Macbook's (3 years old) computer screen sometimes will waver andfreeze and then turn white if not moved around. She was told that thereplacement part would cost $600.  Do youknow what's going on, and what is the replacement part? 

    Who told her that the replacement piece was $600?
    Ask those people which piece it is.

  • My macbook Pro 13" 2013 has a mouse problem, its not moving naturally, and keeps on moving around like its dancing. Please help solve this!!

    My macbook Pro 13" 2013 has a mouse problem, its not moving naturally, and keeps on moving around like its dancing. Please help solve this!!

    Go step by step and test.
    1. Restart
    2. Shut down the computer.
        Clean the trackpad with moist not wet microfiber cloth.
        System Preferences > Point & Click
        Try turning off three finger dragging and then turning it on after  testing.
    3. Is there any Bluetooth device nearby with failing batteries? If so, replace the batteries.
      4. Reset PRAM:   http://support.apple.com/kb/PH14222
    5. Reset SMC.     http://support.apple.com/kb/HT3964
        Choose the method for:
        "Resetting SMC on portables with a battery you should not remove on your own".
    6. Close all windows and quit all applications.
        Click the Spotlight -the magnifying glass icon- in the menu bar. Enter Disk utility in the box.
        Select Disk Utility from the drop down. When the Disk utility window opens up,
        select  Macintosh HD, then First Aid.
        Click Repair Disk Permissions button.
        Ignore the  time remaining estimate.
        Last 1 minute may take longer.

  • Making an arrow that can be moved around and switch directions

    how do you create an arrow that can be moved and pionted in different directions

    Use transform to rotate and move around.  However, it's often faster just to draw a new arrow.

  • I have opened a page in WORD and cut and tried to paste a photograph to it. The result is that the page is now frozen with the coloured circle{ cursor} spinning around and not letting me do anything with the page. I only wish to close it down. Thank you

    Have opened a page in WORD and tried to cut and paste to it.  The result is that the page is frozen with the  coloured cursor just spinning around and not letting me do anything with the page. I only wish to close the page down. Thanking you

    Many thanks for your help in this instance. The command-option-shift-esc and force quit solved the problem for me . Now I can get back to using my computer once more

  • Flash moving my symbols and layers around the stage

    Every now and then if I close and reopen flash, it will randomly move different layers and symbols around so they're a discombobulated mess.  My animation isn't smooth anymore and I have to try to piece it back together, but it never looks as nice as it did when I first drew it.  It seems to pick things at random.  Here is an example: http://dl.dropbox.com/u/54053846/fennec%20running7.swf
    Sometimes I'll start drawing and it'll brush stroke like, 100 px to the left of where I'm trying to draw, too.
    What am I doing wrong?  I noticed that it often happens right after I change the stage size to match the contents (sometimes I draw off the stage and instead of making it a symbol to fit within the stage, I just change the stage size).

    Hi nupurfromcali,
    Welcome to the Support Communities!
    The articles below may be able to help you with this.  Click on the links to see more details and screenshots. 
    iPod only shows an Apple logo and doesn't start up
    http://support.apple.com/kb/TS1399
    Restoring iPod to factory settings
    http://support.apple.com/kb/ht1339
    Cheers,
    - Judy

Maybe you are looking for

  • Error in Starting Oracle BAM Active Data Cache

    I am not able to start "Oracle BAM Active Data Cache" on my machine. The other two components "Oracle BAM Event Engine" and "Oracle BAM Report Cache" are starting properly. When I see the event log file of my Computer I could see the details as below

  • User exit/BADI for MIGO Batch numbers

    Hi All, I want to find a user exit which will give the oldest batch number (MCHA-LWDET) of semi-finished product during the goods receipts of the finished prodocut in MIGO. Could you please give me any hints to find out the user exit for this. Regard

  • Send error messages in DatabaseMail when SQL Job fails/succeeds

    Hi , I have setup a profile and account for sending mail using DatabaseMail. I have a SQL JOB. If the job fails, I receive an Email. Till this point, everything works fine. This is what the email looks like : JOB RUN: 'GenerateJVForLabourAndOverheads

  • No std Inf.& warning messages while CAT2 being called from custom Zprogram

    Hi, We have built a custom z program that will be called CAT2 transaction using batch data processing(BDC). CAT2 being called  by CALL TRANSACTION ' USING bdcdata  MODE 'N'. The idea was that facilitate users  to choose multiple employees at a time a

  • Newbie needs colour replacement advice

    I'm still feeling my way along with PS and would appreciate some help with what is probably a very straightforward issue: I'm planning to do some giclee prints involving scans of original watercolour paintings of botanical subjects. Because of the na