Page 2 of 2 FirstFirst 12
Results 21 to 28 of 28
Like Tree1Likes

Thread: alphacomposite for multiple shapes (cut a hole through them)

  1. #21
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default Re: alphacomposite for multiple shapes (cut a hole through them)

    Quote Originally Posted by aubreybourke View Post
    Is there any need to put the methods inside the constructor into the invokeLater
    I was talking about the constructor and method calls in the main(...) method.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  2. #22
    aubreybourke is offline Member
    Join Date
    Feb 2012
    Location
    Dublin
    Posts
    29
    Rep Power
    0

    Default Re: alphacomposite for multiple shapes (cut a hole through them)

    Yeah Ive got everything in my main method scheduled via invokeLater.

  3. #23
    aubreybourke is offline Member
    Join Date
    Feb 2012
    Location
    Dublin
    Posts
    29
    Rep Power
    0

    Default Re: alphacomposite for multiple shapes (cut a hole through them)

    So think I managed to follow everything. And also quite sure my Linux configuration isn't right here. I will have to test my code on another Linux distro later.

    I have a second piece of code which uses the setOpaque(frame, false) and the panel.setOpaque(false) methods. It calls repaint much more than once a minute (like in AlpahaRings) for animation. So I can see the problem allot clearer with that program. And it also suffers from flickering on each call to repaint, plus it doesn't clear the painting area on each repaint (leaving layers of previous painting).

    Sorry, should have told you that from the start. I doubt there is any way you can reproduce this problem, unless you actually download and install Lubuntu (it is a great OS) then install compiz and run the command "compiz --replace". Which enables lubuntu to support true transparency.

    In anycase, I'm quite happy with the alpaRings.

    Java Code:
    /*
     * Rings - A Java RingClock Implementation (like conky lotus rings)
     */
    package rings;
    
    import com.sun.awt.AWTUtilities;
    import java.awt.AlphaComposite;
    import java.awt.Color;
    import java.awt.Composite;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.RenderingHints;
    import java.awt.Toolkit;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.image.BufferedImage;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JWindow;
    import javax.swing.SwingUtilities;
    import javax.swing.Timer;
    
    /**
     *
     * @author aubrey
     */
    public class AlphaRings extends JPanel {
        
        int ito, to, from;
        int hours, mins;
        
        BufferedImage image;
        static JWindow f;
        static AlphaRings ringClockPanel;
        
        Graphics g;
        Graphics2D g2d, gc;
        
        boolean b = true|false;
        
        public AlphaRings()
        {
     
            SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            setOpaque(false);
            
           
           
           update();
           
           initialize();
             
           timer.start();  
           }
           });
        }
        public void initialize()
        {
            setSize(getPreferredSize());
            //setSize(200, 200);
            
            //set fillArc to 12 O Clock
            //ie from = 90;
            from = 90;
    
        }
        
        public void update(){
            //DateTime Example
            //String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(Calendar.getInstance().getTime());
            
            //Time Example
            String timeStamp = new SimpleDateFormat("HH:mm:ss").format(Calendar.getInstance().getTime());
            System.out.println(timeStamp);
            
            String tempMins = timeStamp.substring(3,5);
            System.out.println("Minutes: "+tempMins);
            mins= Integer.parseInt(tempMins);
            to = mins*6;
            
            String tempHours = timeStamp.substring(0,2);
            
            hours= Integer.parseInt(tempHours);
            switch(hours)
            {
                case 13: hours=1;
                  break;
                case 14: hours=2;
                  break;
                case 15: hours=3;
                  break;
                case 16: hours=4;
                  break;
                case 17: hours=5;
                  break;
                case 18: hours=6;
                  break;
                case 19: hours=7;
                   break;
                case 20: hours=8;
                   break;
                case 21: hours=9;
                    break;
                case 22: hours=10;
                    break;
                case 23: hours=11;
                    break;
                case 00: hours=12;
                    break;
                default:
                    break;
            }
            System.out.println("Hours: "+hours);  
            int degree = 360/12;
            ito = hours*degree;
            
        }
        
    
        public void paintComponent(Graphics g)
        {
           
            super.paintComponent(g);
            
                            //OFF Screen
                            image = new BufferedImage(200, 200, BufferedImage.TYPE_INT_ARGB);
                                 Graphics2D gc = image.createGraphics();
       
            
                           Composite originalComposite = gc.getComposite();
              
                            RenderingHints renderHints = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                             renderHints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
                            gc.setRenderingHints(renderHints);
       
            
                           AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.2f);   
                            gc.setComposite(ac);   
                            gc.setColor(Color.WHITE);
    
                            gc.fillOval(25, 25, 100, 100);  
                            
                            gc.setComposite(originalComposite);
                            ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.7f);   
                            gc.setComposite(ac);     
                            gc.setColor(Color.WHITE);
                            gc.fillArc(25, 25, 100, 100, from, -to);
          
                            
                            ac = AlphaComposite.getInstance(AlphaComposite.DST_OUT);
                            gc.setComposite(ac);
                            gc.fillOval(35,35, 80,80);
                            gc.setComposite(originalComposite);
                            
                            ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.3f);   
                            gc.setComposite(ac);                 
                            gc.setColor(Color.WHITE);
            
                            gc.fillOval(35, 35, 80, 80);
                            
                            gc.setComposite(originalComposite);
                            
                            gc.setColor(Color.lightGray);
                            gc.fillArc(35, 35, 80, 80, from, -ito);
                        
                        
     
                            ac = AlphaComposite.getInstance(AlphaComposite.DST_OUT);
                            gc.setComposite(ac);
                     
                            gc.fillOval(45, 45, 60, 60);
                            gc.setComposite(originalComposite);
           
                           
             //On Screen
              Graphics2D g2d = (Graphics2D)g;            
              Composite originalComposite1 = g2d.getComposite();          
              ac = AlphaComposite.getInstance(AlphaComposite.DST_OUT);   
              g2d.setComposite(ac);      
            
              g2d.clearRect(0, 0, 200, 200);
              g2d.setComposite(originalComposite1);
              g2d.drawImage(image, 0, 0, null);
            
          
            
        }
    public Dimension getPrefferedSize(){
        return(new Dimension(200, 200));
    }
        private final Timer timer = new Timer(1000*60, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                    
                System.out.print("ON EDT: ");
                System.out.println(javax.swing.SwingUtilities.isEventDispatchThread());    
                
                update();
                    
                    
                repaint();
            }
        });
        public static void main(String[] args) {
            
            SwingUtilities.invokeLater(new Runnable() {
        public void run() {
           
       
            
            ringClockPanel = new AlphaRings();
            f = new JWindow();
      
            f.setSize(ringClockPanel.getPrefferedSize());
           
            
    
            AWTUtilities.setWindowOpaque(f, false);
            Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
            double width = screenSize.getWidth();
            double height = screenSize.getHeight();
            
            int w = (int)width;
            System.out.println(w);
            int halfed = (w/2)-(f.getWidth()/2);
       
            
            
            f.setLocation(1700, 20);
            f.getContentPane().add(ringClockPanel);
            f.setVisible(true);
        
             }
           });
        
        }
    }

  4. #24
    aubreybourke is offline Member
    Join Date
    Feb 2012
    Location
    Dublin
    Posts
    29
    Rep Power
    0

    Default Re: alphacomposite for multiple shapes (cut a hole through them)

    Heres a screenshot of the above code:
    alphacomposite for multiple shapes (cut a hole through them)-alpharings.png

  5. #25
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default Re: alphacomposite for multiple shapes (cut a hole through them)

    You're still creating the BufferedImage in the paintComponent override. That's inefficient, isn't needed, and could be the primary cause of flickering.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #26
    aubreybourke is offline Member
    Join Date
    Feb 2012
    Location
    Dublin
    Posts
    29
    Rep Power
    0

    Default Re: alphacomposite for multiple shapes (cut a hole through them)

    Yes it seems like a possiblity. But I have tested the same code on Windows and its fine. No flicker, No layers overlapping! I guess it could be anything from the ubuntu Nvidia graphics driver to the Lubuntu operating system, the LXDE environment or Compiz. But Id place a bet its compiz. Might even be able to fix it in the configuration panel. Might go try that...

  7. #27
    aubreybourke is offline Member
    Join Date
    Feb 2012
    Location
    Dublin
    Posts
    29
    Rep Power
    0

    Default Re: alphacomposite for multiple shapes (cut a hole through them)

    I personally love Lubuntu. The branding on all releases has been very good. But apart from looking good, its really efficient. I was using KDE for a long time and at one stage the fans were up so high for so long that my bill went through the roof. Later we discovered the emmersion for the hot water was on constant for six months. But I never got over the thought of overconsuming power. I even started reading about all the ways I could conserve power. Have you heard of ACPI? Aparantly getting your monitor to turn itself off, instead of a screensaver saves 95% of its consumption. Then there is underclocking the CPU with cpufrequtils.

    From the lubuntu site:
    Lubuntu is a flavor of Ubuntu based on the Lightweight X11 Desktop Environment (LXDE), as its default GUI. The goal is to provide a very lightweight distribution, with all the advantages of the Ubuntu world (repositories, support, etc.). Lubuntu is targeted at "normal" PC and laptop users running on low-spec hardware.

  8. #28
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default Re: alphacomposite for multiple shapes (cut a hole through them)

    This is how I would do it:
    Java Code:
    import com.sun.awt.AWTUtilities;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.BufferedImage;
    import java.util.Calendar;
    import javax.swing.*;
    
    public class Clock extends JComponent {
    
      private BufferedImage image;
      private static final Color MINUTE_COLOR = Color.WHITE;
      private static final Color MINUTE_BACK_COLOR = Color.DARK_GRAY;
      private static final Color HOUR_COLOR = Color.LIGHT_GRAY;
      private static final Color HOUR_BACK_COLOR = Color.GRAY;
      private static final int STROKE_WIDTH = 20;
      private Calendar last = Calendar.getInstance();
      private Timer timer = new Timer(500, new ActionListener() {
    
        public void actionPerformed(ActionEvent e) {
          Calendar now = Calendar.getInstance();
    
          if (last.get(Calendar.MINUTE) != now.get(Calendar.MINUTE)) {
            last = now;
            updateImage();
          }
        }
      });
    
      public Clock() {
        setOpaque(false);
        timer.start();
    
        addComponentListener(new ComponentAdapter() {
    
          @Override
          public void componentResized(ComponentEvent e) {
            updateImage();
          }
        });
      }
    
      @Override
      public Dimension getPreferredSize() {
        return new Dimension(200, 200);
      }
    
      private void updateImage() {
        int w = getWidth();
        int h = getHeight();
        if (w + h == 0) {
          image = null;
          return;
        }
        if (image == null || w != image.getWidth() || h != image.getHeight()) {
          image = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
        }
    
        int minuteArc = last.get(Calendar.MINUTE) * (360 / 60);
        int hourArc = last.get(Calendar.HOUR) * (360 / 12);
    
        Graphics2D g2 = image.createGraphics();
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setStroke(new BasicStroke((float) (STROKE_WIDTH + 1),
                BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER));
    
        w -= STROKE_WIDTH * 2;
        h -= STROKE_WIDTH * 2;
        g2.setColor(MINUTE_COLOR);
        g2.drawArc(STROKE_WIDTH, STROKE_WIDTH, w, h, 90, -minuteArc);
        g2.setColor(MINUTE_BACK_COLOR);
        g2.drawArc(STROKE_WIDTH, STROKE_WIDTH, w, h, 90 - minuteArc, minuteArc - 360);
    
        w -= STROKE_WIDTH * 2;
        h -= STROKE_WIDTH * 2;
        g2.setColor(HOUR_COLOR);
        g2.drawArc(STROKE_WIDTH * 2, STROKE_WIDTH * 2, w, h, 90, -hourArc);
        g2.setColor(HOUR_BACK_COLOR);
        g2.drawArc(STROKE_WIDTH * 2, STROKE_WIDTH * 2, w, h, 90 - hourArc, hourArc - 360);
    
        repaint();
      }
    
      @Override
      protected void paintComponent(Graphics g) {
        // super.paintComponent(g);
        if (image != null) {
          g.drawImage(image, 0, 0, this);
        }
      }
    
      public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
    
          @Override
          public void run() {
            JWindow window = new JWindow();
            AWTUtilities.setWindowOpaque(window, false);
            window.add(new Clock());
            window.pack();
    
            Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
            window.setLocation(screenSize.width - window.getWidth(), 0);
            window.setVisible(true);
          }
        });
      }
    }
    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Need help on Multiple Moving Shapes
    By Majeh in forum New To Java
    Replies: 1
    Last Post: 03-19-2011, 09:54 PM
  2. Security hole by Race condition
    By simorgh in forum Threads and Synchronization
    Replies: 0
    Last Post: 03-24-2010, 05:10 AM
  3. Struggling with AlphaComposite .. please help me
    By jeshmal4u in forum Java 2D
    Replies: 3
    Last Post: 03-08-2010, 06:11 AM
  4. Inserting multiple shapes using mouseListener
    By thayalan in forum Java 2D
    Replies: 1
    Last Post: 03-14-2009, 03:43 PM

Tags for this Thread

Posting Permissions

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