Results 1 to 7 of 7
Thread: Problems with Opacity
- 04-18-2012, 06:57 PM #1
Member
- Join Date
- Apr 2012
- Posts
- 3
- Rep Power
- 0
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) { } }
- 04-18-2012, 09:37 PM #2
- 04-18-2012, 09:42 PM #3
Member
- Join Date
- Apr 2012
- Posts
- 3
- Rep Power
- 0
Re: Problems with Opacity
No, I didn't know that. :P
How am I supposed to code it?
- 04-18-2012, 10:08 PM #4
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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 04-18-2012, 10:20 PM #5
Member
- Join Date
- Apr 2012
- Location
- Gampaha, Sri Lanka
- Posts
- 2
- Rep Power
- 0
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.
- 04-18-2012, 11:16 PM #6
Member
- Join Date
- Apr 2012
- Posts
- 3
- Rep Power
- 0
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 :/
- 04-19-2012, 05:11 AM #7
Re: Problems with Opacity
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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
InputStream/Jar Problems/File IO Problems
By rdjava in forum Advanced JavaReplies: 31Last Post: 01-17-2011, 11:12 AM -
Opacity change
By ElhombreLoco in forum New To JavaReplies: 4Last Post: 05-27-2010, 08:54 PM -
GUI Help(Layered panes, opacity, events)
By xcallmejudasx in forum New To JavaReplies: 1Last Post: 04-29-2009, 11:11 PM -
Trouble retaining transparent back color while setting opacity
By playwin2 in forum Java 2DReplies: 4Last Post: 03-26-2009, 09:42 PM -
a few problems
By gary in forum AWT / SwingReplies: 0Last Post: 07-11-2007, 04:57 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks