Results 1 to 3 of 3
  1. #1
    ravi.joshi53 is offline Member
    Join Date
    Oct 2011
    Posts
    24
    Rep Power
    0

    Question Want to know about simultaneously drawing graphics in canvas and updating text inside

    Hi, I am developing a simple GUI application containing three Panel inside a window.
    Left JPanel contains a TextArea having some text written on it. Right JPanel (buttonPanel) contains some JButton and JTextField. Central part contains an Applet, which just have a canvas with ItemListener.
    I want to highlight any text (inside JTextArea) simultaneously with drawing simple 2D graphics in the canvas according to the user input provided from the buttonPanel.

    Should i use thread here? I am not very friendly with using threads. Please explain me in details.

    Thank you...

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Want to know about simultaneously drawing graphics in canvas and updating text in

    I'm confused -- you're using an applet inside of a GUI? Don't do this, please. Applets are top level containers and shouldn't be used in this way, and I can think of no reason why you'd need to use them this way.

    Next, you ask for explanation in detail but neglect to give us important details about exactly what you're trying to do and any code you have. Please share with us what you've got and what you know;

  3. #3
    ravi.joshi53 is offline Member
    Join Date
    Oct 2011
    Posts
    24
    Rep Power
    0

    Thumbs up Re: Want to know about simultaneously drawing graphics in canvas and updating text in

    Quote Originally Posted by Fubarable View Post
    I'm confused -- you're using an applet inside of a GUI? Don't do this, please. Applets are top level containers and shouldn't be used in this way, and I can think of no reason why you'd need to use them this way.

    Next, you ask for explanation in detail but neglect to give us important details about exactly what you're trying to do and any code you have. Please share with us what you've got and what you know;
    Ok… Actually that central part is a canvas not an applet, in which 2D simple graphics can be displayed easily (i.e. Graphics G). My application contains so many source files, that is why I have not provided any code.
    Well, left JPanel contains a JTextArea with some text on it. Right JPanel (buttonPanel) contains JButtons and JTextField with actionListener, so that user can provide input to the canvas with this JPanel. The central canvas has a mouseListener, which is sometimes require for mouse click purpose. Coming to the point, User can provide input from the buttonPanel and according to the input, some drawing is displayed using Graphics g. At the same time while displaying the drawing in the canvas, I want to highlight some portion of the text written on the JTextArea on the left JPanel. Highlighting is done by the following code:
    Java Code:
    Highlighter highLighter = new DefaultHighlighter();  
    HighlightPainter painter = new DefaultHighlighter.DefaultHighlightPainter(Color.red);
    public void highLightLine(int lineNumber){
    /*
    *There is a parser, which can calculate the start and end index for a line.
    */
    highLighter.removeAllHighlights();
    try{
    highLighter.addHighlight(startIndex, endIndex, painter);
    Thread.sleep(500L);
    }catch (InterruptedException ex) {}
    }
    For example, in buttonPanel there are two JButton named UP and DOWN with a JTextField for entering the integer value. Whenever user clicks on the UP button with some value in JTextField, canvas have to draw an arrow going to upward direction with the specified unit in the JTextField and at the same time JTextArea should highlight text “UP” written on the JTextArea situating at left JPanel.
    As my experience says, for highlighting the text, I called highLightLine(int lineNumber) method from the canvas in the following manner.
    Java Code:
    public void paint(Graphics g) {
    myHighlighter.highLightLine(2);
    g.drawString("My STRING", i, j);
    myHighlighter.highLightLine(5);
    g.drawLine(x1, y1, x2, y2);
    }
    As seems by the code, it should first highlight the line number 2 of the text then it should draw string and after that highlight line number 5 then draw line. But it is not doing the same. Every time drawing task is done first then the highlight task, that is why i can only see the effect of last highLightLine() method, which i don't want. I want to highlight the line simultaneously with the drawing task. That is why I am thinking for implementing threads here but don't know how.

    Sorry for such a big description as i can't attach the whole source code here.

    Thank you.

Similar Threads

  1. Replies: 1
    Last Post: 10-26-2011, 07:03 PM
  2. newbie drawing two circles simultaneously
    By nadeemshafi9 in forum Threads and Synchronization
    Replies: 7
    Last Post: 01-09-2011, 01:53 PM
  3. Little help with Drawing canvas needed
    By Gfunk in forum New To Java
    Replies: 4
    Last Post: 03-30-2010, 11:15 AM
  4. Issues with updating a canvas in Java
    By Sandworm in forum Advanced Java
    Replies: 1
    Last Post: 11-19-2009, 08:58 AM
  5. Replies: 0
    Last Post: 07-05-2009, 12:31 PM

Posting Permissions

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