Drawing outside of overridden paint

How can I draw outside a paint in awt ?
I have a method and I don't know how to get the Graphics object to draw on the
Frame, Panel etc.

unix.bsd wrote:
Still, same question, using paintComponent on a panel for example when I click the panel
lets say I have a mouseListener I want to draw something I must call the paintComponent?It would be a JPanel, and no, you wouldn't call paintComponent as you almost never call paint or paintComponent directly. Have you had a chance to go through the 2D graphics tutorial on the Sun site? If not, you should as it will likely answer most of your questions.
It is bizarre for me, why can't I have drawing logic and actions outside of a paint ?Because doing Java graphics requires a different way of thinking. It was bizarre to me and I think most of us when we started to learn it, but with study and practice, it will become more second nature. Again I highly recommend the tutorials.

Similar Messages

  • Drawing outside the java applet ?

    Hi all !
    How do you draw "outside" the space taken by the applet on the web page ?
    For example, say I've got a menu bar where the applet size is 100 width by 20 height on the page, and when the user clicks on one of the buttons that I've drawn within the menu, I want to be able to draw menu buttons "below" the applet, over the web page (pop up menu style)
    Cheers for a pointer in the right direction :)

    Ooookay ..... then how come I've got an demo of
    someone's applet here that does exactly this, i.e.
    draws outside of it's original size ?
    Show us the demo.
    How might they do it ? (dont really want to decompile
    someone else's class file, coz that's rude :-)Maybe someone will do it for you; the impossible has always attracted the human mind.

  • Drawing outside the GUI

    Is there any way that I can draw outside the GUI? Something like a marker that draws anywhere on the screen, or a rectangle that you can drag around to take have a reference when taking a portion of a screen shot.

    Hojima wrote:
    Is there any way that I can draw outside the GUI? Something like a marker that draws anywhere on the screen, or a rectangle that you can drag around to take have a reference when taking a portion of a screen shot.That's not something you can do easily in Java, without going native I suspect.
    As an approximation, would you consider having Java take a screen shot, then allowing you to draw on that?

  • [svn:osmf:] 10683: Bug fix for SWFElement drawing outside of its bounds causing layout havok.

    Revision: 10683
    Author:   [email protected]
    Date:     2009-09-29 09:29:55 -0700 (Tue, 29 Sep 2009)
    Log Message:
    Bug fix for SWFElement drawing outside of its bounds causing layout havok.   FM-87 and FM-77
    Ticket Links:
        http://bugs.adobe.com/jira/browse/FM-87
        http://bugs.adobe.com/jira/browse/FM-77
    Modified Paths:
        osmf/trunk/framework/MediaFramework/org/openvideoplayer/content/ContentElement.as

    FYI - This regression has been filed here: http://bugs.adobe.com/jira/browse/SDK-31989

  • Scrolling a custom Component (e.g. JPanel) with overridden paint(Graphic g)

    Hi.
    I’m creating an application for modelling advanced electrical systems in a house. Until now I have focused on the custom canvas using Java2D to draw my model. I can move the model components around, draw lines between them, and so on.
    But now I want to implement a JScrollPane to be able to scroll a large model. I’m currently using a custom JPanel with a complete override of the paint(Graphic g) method.
    Screen-shot of what I want to scroll:
    http://pchome.grm.hia.no/~aalbre99/ScreenShot.png
    Just adding my custom JPanel to a JScrollPane will obviously not work, since the paint(Graphic g) method for the JPanel would not be used any more, since the JScrollPane now has to analyze which components inside the container (JPanel) to paint.
    So my question is therefore: How do you scroll a custom Component (e.g. JPanel) where the paint(Graphic g) method is totally overridden.
    I believe the I have to paint on a JViewport instructing the JScrollPane my self, but how? Or is there another solution to the problem?
    Thanks in advance for any suggestions.
    Aleksander.

    I�m currently using a custom JPanel with a complete override of the paint(Graphic g) method. Althought this isn't your problem, you should be overriding the paintComponent(..) method, not the paint(..) method.
    But now I want to implement a JScrollPane to be able to scroll a large model.When you create a custom component to do custom painting then you are responsible for determining the preferredSize of the component. So, you need to override the getPreferredSize(...) method to return the preferredSize of your component. Then scrolling will happen automatically when the component is added to a scrollPane.

  • Tablet draws outside of cursor circle...

    You know the little circle that acts as the 'paintbrush tip'? For some reason, every time I draw, I'm not getting accuracy from within that circle, but it's like the paintbrush tip is on the top-left OUTSIDE side of the circle.
    How do I fix thiiiis?
    Using Photoshop CS6 Extended. Genius tablet.
    Also, does anyone know how to edit it so that you don't have to select a specific brush for sensitivity? ALso, is there any way to make the brush sharper and cleaner? It just looks blurry.

    Oh. Ugh. Well, Photoshop CS6 is just a bit too involved for me. I figured out that it was because of the brush I was on (So, I guess essentially a brush preset issue). I guess it was a caligraphic brush? So it was writing as if the pen was held at an angle.
    I just selected a different brush to test that (I thought the caligraphic brush tip was the only one that had pressure sensitivity on it, but just found that it isn't), and it works fine.
    Thanks for your time.

  • Draw Image out of paint(graphics) method

    Does anybody know who can I draw an image out of the paint method of the component??
    thx

    Create a BufferedImage, get it's graphics and draw on this image as much as you want.
    BufferedImage bI = new BufferedImage(200,200,BufferedImage.TYPE_3BYTE_BGR);
    Graphics      g  = bI.getGraphics();
    Graphics2D    g2 = bI.createGraphics();
    g.setColor(Color.red);
    g.drawLine(10,10,190,190);
    g2.setColor(Color.blue);
    g2.draw(new Rectangle(50,50,100,100));

  • Hint : Corel Draw and Corel Photo Paint 8 LE legit &  free download

    I found this while searching for good software for OS9 :
    http://main.system7today.com/software/corelgraphics8/corelgraphics8.html
    Basically its the full Corel Suite ( has some Potoshop capabilities ) for free.
    The only thing is you can only have open one document at a time , but no restrictions in save/edit or whatever. Also a great Help System how to learn graphics manipulation.
    This runs extremely well under OS 9.22 with enough Ram. ( needs PowerMac CPU)
    On the site is even more goodies to find. Also note that Corel has made their WordProcessor "WordPerfect" free for Mac OS. It's as good as MS Word.

    I got it working myself
    Maybe others are dealing with the same problem, so here is what I did:
    At Corel's support website I found no information about similar problems, just a description how to set back the current workspace to factory defaults.
    This is done by clicking at the program icon to start the software and then holding down the shift key immediately after clicking. Then a message box opens asking for confirmation.
    Although I had done some custom settings to the workspace I had nothing to lose and gave it a try. Voila
    Not sure what the reason behind is; I think that there are old configuration files existing or settings got mixed up for whatever reason.
    Before I did so, I renamed the matching *.plist file at my user's library/preferences folder to *.plist.old - but this did not bring any improvement (in fact, even no new plist file became created).

  • Some questions on drawing

    hi all, i want to draw out a different shape depending on what key the user pressed, but how do i draw outside of the paint() method? for example, when the user presses 1 on the handphone(emulator), i draw out a rectangle. i tried to check for the key been pressed in paint(), but its like paint() is only been called when the MIDlet starts up, and i can only press a key after everything has been drawn, so nothing gets drawn out.
    public void paint(Graphics g)
    public void drawrect()
    public void drawsquare()
    public void keyPressed(int keyCode)
       //if 1 is pressed..........
       drawrect();
       //if 2 is pressed..........
       drawsquare();
    }

    stupid me i placed the repaint() in the wrong place haha got it working now

  • Runtime error accessing ArrayList

    These 2 classes are basically the classes from the book developing games in java, without the fullscreen stuff. They are supposed to create an animation. They both compile fine but when I run I get an error message, an no animation, paint() is only called once. Here's the error message:
    Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 4, Size: 4
    at java.util.ArrayList.RangeCheck(ArrayList.java:546)
    at java.util.ArrayList.get(ArrayList.java:321)
    at Animation.getFrame(Animation.java:60)
    at Animation.update(Animation.java:43)
    at AnimationTest1.animationLoop(AnimationTest1.java:58)
    at AnimationTest1.run(AnimationTest1.java:44)
    at AnimationTest1.main(AnimationTest1.java:10)
    I think this means the program is trying to access frame 4 when there is none. Right? I just can't get my brain around this. Any help would be great!
    import java.awt.Image;
    import java.util.ArrayList;
    /*     this class manages a serie of images (frames) and the amount of time to
         display each frame */
    public class Animation {
         private ArrayList frames;
         private int currFrameIndex;
         private long animTime;
         private long totalDuration;
         //creates a new, empty animation, calls start().
         public Animation() {
              frames = new ArrayList();
              totalDuration = 0;
              start();
         //adds an image to the animation, + how long to display the image.
         public synchronized void addFrame(Image image, long duration) {
              totalDuration += duration;
              frames.add(new AnimFrame(image, totalDuration));
         //starts this animation over from the beginning .
         public synchronized void start() {
              animTime = 0;
              currFrameIndex = 0;
         //updates this animations current frame if necessary
         public synchronized void update(long elapsedTime) {
              if (frames.size() >1) {
                   animTime += elapsedTime;
                   if (animTime >= totalDuration) {
                        animTime = animTime % totalDuration;
                        currFrameIndex = 0;
                   while (animTime > getFrame(currFrameIndex).endTime) {
                        currFrameIndex ++;
         //gets the animations current frame. null if none availible.
         public synchronized Image getImage() {
              if (frames.size() == 0) {
                   return null;
              else {
                   return getFrame(currFrameIndex).image;
         private AnimFrame getFrame(int i) {
              return (AnimFrame)frames.get(i);
         private class AnimFrame {
              Image image;
              long endTime;
              public AnimFrame(Image image, long endtime) {
                   this.image = image;
                   this.endTime = endTime;
    import java.awt.*;
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    public class AnimationTest1 {
         public static void main (String args[]) {          
              AnimationTest1 test = new AnimationTest1();
              test.run();
         private static final long DEMO_TIME = 5000;
         private JFrame frame;
         private Animation anim;
         public void loadImages() {
              //load images
              Image player1 = loadImage("gif1.png");
              Image player2 = loadImage("gif2.png");
              Image player3 = loadImage("gif3.png");
              //create animation
              anim = new Animation();
              anim.addFrame(player1, 200);
              anim.addFrame(player2, 200);
              anim.addFrame(player3, 200);
              anim.addFrame(player2, 200);
         //loads image with ImageIcon
         private Image loadImage(String filename) {
              return new ImageIcon(filename).getImage();
         public void run() {
              try {
                   frame = new JFrame();
                   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                   frame.setSize(300, 300);
                   frame.setVisible(true);
                   loadImages();
                   animationLoop();
              finally {
         public void animationLoop() {
              long startTime = System.currentTimeMillis();
              long currTime = startTime;
              while (currTime - startTime < DEMO_TIME) {
                   long elapsedTime = System.currentTimeMillis() - currTime;
                   currTime += elapsedTime;
                   anim.update(elapsedTime);
                   //getContentpane.getgraphics??!!
                   Graphics g = frame.getGraphics();
                   draw(g);
                   g.dispose();
                   try {
                        Thread.sleep(20);
                   catch (InterruptedException ex) {}
         //draw the animation
         public void draw(Graphics g) {
              g.setColor(Color.white);
              g.fillRect(0, 0, 300, 300);          
              g.drawImage(anim.getImage(), 0, 0, null);
    }

    106498II wrote:
    i changed it to read:
    while (animTime > getFrame(currFrameIndex).endTime) {
                        currFrameIndex ++;
                        if (currFrameIndex >3) {
                             currFrameIndex = 0;
                        }this gets rid of the error but still no animation. Perhaps its something to do with that I draw directly on the JFrame?On the JFrame? Better to draw in a JPanel or JComponent. Also, perhaps you're trying to animate on the EDT in which case you get the beginning picture, and the end picture but usually you get nothing in between. Are you using a non-EDT thread or a Swing Timer?
    Addendum: yeah it looks like you're trying to put the EDT (event dispatch thread -- the main thread that runs Swing) to sleep here. Please don't do this. Also you're trying to draw outside of a paint or paintComponent method.
    Solution: Run don't walk to the Sun java2D and Swing graphics tutorials and read and study them.
    Edited by: petes1234 on Dec 11, 2007 7:21 PM

  • Appleworks 6 draw and paint crash/restart typing in text tool, save crash

    This has happened 15 times in the past week,
    1st in draw when typing using text tool many times, so I got into saving, then same last night in paint, i was trying to save because familiar with problem crash and the automatic restart, this happened when trying to save file and typing file name in popup window, I lost quite a bit of work, 1hr., there is not much point in trying to use this app. if this cannot be fixed.
    Also, many of the tools are grayed out and not usable, and pallets specifically line weights arrows not available. No color pallet for line, no way to draw straight line in paint mode, much more that is difficult or impossible, can't figure out easy way to make new page the size I want other than by adjusting pixel numbers.
    May be or not related, when I try to open a doc file from outside, I get a popup telling me I have to buy apple software.
    This macbook is new as of Jan. 2010, the box said ilife etc., so is this true, I would expect what came with the macbook to just work, no hassle.
    Should I try gimp?, someone told me it is as good as photoshop even easier.
    I have paintbrush, tux, etc,
    None of these apps. works as well as the old macpaint and macdraw from way back in the old black and white days. All very discouraging. This stuff should work as well as those apps. did. Oh well, I rather like the old pencil and paper, but I was trying to do a brochure.
    Photos, typesetting, drawing, cut paste etc.
    Isn't this what appleworks is for?
    anybody out there have any suggestions.
    I read 'strange behavior appleworks' and peggy's re, I don't find any appleworks in my preference folder as suggested.
    Is it possible other keystroke commands like in speech app. could cause this restart?
    Thank you anyone out there who reads this and might know an easy fix.
    primative

    That the problem occurs when typing, this user tip might help.

  • Changeing Paint's Graphics outside paint

    I have this code:
    public void Paint (Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    g2.setTransform (t);
    how can I access g2 from outside Paint?

    You should not access g2d outside of paint. Let me explain. First of all, references to Graphics that are passed to methods, such as paint() and update(), are only valid during the execution of the method that they are passed to. That is, they are a copy of the original Graphics object. Once the method returns, the reference is no longer valid because its caller is responsible for disposing of it. Each Graphics represents a graphics context supplied by the native windowing system and there are usually only a limited number of them. Is this making sense. The important thing to remember is that the paint() method only has a copy of the Graphics object and that it is usually a bad idea to store it in an instance variable. The way that you get a reference to the Graphics of an image or a component (when you are outside of the paint() method) is to call the image.getGraphics(), component.getGraphics() or any of the Graphics.create() methods. Each returns a copy of the original Graphics object and you will be responsible for disposing of this copy when you are done using it. A very good example of this can be found when Double Buffering. Double Buffering is where you clear and draw an image off screen first, and then "blit" it straight onto your component. Here is an excerpt of code that is not from a paint() method://use double buffering to avoid flicker (paint image off screen then blit to screen)
    if (offScreenImage == null) offScreenImage = createImage(getWidth(),getHeight());
    Graphics offScreenGraphics = offScreoffScreenImage.getGraphics();
    Graphics g = getGraphics();
    //get graphics can return null if invoked before component's peer is created
    if (offScreenGraphics!=null && g!=null) {
       try {
          //clear the offscreen image
          offScreenGraphics.setColor(getBackground());
          offScreenGraphics.fillRect(0,0,getWidth(),getHeight());
          //blit to screen
          offScreenGraphics.drawImage(image,0,0,this);
          g.drawImage(offScreenImage,0,0,this);
       } finally {
          //must dispose of all graphics
          offScreenGraphics.dispose();
          g.dispose();
       }//end try
    }//end ifIt demonstrates the getting of the Graphics object when your current method it is in wasn't passed a reference to the Graphics object. In the code above, image and offScreenImage are instance variables. If you still want to use the Java2D version of the Graphics object, you will have to cast it once you get a reference to it. I hope this helps.
    Also, about repaint(). When you want to do application-triggered painting, you will want to call the repaint() method and should never call the paint() method directly. repaint() calls update(), update() clears the screen on heavyweight components, and finally paint() is called. For a very good article involving painting into components please read the following (it is a must read):
    http://java.sun.com/products/jfc/tsc/articles/painting/index.html
    tajenkins

  • Why does my drawing gets cleared sometime after paint() ?

    I ' m using a JNI dll for a special video player with JDK 1.6.13 on Windows XP. For painting the video frames I followed the AWT_Native_Native Interface spec on
    [http://java.sun.com/j2se/1.5.0/docs/guide/awt/1.3/AWT_Native_Interface.html]
    For the drawing I use SetDIBitsToDevice() from MS DirectDraw. For testing I tried also TextOut(). Both functions with their painting work, but some time after the paint() call my canvas frame gets completely cleared. Sometimes my painting stays on the screen - but only until the next update event. From the debugger it looks like the window gets cleared after the thread from the paint() method has finished.
    Interestingly if I call Java draw functions in the paint() method (without JNI), the drawing stays permanently.
    Are there any changes in the JVM 1.6 which are not included in the AWT_Native_Interface Spec?
    Thanks for any help or working example code,
    Stefan

    The real solution is to call:
          Toolkit.getDefaultToolkit().sync();in the overridden paint() method before the JNI drawing call.

  • Appleworks Drawing & Painting: Color palettes *still* getting scrambled

    I have a problem when using Appleworks drawing files with painting frames.
    An Appleworks painting frame normally has two color palettes. One comes from the Drawing document, and this determines the background color of the painting frame. Anywhere that has no other color has this color. The other comes from the painting frame itself.
    When clicking into the painting frame, both of these can get scrambled, with unpredictable results. In some cases, although both the drawing document and the painting frame used the same [presentation] palette, and the background color and another color were supposed to be the same, one could switch to another color. I started resetting the background color of each painting frame to white [in the drawing document], and filling it with whatever color I needed [in the painting frame] which solved one of the problems.
    However, I'm still facing the annoying problem that the color palette gets scrambled. When I click into the Painting Frame, the color palette scrambles. If I start painting, it gets screwed up. If I switch the palette from presentation, to something else, back to presentation, that fixes it for the moment. If I click out of the painting frame, and then back into it, or click from one painting frame into another painting frame, the color palette scrambles again. I have to switch the palette twice each time I enter the frame.
    Is there any way to correct this problem?

    The earliest version of AppleWorks stable in OS X is 6.2.4. That you're using 6.2.2 could be a big part of the problem. Since AppleWorks was designated "end-of-life" it is difficult to find the updaters (especially the US/North American) on Apple's site. Here is the page with the links I have for the 6.2.9 updater. The page has a link to the North American updater in the upper right (the blue 15.8 MB) & links to other languages further down.
    AppleWorks 6.2.9 for Mac
    Whenever you do an update, it's a good idea to do two maintenance tasks, one for the system (Repair Permissions) and one for AppleWorks (delete preferences). (Thanks to Barry for this way to format this very common answer.)
    To Repair permissions, launch Disk Utility, found in the Utilities folder in your Applications folder, click on First Aid, then on Repair Permissions.
    To delete AppleWorks’ preference files, go to HD > Users > (your account) > Library > Preferences. Find and delete the file com.apple.appleworks.plist. Find and open the folder AppleWorks in this Preferences folder, then delete all of the enclosed files (with the exception of the Button Bar Preferences if you have customized the Button Bars). AppleWorks will recreate the preference files as it needs them.

  • Appleworks Drawing & Painting: Color palettes getting scrambled

    I'm currently putting together a drawing document using multiple painting frames. I'm happy to use one color palette - the presentation palette - throughout, but Appleworks keeps scrambling the colors.
    This happens each time I switch between the drawing document and the painting frames, or between two painting frames. The effect is to change some of the colors in the palette, and in the document too. At times a completely different palette appears, under the same presentation heading. Two places which are supposed to have the same color, and the same cell in the palette, get different colors, using the same cell of the palette.

    The earliest version of AppleWorks stable in OS X is 6.2.4. That you're using 6.2.2 could be a big part of the problem. Since AppleWorks was designated "end-of-life" it is difficult to find the updaters (especially the US/North American) on Apple's site. Here is the page with the links I have for the 6.2.9 updater. The page has a link to the North American updater in the upper right (the blue 15.8 MB) & links to other languages further down.
    AppleWorks 6.2.9 for Mac
    Whenever you do an update, it's a good idea to do two maintenance tasks, one for the system (Repair Permissions) and one for AppleWorks (delete preferences). (Thanks to Barry for this way to format this very common answer.)
    To Repair permissions, launch Disk Utility, found in the Utilities folder in your Applications folder, click on First Aid, then on Repair Permissions.
    To delete AppleWorks’ preference files, go to HD > Users > (your account) > Library > Preferences. Find and delete the file com.apple.appleworks.plist. Find and open the folder AppleWorks in this Preferences folder, then delete all of the enclosed files (with the exception of the Button Bar Preferences if you have customized the Button Bars). AppleWorks will recreate the preference files as it needs them.

Maybe you are looking for

  • Weblogic Server 10.3.6.0.8 one of data sources failing while other 6 not

    Hello, I have 7 data sources connecting to the same database schema. After I upgraded the environment (production) day before yesterday evening I noticed one of the data sources start failing with exception below: "Connection test failed with the fol

  • Foreground color gray

    I'm trying to recolor an object by making a Hue/Saturation layer and clipping it to my object I want recolored. I select the color I want, create the new layer and every time I do that, the selected foreground color reverts to gray and I cannot recol

  • Error when installing SAP gateway in Flash Builder 4.6

    I installed Flash Builder 4.6 but I am getting an error installing the SAP add-on. it says org.maven.ide.eclipse missing. I am running this as admin.

  • Alterations in the Environment window don't do anything to the midi

    The midi signals for Yamaha DTXplorer are off from GM. The snare (G0) and bass drum (A0) output notes do not trigger snare (D1) and bass drum (C1). I hear all the other drums. The snare and bass drum do record (but not on channels that make any sound

  • Nothing seems to work, please help!! :(

    Hey guys,  I was hoping if anyone could help me, i am a first time BB user and i set everything up today! For about 1 hour everything was working, untill i got an error message saying it could not connect to the internet!  Things i have tried: Send S