Menu

#260 Static Error when working with TransparentBackground

closed
5
2010-09-13
2010-09-12
Tahoe Sands
No

I found some code that seems to be doing something that I would like to try, and it complies well, but I get a Static Error when I try to run (execute) the code from within DrJava. It complies and runs (executes) just fine in a "DOS" command window, it just won't run in DrJava

By the way, the code comes from the book, "Swing Hacks" By Joshua Marinacci, Chris Adamson. Credit where credit is due.

Here is the run-time error message:
Static Error: No constructor in TransparentBackground matches this invocation
Arguments: ()
Candidate signatures: TransparentBackground(JFrame)

And here is the code I am compiling and running in DrJava:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;

public class TransparentBackground extends JComponent
implements ComponentListener, WindowFocusListener, Runnable {

// constants ---------------------------------------------------------------
// instance ----------------------------------------------------------------
private JFrame _frame;
private BufferedImage _background;
private long _lastUpdate = 0;
private boolean _refreshRequested = true;
private Robot _robot;
private Rectangle _screenRect;
private ConvolveOp _blurOp;

// constructor -------------------------------------------------------------

public TransparentBackground(JFrame frame) {
_frame = frame;
try {
_robot = new Robot();
} catch (AWTException e) {
e.printStackTrace();
return;
}

Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
_screenRect = new Rectangle(dim.width, dim.height);

float[] my_kernel = {
0.10f, 0.10f, 0.10f,
0.10f, 0.20f, 0.10f,
0.10f, 0.10f, 0.10f};
_blurOp = new ConvolveOp(new Kernel(3, 3, my_kernel));

updateBackground();
_frame.addComponentListener(this);
_frame.addWindowFocusListener(this);
new Thread(this).start();
}

// protected ---------------------------------------------------------------

protected void updateBackground() {
_background = _robot.createScreenCapture(_screenRect);
}

protected void refresh() {
if (_frame.isVisible() && this.isVisible()) {
repaint();
_refreshRequested = true;
_lastUpdate = System.currentTimeMillis();
}
}

// JComponent --------------------------------------------------------------

protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
Point pos = this.getLocationOnScreen();
BufferedImage buf = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
buf.getGraphics().drawImage(_background, -pos.x, -pos.y, null);

Image img = _blurOp.filter(buf, null);
g2.drawImage(img, 0, 0, null);
g2.setColor(new Color(255, 255, 255, 192));
g2.fillRect(0, 0, getWidth(), getHeight());
}

// ComponentListener -------------------------------------------------------
public void componentHidden(ComponentEvent e) {
}

public void componentMoved(ComponentEvent e) {
repaint();
}

public void componentResized(ComponentEvent e) {
repaint();

}

public void componentShown(ComponentEvent e) {
repaint();
}

// WindowFocusListener -----------------------------------------------------
public void windowGainedFocus(WindowEvent e) {
refresh();
}

public void windowLostFocus(WindowEvent e) {
refresh();
}

// Runnable ----------------------------------------------------------------
public void run() {
try {
while (true) {
Thread.sleep(100);
long now = System.currentTimeMillis();
if (_refreshRequested && ((now - _lastUpdate) > 1000)) {
if (_frame.isVisible()) {
Point location = _frame.getLocation();
_frame.setLocation(-_frame.getWidth(), -_frame.getHeight());
updateBackground();
_frame.setLocation(location);
refresh();
}
_lastUpdate = now;
_refreshRequested = false;
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}

public static void main(String[] args) {
JFrame frame = new JFrame("Transparent Window");
TransparentBackground bg = new TransparentBackground(frame);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(bg);
frame.pack();
frame.setSize(200, 200);
frame.setLocation(500, 500);
frame.setVisible(true);
}
}

BTW, this is my first post and I am sure I have committed some procedural faux pas, for which the trolls with attack me. Sorry!

Discussion

  • Robert Cartwright

    This is a BUG; it should be easy for us to fix. In the meantime, you can type the following phrase in the interactions pane instead of using the "Run" command:

    TransparentBackground.main(new String[0]);

     
  • Mathias Ricken

    Mathias Ricken - 2010-09-12

    Yes, this is a bug, and I'm already looking at fixing it.

    You can also turn off the "smart run" feature: Go to Edit, Preferences, and then select the Interactions Pane category in the Preferences window. There, remove the check mark from "Smart Run".

     
  • Mathias Ricken

    Mathias Ricken - 2010-09-13

    This problem has been fixed in our latest weekly release (drjava-weekly-20100913-r5385), which you can download at

    http://www.cs.rice.edu/~javaplt/drjavarice/weekly/

    The weekly release is only available as jar file, but it can be used on all supported operating systems. We will release a new development release with native Windows and Mac applications soon.

    I apologize for this problem. Please let us know if this weekly release works for you.

     
  • Mathias Ricken

    Mathias Ricken - 2010-09-13
    • labels: 400075 --> Interactions problem
    • assigned_to: nobody --> mgricken
    • status: open --> closed
     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.