Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8477835
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:34:36+00:00 2026-06-10T18:34:36+00:00

plese look at my code snippets , wha is wrong with it , it

  • 0

plese look at my code snippets , wha is wrong with it , it frrezes GUI when the Swing timer stats which is repeteadly paints on the jpnael ??

class WaveformPanel extends JPanel {

        Timer graphTimer = null;
        AudioInfo helper = null;

        WaveformPanel() {
            setPreferredSize(new Dimension(200, 80));
            setBorder(BorderFactory.createLineBorder(Color.BLACK));
            graphTimer = new Timer(15, new TimerDrawing());
        }

        /**
         * 
         */
        private static final long serialVersionUID = 969991141812736791L;
        protected final Color BACKGROUND_COLOR = Color.white;
        protected final Color REFERENCE_LINE_COLOR = Color.black;
        protected final Color WAVEFORM_COLOR = Color.red;

        protected void paintComponent(Graphics g) {

            super.paintComponent(g);

            int lineHeight = getHeight() / 2;
            g.setColor(REFERENCE_LINE_COLOR);
            g.drawLine(0, lineHeight, (int) getWidth(), lineHeight);

            if (helper == null) {
                return;
            }

            drawWaveform(g, helper.getAudio(0));

        }

        protected void drawWaveform(Graphics g, int[] samples) {

            if (samples == null) {
                return;
            }

            int oldX = 0;
            int oldY = (int) (getHeight() / 2);
            int xIndex = 0;

            int increment = helper.getIncrement(helper
                    .getXScaleFactor(getWidth()));
            g.setColor(WAVEFORM_COLOR);

            int t = 0;

            for (t = 0; t < increment; t += increment) {
                g.drawLine(oldX, oldY, xIndex, oldY);
                xIndex++;
                oldX = xIndex;
            }

            for (; t < samples.length; t += increment) {
                double scaleFactor = helper.getYScaleFactor(getHeight());
                double scaledSample = samples[t] * scaleFactor;
                int y = (int) ((getHeight() / 2) - (scaledSample));
                g.drawLine(oldX, oldY, xIndex, y);

                xIndex++;
                oldX = xIndex;
                oldY = y;
            }
        }

        public void setAnimation(boolean turnon) {
            if (turnon) {
                graphTimer.start();
            } else {
                graphTimer.stop();
            }
        }

        class TimerDrawing implements ActionListener {

            @Override
            public void actionPerformed(ActionEvent e) {

                byte[] bytes = captureThread.getTempBuffer();

                if (helper != null) {
                    helper.setBytes(bytes);
                } else {
                    helper = new AudioInfo(bytes);
                }
                repaint();
            }
        }

    }

I am calling setAnimation of WaveFormPanel from its parent class.when animation starts it does not draw anything but freezes. please , give me solution.

Thank You
Mihir Parekh

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T18:34:38+00:00Added an answer on June 10, 2026 at 6:34 pm

    The java.swingx.Timer calls the ActionPerformed within the EDT. The question then is, what’s taking the time to render. It could be the call to captureThread.getTempBuffer it could be the construction of the help, but I suspect it’s just the sheer amount of data you are trying to paint.

    Having played with this recently, it takes quite a bit of time to process the waveform.

    One suggestion might be to reduce the number of samples that you paint. Rather then painting each one, maybe paint every second or forth sample point depending on the width of the component. You should still get the same jist but without all the work…

    UPDATED

    All samples, 2.18 seconds

    AllSamples

    Every 4th sample, 0.711 seconds

    enter image description here

    Every 8th sample, 0.450 seconds

    enter image description here

    Rather then paint in response to the timer, maybe you need to paint in response to batches of data.

    As your loader thread has a "chunk" of data, may be paint it then.

    As HoverCraftFullOfEels suggested, you could paint this to a BufferedImage first and then paint that to the screen…

    SwingWorker might be able to achieve this for you

    UPDATED

    This is the code I use to paint the above samples.

    // Samples is a 2D int array (int[][]), where the first index is the channel, the second is the sample for that channel
    if (samples != null) {
    
        Graphics2D g2d = (Graphics2D) g;
    
        int length = samples[0].length;
    
        int width = getWidth() - 1;
        int height = getHeight() - 1;
    
        int oldX = 0;
        int oldY = height / 2;
        int frame = 0;
    
        // min, max is the min/max range of the samples, ie the highest and lowest samples
        int range = max + (min * -2);
        float scale = (float) height / (float) range;
    
        int minY = Math.round(((height / 2) + (min * scale)));
        int maxY = Math.round(((height / 2) + (max * scale)));
    
        LinearGradientPaint lgp = new LinearGradientPaint(
                new Point2D.Float(0, minY),
                new Point2D.Float(0, maxY),
                new float[]{0f, 0.5f, 1f},
                new Color[]{Color.BLUE, Color.RED, Color.BLUE});
        g2d.setPaint(lgp);
        for (int sample : samples[0]) {
    
            if (sample % 64 == 0) {
    
                int x = Math.round(((float) frame / (float) length) * width);
                int y = Math.round((height / 2) + (sample * scale));
    
                g2d.drawLine(oldX, oldY, x, y);
    
                oldX = x;
                oldY = y;
    
            }
    
            frame++;
    
        }
    
    }
    

    I use an AudioStream stream to load a Wav file an produce the 2D samples.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please look at the below code snippet and let me know how the out
Please look at this code: @interface myObject:NSObject -(void)function:(id)param; @end @implementation myObject -(void)function:(id)param { NSLog(@BEFORE);
Please look at this code: def chop(array, search): lo = 0 high = len(array)
Please look at the following code: char* test ( ) { char word[20]; printf
Getting the above error in following code. How to rectify it. Thanks. Please look
am new here. i have a slight problem; PLease look at the following code
Please have a look at the code below: import string from collections import defaultdict
Please have a look on the code below .. <div id=click_me>Save</div> <div id=blocks_sortable> <div
Please take a look at this code: template<class T> class A { class base
Look please below this codes throw me : FormatException was unhandled by user code

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.