Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I'm attempting to make a Subclass to JLabel, which I named BlurPanel. I want the class to act like any normal swing container, only it must as default have a transparent background i.e. (setOpaque(false)) and it must blur the background of the parent frame. So far i've managed to blur BufferedImages and resized them, but when using standard swing layout managers i'm having a hard time at cropping the frame picture to the BlurPanels position. Does anyone have a good strategy to do this? Maybe it can be done without copying the underlying picture?
Use GlassPane Luke
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseAdapter;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.Timer;
import javax.swing.WindowConstants;
/**
* Glass pane used to blur the content of the window.
*
* #author SMedvynskyy
*/
#SuppressWarnings("serial")
public class SplashGlassPane extends JPanel implements FocusListener {
/**
* Creates new GlassPane.
*/
public SplashGlassPane() {
addMouseListener(new MouseAdapter() {});
addMouseMotionListener(new MouseAdapter() {});
addFocusListener(this);
setOpaque(false);
setFocusable(true);
setBackground(new Color(0, 0, 0, 190));
}
#Override
public final void setVisible(boolean v) {
// Make sure we grab the focus so that key events don't go astray.
if (v) {
requestFocus();
}
super.setVisible(v);
}
// Once we have focus, keep it if we're visible
#Override
public final void focusLost(FocusEvent fe) {
if (isVisible()) {
requestFocus();
}
}
/**
* {#inheritDoc}
*/
#Override
public final void paint(Graphics g) {
final Color old = g.getColor();
g.setColor(getBackground());
g.fillRect(0, 0, getSize().width, getSize().height);
g.setColor(old);
super.paint(g);
}
#Override
public void focusGained(FocusEvent fe) {
// nothing to do
}
public static void main(String[] args) {
final JFrame frm = new JFrame("Test blurring");
frm.add(new JTextField("It's first component"), BorderLayout.NORTH);
frm.add(new JTextField("It's second component"), BorderLayout.SOUTH);
final JButton btn = new JButton("Start blur");
btn.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
frm.getGlassPane().setVisible(true);
final Timer t = new Timer(5000, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
frm.getGlassPane().setVisible(false);
}
});
t.setRepeats(false);
t.start();
}
});
frm.add(btn);
frm.setSize(500, 400);
frm.setGlassPane(new SplashGlassPane());
frm.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frm.setVisible(true);
}
}
Related
I am trying to create a clock application for desktop in the language Processing 3 (java based) and I need to make the background transparent so you can see what's behind the clock (for example the desktop).
I tried this:
background(0, 0, 0, 0);
doesn't work.
Can anyone help me?
You're going to have to get to the underlying window and set the transparency of that.
How you do that (and whether it's even possible) is going to depend on which renderer you're using, and what your computer is capable of.
Here's an example of how you might do that with the default renderer:
import processing.awt.PSurfaceAWT;
import processing.awt.PSurfaceAWT.SmoothCanvas;
import javax.swing.JFrame;
void setup() {
size(200, 200);
PSurfaceAWT awtSurface = (PSurfaceAWT) surface;
SmoothCanvas smoothCanvas = (SmoothCanvas) awtSurface.getNative();
JFrame jframe = (JFrame)smoothCanvas.getFrame();
jframe.dispose();
jframe.setUndecorated(true);
jframe.setOpacity(.5f);
jframe.setVisible(true);
}
void draw() {
background(0, 128);
}
Please note that this is just example code, so you might have to play with it to get it to work with your computer and your renderer. But the general idea is there: you have to get to the underlying window, and then set the transparency of that.
If this doesn't work, you'll probably have better luck if you use Processing as a Java library instead of going through the Processing editor. Specifically you should be able to get to the underlying window before it's displayed.
I will try to give you some Java code maybe these help you (Transparent Notification frame):
import static java.awt.GraphicsDevice.WindowTranslucency.PERPIXEL_TRANSPARENT;
import static java.awt.GraphicsDevice.WindowTranslucency.TRANSLUCENT;
import java.awt.Color;
import java.awt.Font;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.GridBagLayout;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.geom.RoundRectangle2D;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
/**
*
* #author Coder ACJHP
*/
public class NotifyMe extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private JLabel label;
public NotifyMe() {
super("NotifyMe");
setLayout(new GridBagLayout());
addComponentListener(new ComponentAdapter() {
#Override
public void componentResized(ComponentEvent e) {
setShape(new RoundRectangle2D.Double(0, 0, getWidth(), getHeight(),15,15));
}
});
setUndecorated(true);
setSize(250, 80);
setAlwaysOnTop(true);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
label = new JLabel();
label.setFont(new Font("Adobe Arabic", Font.BOLD, 14));
label.setBounds(0, 0, getWidth(), getHeight());
label.setForeground(Color.RED);
add(label);
}
public void setNotifiyNote(String note) {
this.label.setText(note);
}
public static void main(String[] args) {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
final boolean isTranslucencySupported = gd.isWindowTranslucencySupported(TRANSLUCENT);
if (!gd.isWindowTranslucencySupported(PERPIXEL_TRANSPARENT)) {
System.err.println("Shaped windows are not supported");
System.exit(0);
}
if (!isTranslucencySupported) {
System.out.println("Translucency is not supported, creating an opaque window");
}
SwingUtilities.invokeLater(() -> {
NotifyMe sw = new NotifyMe();
if (isTranslucencySupported) {
sw.setOpacity(0.7f);
}
});
}
}
Pretty much, once I make my JTextArea and my JTextField transparent, as I type it looks as if all of my components are being duplicated and added to the screen. Am I doing something wrong, or is this a NetBeans bug?
package game;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
/**
*
* #author xDaegothx
*/
public class Game extends JFrame
{
JLayeredPane LP;
Game_Chat GC;
public Game()
{
LP = new JLayeredPane();
LP.setBackground(Color.black);
LP.setOpaque(true);
GC = new Game_Chat();
GC.setBounds(0,350,400,250);
LP.add(GC);
this.getContentPane().add(LP);
this.setBounds(0,0,1200,700);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args)
{
new Game();
}
public class Game_Chat extends JLabel
{
JTextArea TA;
JScrollPane JSP;
JTextField TF;
JButton Submit_btn;
public Game_Chat()
{
TA = new JTextArea();
TA.setForeground(new Color(255,255,255,0));
TA.setBackground(new Color(255,255,255,0));
TA.setOpaque(true);
TA.setText("Welcome to 'Game'!");
JSP = new JScrollPane(TA);
JSP.setOpaque(true);
JSP.setForeground(new Color(255,255,255,0));
JSP.setBackground(new Color(255,255,255,0));
JSP.setBounds(0,0,400,225);
TF = new JTextField();
TF.setOpaque(true);
//TF.setBackground(new Color(255,255,255,0));
TF.setBounds(0,225,350,25);
Submit_btn = new JButton("Send");
Submit_btn.setBorder(null);
Submit_btn.setBounds(350,225,50,25);
TF.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent ae)
{
Submit();
}
});
Submit_btn.addMouseListener(new MouseAdapter()
{
#Override
public void mouseClicked(MouseEvent me)
{
Submit();
}
});
add(JSP);
add(TF);
add(Submit_btn);
setBackground(Color.gray);
setOpaque(true);
}
public void Submit()
{
String charname = "MyName";
TA.append("\n"+charname+": "+TF.getText());
}
}
}
What is the point of setting both the foreground and background transparent? You will never see the text if it is transparent!
Anyway to make a component completely transparent you don't play with the background. Instead you just use:
textArea.setOpaque(false);
If you want partially transparent backgrounds then you do use the setBackground() method. But you will have painting problems. See Backgrounds With Transparency for an explanation of the problems and some potential solutions.
Also, you should NOT be using setBounds() to set the size/location of a component. Swing was designed to be used with layout managers. So take the time to learn how to use them for better functioning programs.
I want to block input to a window, but still be able to move it.
If there was a modal dialog type allowing the window that spawned it to move, then I would be happy.
Say I have a window that opens another window. This second window then opens a modal dialog, which blocks input to the other two windows (fine), but also locks these two windows in place (why - Amigas didn't do this :) ?).
My problem is that I may need to visually read something in the first window for use in the dialog, but this may not be possible because the second window is locked in place, covering it.
I have almost solved this with glass panes, I think. I set the class below to be the glass pane of the root pane of my window, then I call setVisible(true) on it when I want to block and setVisible(false) when I want to unlock the window. When locked, the window greys out to indicate this.
Mouse input is blocked except for closing the window which is fine for now - the problem is that I can still tab around the components on the blocked window and if I get to an editable one, I can edit it with the keyboard, regardless of my empty KeyListener.
Is there an easy way I can prevent the components behind the glass pane from gaining focus?
I am hoping it can be done on the "InputSink" class itself.
I have tried adding its own selfish focus traversal policy and requesting focus when it is visible, but this has no effect.
I have also tried an example I found where a FocusListener was added, whose focusLost method requests focus if the glass pane is visible, but that is overkill, as the window then always stays at front.
Does anybody know a solution in between those two extremes? This is what I have:
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.FocusTraversalPolicy;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.KeyAdapter;
import java.awt.event.MouseAdapter;
import javax.swing.JPanel;
public class InputSink extends JPanel {
public InputSink() {
this(0.2f); //Default opacity.
}
public InputSink(float alpha) {
setOpaque(false);
setBackground(new Color(0, 0, 0, alpha)); //Just store it here.
addMouseListener(new MouseAdapter() {});
addKeyListener(new KeyAdapter() {});
setFocusTraversalPolicy(new FocusTraversalPolicy() {
#Override
public Component getLastComponent(Container aContainer) {
return InputSink.this;
}
#Override
public Component getFirstComponent(Container aContainer) {
return InputSink.this;
}
#Override
public Component getDefaultComponent(Container aContainer) {
return InputSink.this;
}
#Override
public Component getComponentBefore(Container aContainer, Component aComponent) {
return InputSink.this;
}
#Override
public Component getComponentAfter(Container aContainer, Component aComponent) {
return InputSink.this;
}
});
}
public void paintComponent(final Graphics gfx) { //Handle grey-out.
gfx.setColor(getBackground());
Rectangle rect = gfx.getClipBounds();
gfx.fillRect(rect.x, rect.y, rect.width, rect.height);
}
#Override
public void setVisible(boolean visible) {
super.setVisible(visible);
if (visible)
requestFocus();
}
}
So the version I used following Guillaume Polet's suggestion was
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.KeyEventDispatcher;
import java.awt.KeyboardFocusManager;
import java.awt.Rectangle;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class InputSink extends JPanel {
KeyEventDispatcher blockingDispatcher = new KeyEventDispatcher() {
#Override
public boolean dispatchKeyEvent(KeyEvent e) {
return InputSink.this == ((JFrame) SwingUtilities.getWindowAncestor((Component) e.getSource())).getGlassPane(); //Consume!
}
};
public InputSink) {
this(0.2f); //Default opacity.
}
public InputSinkfloat alpha) {
setOpaque(false);
setBackground(new Color(0, 0, 0, alpha)); //Just store it here.
addMouseListener(new MouseAdapter() {});
addKeyListener(new KeyAdapter() {});
}
public void paintComponent(final Graphics gfx) { //Handle grey-out.
gfx.setColor(getBackground());
Rectangle rect = gfx.getClipBounds();
gfx.fillRect(rect.x, rect.y, rect.width, rect.height);
}
#Override
public void setVisible(boolean visible) {
super.setVisible(visible);
if (visible)
KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(blockingDispatcher);
else
KeyboardFocusManager.getCurrentKeyboardFocusManager().removeKeyEventDispatcher(blockingDispatcher);
}
}
Thank you!
You can add a KeyEventDispatcher to the KeyboardFocusManager to block keyboard input.
Small demo below:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.KeyEventDispatcher;
import java.awt.KeyboardFocusManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
public class TestGlassPane {
private static final int COUNTDOWN = 10;
private static final String CLICK_ME = "Click me";
private static final Color GRAY = new Color(192, 192, 192, 128);
private JFrame frame;
private JButton button;
private Timer timer;
private int countdown;
private KeyEventDispatcher blockingDispatcher;
private static class GrayPanel extends JComponent {
#Override
protected void paintComponent(Graphics g) {
g.setColor(GRAY);
g.fillRect(0, 0, getWidth(), getHeight());
}
}
public TestGlassPane() {
blockingDispatcher = new KeyEventDispatcher() {
#Override
public boolean dispatchKeyEvent(KeyEvent e) {
return true;
}
};
}
protected void initUI() {
frame = new JFrame(TestGlassPane.class.getSimpleName());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
button = new JButton(CLICK_ME);
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
blockUserInput();
}
});
GrayPanel glassPane = new GrayPanel();
glassPane.addMouseListener(new MouseAdapter() {
});
frame.setGlassPane(glassPane);
frame.add(button);
frame.setSize(200, 200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
protected void blockUserInput() {
KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(blockingDispatcher);
frame.getGlassPane().setVisible(true);
countdown = COUNTDOWN;
timer = new Timer(1000, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
countdown--;
if (countdown == 0) {
timer.stop();
frame.getGlassPane().setVisible(false);
button.setText(CLICK_ME);
KeyboardFocusManager.getCurrentKeyboardFocusManager().removeKeyEventDispatcher(blockingDispatcher);
} else {
button.setText("We will be back in " + countdown + " seconds");
}
}
});
timer.start();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new TestGlassPane().initUI();
}
});
}
}
Normally, the button can be activated with the Space key, but you will see that it actually gets blocked.
because the second window is locked in place
the JDialog has always been 'moveable' for me (using windows).
another possibility to block input:
when you show your non-modal dialog, include this line
frame.setEnabled(false);
also add a windowListener to the dialog, so that on closing
frame.setEnabled(true);
seems to work OK on windows, other platforms unknown
I am very new to Java AWT. My question header must seem ridiculous to you, sorry about that. In my application I have three buttons which display different threads when clicked on. Now I want to add maybe a button or checkboxes or choicelist, etc when clicked on a particular button. For eg, if I click on yes button, it should display a choice list, something like that. How do I achieve something like that? Here is my code so far:
import java.awt.Button;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class AppWindow extends Frame implements ActionListener{
String keymsg = "Test message";
String mousemsg = "Nothing";
int mouseX=30, mouseY=30;
String msg;
public AppWindow(){
//addKeyListener(new MyKeyAdapter(this));
//addMouseListener(new MyMouseAdapter(this));
addWindowListener(new MyWindowAdapter());
}
public void paint(Graphics g){
g.drawString(msg, 150, 100);
}
//Here the window is created:
public static void main(String args[]){
AppWindow appwin = new AppWindow();
appwin.setSize(new Dimension(300,200));
appwin.setTitle("My first AWT Application");
appwin.setLayout(new FlowLayout(FlowLayout.LEFT));
appwin.setVisible(true);
Button yes,no,maybe;
yes = new Button("yes");
no = new Button("no");
maybe = new Button("maybe");
appwin.add(yes);
appwin.add(no);
appwin.add(maybe);
yes.addActionListener(appwin);
no.addActionListener(appwin);
maybe.addActionListener(appwin);
}
#Override
public void actionPerformed(ActionEvent ae) {
// TODO Auto-generated method stub
String str = ae.getActionCommand();
if(str.equals("yes")){
msg = "You pressed Yes";
}
if(str.equals("no")){
msg = "You pressed No";
}
if(str.equals("maybe")){
msg = "You pressed Maybe";
}
repaint();
}
}
class MyWindowAdapter extends WindowAdapter {
public void windowClosing(WindowEvent we){
System.exit(0);
}
}
Points describing what you should be doing :
As already mentioned by others, better to use Swing over AWT, since Swing is more advanced.
As much as possible, always try to Paint on top of a JPanel or a
JComponent, instead of Painting right on top of your JFrame, by
overriding the paintComponent(Graphics g) method of the said
JComponent/JPanel
Never call setVisible(true) on the JFrame until and unless it's
size has been established. So in general terms, this has to be the
last call, once you are done adding components to the JFrame and
the size of the JFrame has been realized by the LayoutManager.
Inside your actionPerformed(...), instead of writing all if
statement blocks, you should adhere to the if-else if statement
blocks. The benefit of this, over the former is that, at any given
time, only one event will be fired, hence once the said condition is
satisfied, you don't want your code to keep checking other
conditions, which in general is really not a good programming
practice, IMHO.
MOST IMPORTANT THING : Never make calls like pack()/setVisible(...) from within the main method, such calls belong
to the Event Dispatch Thread, and must be done on the same. Please
read Concurrency in Swing for more detail.
Have a look at the example program, for better understanding.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class ComponentExample
{
private CustomPanel drawingBoard;
private JPanel contentPane;
private JButton yesButton;
private JButton noButton;
private JButton maybeButton;
private JComboBox cbox;
private ActionListener buttonAction = new ActionListener()
{
#Override
public void actionPerformed(ActionEvent ae)
{
JButton button = (JButton) ae.getSource();
if (cbox.isShowing())
contentPane.remove(cbox);
if (button == yesButton)
{
drawingBoard.setText("You Pressed YES.");
contentPane.add(cbox, BorderLayout.PAGE_END);
}
else if (button == noButton)
drawingBoard.setText("You Pressed NO.");
else if (button == maybeButton)
drawingBoard.setText("You Pressed MAYBE.");
/*
* revalidate()/repaint() is needed
* when the JComponent is added or
* removed from the already
* visible Container.
*/
contentPane.revalidate();
contentPane.repaint();
}
};
public ComponentExample()
{
cbox = new JComboBox(
new String[]{"I GOT IT"
, "I STILL HAD DOUBT"});
}
private void displayGUI()
{
JFrame frame = new JFrame("Component Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPane = new JPanel();
contentPane.setOpaque(true);
contentPane.setBackground(Color.DARK_GRAY);
contentPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(5, 5));
JPanel buttonPanel = new JPanel();
buttonPanel.setOpaque(true);
buttonPanel.setBackground(Color.WHITE);
yesButton = new JButton("YES");
yesButton.addActionListener(buttonAction);
noButton = new JButton("NO");
noButton.addActionListener(buttonAction);
maybeButton = new JButton("MAY BE");
maybeButton.addActionListener(buttonAction);
buttonPanel.add(yesButton);
buttonPanel.add(noButton);
buttonPanel.add(maybeButton);
contentPane.add(buttonPanel, BorderLayout.PAGE_START);
drawingBoard = new CustomPanel();
contentPane.add(drawingBoard, BorderLayout.CENTER);
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String... args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new ComponentExample().displayGUI();
}
});
}
}
class CustomPanel extends JPanel
{
private String msg;
public CustomPanel()
{
msg = "";
setOpaque(true);
setBackground(Color.WHITE);
}
public void setText(String msg)
{
this.msg = msg;
repaint();
}
#Override
public Dimension getPreferredSize()
{
return (new Dimension(300, 300));
}
#Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawString(msg, getWidth() / 3, getHeight() / 3);
}
}
I don't know if I have understood the question well but... couldn't you create those elements and call their setVisible(boolean) methods to make them not visible at first, and them make them visible when user pushes buttons?
A few days ago I posted a question about a program that caused text on screen to change color when the mousewheel was scrolled. It was unfortunately a badly put together question with too much code posted to be particularly useful.
I had several responses, one of which was from the user trashdog who posted something that fixed the problem (which can be found at the bottom of this page here: Window going blank during MouseWheelMotion event) , however having read the class descriptions of all the things I didn't know about in the program he posted and gone through its execution I don't understand why his achieves a different effect from mine.
His seems to log every mouse wheel movement where as mine only does the initial movement. Also several people commented that they couldn't replicate the effect of my program probably because it was so big.
Below is an extremely simplified version which still elicits the same effect (I hope).
Question: What is the fundamental difference between the two programs that fixes the screen going blank when the mouse wheel events are being processed?
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.util.LinkedList;
import javax.swing.JFrame;
public class WheelPrinter implements MouseWheelListener, Runnable {
JFrame frame;
LinkedList colorList;
int colorCount;
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
WheelPrinter w = new WheelPrinter();
w.run();
}
public WheelPrinter() {
frame = new JFrame();
frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.addMouseWheelListener(this);
frame.setVisible(true);
frame.setBackground(Color.WHITE);
colorList = new LinkedList();
colorList.add(Color.BLACK);
colorList.add(Color.BLUE);
colorList.add(Color.YELLOW);
colorList.add(Color.GREEN);
colorList.add(Color.PINK);
}
#Override
public void mouseWheelMoved(MouseWheelEvent e) {
colorChange();
}
#Override
public void run() {
while(true) {
draw(frame.getGraphics());
try {
Thread.sleep(20);
} catch (Exception ex) {
}
}
}
public void draw(Graphics g) {
g.setColor(frame.getBackground());
g.fillRect(0,0,frame.getWidth(),frame.getHeight());
g.setFont(new Font("sansserif", Font.BOLD, 32));
g.setColor(frame.getForeground());
g.drawString("yes", 50, 50);
}
public void colorChange() {
colorCount++;
if (colorCount > 4) {
colorCount = 0;
}
frame.setForeground((Color) colorList.get(colorCount));
}
}
(Try spinning your mouse wheel really hard if you try running my code and it will become even more obvious)
while(true) { is endless loop, without break; f.e.
use Swing Timer instead of Runnable#Thread delayed by Thread.Sleep()
paint to the JPanel or JComponent, not directly to the JFrame
all painting to the Swing JComponent should be done in paintComponent()
more in the 2D Graphics tutorial
edit
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseWheelEvent;
import java.util.LinkedList;
import java.util.Queue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
/**
* based on example by #trashgod
*
* #see http://stackoverflow.com/a/10970892/230513
*/
public class ColorWheel extends JPanel {
private static final int N = 32;
private static final long serialVersionUID = 1L;
private final Queue<Color> clut = new LinkedList<Color>();
private final JLabel label = new JLabel();
public ColorWheel() {
for (int i = 0; i < N; i++) {
clut.add(Color.getHSBColor((float) i / N, 1, 1));
}
//clut.add(Color.BLACK);
//clut.add(Color.BLUE);
//clut.add(Color.YELLOW);
//clut.add(Color.GREEN);
//clut.add(Color.PINK);
label.setFont(label.getFont().deriveFont(36f));
label.setForeground(clut.peek());
label.setText("#see http://stackoverflow.com/a/10970892/230513");
setBackground(Color.white);
add(label);
label.addMouseWheelListener(new MouseAdapter() {
#Override
public void mouseWheelMoved(MouseWheelEvent e) {
label.setForeground(clut.peek());
clut.add(clut.remove());
}
});
}
#Override
public Dimension getPreferredSize() {
int w = SwingUtilities.computeStringWidth(label.getFontMetrics(
label.getFont()), label.getText());
return new Dimension(w + 20, 80);
}
private void display() {
JFrame f = new JFrame("ColorWheel");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(this);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
new ColorWheel().display();
}
});
}
}
The fundamental difference is that you are trying to interact with the Graphics object from the wrong thread and without knowing anything about the state the Graphics object is in at the time.
The generally correct way to interact with a Graphics object in Swing is by making a custom component which overrides the paintComponent(Graphics) method. You do your drawing while inside that method.
Your colorChange() method can tell your component to re-draw itself by calling repaint(), which will eventually lead to a call to paintComponent(Graphics) on the correct thread at the correct time.
See tutorial here