Results 1 to 7 of 7
  1. #1
    Kazie is offline Member
    Join Date
    Apr 2012
    Posts
    3
    Rep Power
    0

    Default Problems with Opacity

    Hi I'm working on a little project that lets me draw on my desktop. The main idea is to have a button that lets you switch the background from being fully transparent (only showing what I have drawn) to 50% transparent (at wich i am supposed to draw).

    My 2 Main problems is:

    1. I can't make the background 50% transparent, only fully transparent or fully opaque.

    2. While drawing too fast the line becomes dotted.

    This is my Main class:

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.geom.GeneralPath;
    import java.util.ArrayList;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    public class Main extends JFrame implements MouseMotionListener, MouseListener, KeyListener {
    
        boolean stop = false;
        int currentX, currentY;
        ArrayList<Line> list = new ArrayList();
    
        public Main() {
            super("PaintOnScreen");
    
            addMouseListener(this);
            addMouseMotionListener(this);
            addKeyListener(this);
    
            setExtendedState(Frame.MAXIMIZED_BOTH);
            setUndecorated(true);
            setAlwaysOnTop(true);
    
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setVisible(true);
    
    
    
            JPanel panel = new JPanel() {
    
                public void paintComponent(Graphics g) {               
                    for (Line line : list) {
                        line.paint(g);
                    }
                }
            };
    
            panel.setOpaque(false);
            setGlassPane(panel);
    
            getGlassPane().setVisible(true);
            setVisible(true);
        }
    
        // Toggles the paintmode off to make the fram translucent
        void Paintmodeoff() {
            com.sun.awt.AWTUtilities.setWindowOpaque(this, false);
            stop = true;
            repaint();
        }
        // Toggles the paintmode on to make the area drawable
        void Paintmodeon() {
            com.sun.awt.AWTUtilities.setWindowOpaque(this, true);
            stop = false;
        }
    
        public static void main(String[] args) {
            new Main();
        }
    
        @Override
        public void mouseDragged(MouseEvent e) {
            currentX = e.getX();
            currentY = e.getY();
    
            list.add(new Line(currentX, currentY));
            
            repaint();
        }
    
        @Override
        public void mouseMoved(MouseEvent e) {
        }
    
        @Override
        public void mouseClicked(MouseEvent e) {
        }
    
        @Override
        public void mousePressed(MouseEvent e) {
            currentX = e.getX();
            currentY = e.getY();
            list.add(new Line(currentX, currentY));
        }
    
        @Override
        public void mouseReleased(MouseEvent e) {
            list.add(new Line(currentX, currentY));
            repaint();
            System.out.println("added line");
        }
    
        @Override
        public void mouseEntered(MouseEvent e) {
        }
    
        @Override
        public void mouseExited(MouseEvent e) {
        }
    
        @Override
        public void keyTyped(KeyEvent e) {
        }
    
        @Override
        public void keyPressed(KeyEvent e) {
            if (e.getKeyChar() == KeyEvent.VK_ESCAPE) {
                System.exit(0);
            }
            if (e.getKeyCode() == KeyEvent.VK_A) {
                Paintmodeoff();
                System.out.println("exit Drawing mode");
            }
            if (e.getKeyCode() == KeyEvent.VK_S) {
                Paintmodeon();
                System.out.println("enter Drawing mode");
            }
    
        }
    
        @Override
        public void keyReleased(KeyEvent e) {
        }
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default Re: Problems with Opacity

    Quote Originally Posted by Kazie View Post
    Java Code:
            com.sun.awt.AWTUtilities.setWindowOpaque(this, false);
    :
    :
            com.sun.awt.AWTUtilities.setWindowOpaque(this, true);
    You do know that code is liable to break ... or don't you? And now that Java 7 is out, there's no need to write such fragile code.

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

  3. #3
    Kazie is offline Member
    Join Date
    Apr 2012
    Posts
    3
    Rep Power
    0

    Default Re: Problems with Opacity

    No, I didn't know that. :P
    How am I supposed to code it?

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default Re: Problems with Opacity

    Read the Java 7 API for JFrame. Pay attention to the methods inherited from Window. Or go straight to the API for Window, you'll find a few set... methods that weren't there in Java 6.

    All classes that aren't a part of the public API are subject to change or removal.

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

  5. #5
    Amali is offline Member
    Join Date
    Apr 2012
    Location
    Gampaha, Sri Lanka
    Posts
    2
    Rep Power
    0

    Default Re: Problems with Opacity

    //This is transparent simple example. Try to applly to ur project.
    //I'm using setOpacity method.
    public class TransparentDemo extends JFrame {
    public TransparentDemo() {
    setLayout(new FlowLayout());
    setSize(400,300);
    add(new JButton("OK"));
    }

    public static void main(String[] args) {
    JFrame.setDefaultLookAndFeelDecorated(true);
    TransparentDemo frame = new TransparentDemo();
    frame.setOpacity(0.50f);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    }
    }
    Last edited by Amali; 04-18-2012 at 10:25 PM.

  6. #6
    Kazie is offline Member
    Join Date
    Apr 2012
    Posts
    3
    Rep Power
    0

    Default Re: Problems with Opacity

    Ok, after downloading the latest jdk i finally got it working with setOpacity(0.5f);
    However that gave me a new problem, when making the window transparent the lines which I have drawn becomes transparent too :/

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default Re: Problems with Opacity

    Quote Originally Posted by Amali View Post
    //This is transparent simple example. Try to applly to ur project.
    Don't spoonfeed here. You're possibly denying the questioner the opportunity to acquire the very necessary skill of reading and understanding the API.

    And that's bad code. All Swing components should be constructed and their methods called only on the EDT. Find and go through the tutorial on Concurrency in Swing, and search the net for articles on Swing's single threaded rule.

    Also bad is that you didn't bother to find out how people post formatted code. This forum has a FAQs section, which should have been the first place to look. Or you could have clicked "Reply with Quote" below a post that does have properly formatted code to see how it's done.

    In summary, posting a few lines of code that 'works' is not helpful. Please don't do it again.

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

Similar Threads

  1. InputStream/Jar Problems/File IO Problems
    By rdjava in forum Advanced Java
    Replies: 31
    Last Post: 01-17-2011, 11:12 AM
  2. Opacity change
    By ElhombreLoco in forum New To Java
    Replies: 4
    Last Post: 05-27-2010, 08:54 PM
  3. GUI Help(Layered panes, opacity, events)
    By xcallmejudasx in forum New To Java
    Replies: 1
    Last Post: 04-29-2009, 11:11 PM
  4. Replies: 4
    Last Post: 03-26-2009, 09:42 PM
  5. a few problems
    By gary in forum AWT / Swing
    Replies: 0
    Last Post: 07-11-2007, 04:57 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
  •