下面列出了javax.swing.RepaintManager#currentManager ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void addDirtyRegion(JComponent c, int x, int y, int w, int h) {
if (RepaintManager.currentManager(c) == this) {
testFuture.defaultCalled();
} else {
testFuture.delegateCalled();
}
super.addDirtyRegion(c, x, y, w, h);
}
@Override
public void addDirtyRegion(JComponent c, int x, int y, int w, int h) {
if (RepaintManager.currentManager(c) == this) {
testFuture.defaultCalled();
} else {
testFuture.delegateCalled();
}
super.addDirtyRegion(c, x, y, w, h);
}
@Test
public void testCustomPaint() {
HexOrDecimalInput input = new HexOrDecimalInput();
RepaintManager repaintManager = RepaintManager.currentManager(input);
repaintManager.setDoubleBufferingEnabled(false);
SpyPrintStream spy = new SpyPrintStream();
DebugGraphics.setLogStream(spy);
DebugGraphics debugGraphics = new DebugGraphics(scratchGraphics());
debugGraphics.setDebugOptions(DebugGraphics.LOG_OPTION);
Graphics2D g2d = new Graphics2DAdapter(debugGraphics);
input.paintComponent(g2d);
assertThat(spy.toString(), CoreMatchers.containsString("Dec"));
spy.reset();
input.setHexMode();
input.paintComponent(g2d);
assertThat(spy.toString(), CoreMatchers.containsString("Hex"));
spy.reset();
input.setDecimalMode();
input.paintComponent(g2d);
assertThat(spy.toString(), CoreMatchers.containsString("Dec"));
}
@Override
public void addDirtyRegion(JComponent c, int x, int y, int w, int h) {
if (RepaintManager.currentManager(c) == this) {
testFuture.defaultCalled();
} else {
testFuture.delegateCalled();
}
super.addDirtyRegion(c, x, y, w, h);
}
@Override
public void addDirtyRegion(JComponent c, int x, int y, int w, int h) {
if (RepaintManager.currentManager(c) == this) {
testFuture.defaultCalled();
} else {
testFuture.delegateCalled();
}
super.addDirtyRegion(c, x, y, w, h);
}
@Override
public void addDirtyRegion(JComponent c, int x, int y, int w, int h) {
if (RepaintManager.currentManager(c) == this) {
testFuture.defaultCalled();
} else {
testFuture.delegateCalled();
}
super.addDirtyRegion(c, x, y, w, h);
}
@Override
public void addDirtyRegion(JComponent c, int x, int y, int w, int h) {
if (RepaintManager.currentManager(c) == this) {
testFuture.defaultCalled();
} else {
testFuture.delegateCalled();
}
super.addDirtyRegion(c, x, y, w, h);
}
/** @param comp The component to revalidate. */
public static void revalidateImmediately(Component comp) {
if (comp != null) {
RepaintManager mgr = RepaintManager.currentManager(comp);
mgr.validateInvalidComponents();
mgr.paintDirtyRegions();
}
}
@Override
public void addDirtyRegion(JComponent c, int x, int y, int w, int h) {
if (RepaintManager.currentManager(c) == this) {
testFuture.defaultCalled();
} else {
testFuture.delegateCalled();
}
super.addDirtyRegion(c, x, y, w, h);
}
@Override
public void addDirtyRegion(JComponent c, int x, int y, int w, int h) {
if (RepaintManager.currentManager(c) == this) {
testFuture.defaultCalled();
} else {
testFuture.delegateCalled();
}
super.addDirtyRegion(c, x, y, w, h);
}
/**
* Set the alpha transparency level for this component. This automatically
* causes a repaint of the component.
*
* @param alpha must be a value between 0 and 1 inclusive.
*/
public void setAlpha(float alpha) {
if (alpha < 0 || alpha > 1) {
throw new IllegalArgumentException("Alpha must be between 0 and 1 inclusive");
}
if (this.alpha != alpha) {
float oldAlpha = this.alpha;
this.alpha = alpha;
if (alpha > 0f && alpha < 1f) {
if (oldAlpha == 1) {
//it used to be 1, but now is not. Save the oldOpaque
oldOpaque = isOpaque();
setOpaque(false);
}
//TODO this was quite the controversial choice, in automatically
//replacing the repaint manager. In retrospect, I'd probably
//opt for making this a manual choice. There really isn't a clear
//win, no matter the approach.
RepaintManager manager = RepaintManager.currentManager(this);
if (!manager.getClass().isAnnotationPresent(TranslucentRepaintManager.class)) {
RepaintManager.setCurrentManager(new RepaintManagerX());
}
} else if (alpha == 1) {
//restore the oldOpaque if it was true (since opaque is false now)
if (oldOpaque) {
setOpaque(true);
}
}
firePropertyChange("alpha", oldAlpha, alpha);
repaint();
}
}
@Override
public void addDirtyRegion(JComponent c, int x, int y, int w, int h) {
if (RepaintManager.currentManager(c) == this) {
testFuture.defaultCalled();
} else {
testFuture.delegateCalled();
}
super.addDirtyRegion(c, x, y, w, h);
}
/**
* Set the alpha transparency level for this component. This automatically
* causes a repaint of the component.
*
* @param alpha must be a value between 0 and 1 inclusive.
*/
public void setAlpha(float alpha) {
if (alpha < 0 || alpha > 1) {
throw new IllegalArgumentException("Alpha must be between 0 and 1 inclusive");
}
if (this.alpha != alpha) {
float oldAlpha = this.alpha;
this.alpha = alpha;
if (alpha > 0f && alpha < 1f) {
if (oldAlpha == 1) {
//it used to be 1, but now is not. Save the oldOpaque
oldOpaque = isOpaque();
setOpaque(false);
}
//TODO this was quite the controversial choice, in automatically
//replacing the repaint manager. In retrospect, I'd probably
//opt for making this a manual choice. There really isn't a clear
//win, no matter the approach.
RepaintManager manager = RepaintManager.currentManager(this);
if (!manager.getClass().isAnnotationPresent(TranslucentRepaintManager.class)) {
RepaintManager.setCurrentManager(new RepaintManagerX());
}
} else if (alpha == 1) {
//restore the oldOpaque if it was true (since opaque is false now)
if (oldOpaque) {
setOpaque(true);
}
}
firePropertyChange("alpha", oldAlpha, alpha);
repaint();
}
}
public static void disableDoubleBuffering(Component c) {
RepaintManager currentManager = RepaintManager.currentManager(c);
currentManager.setDoubleBufferingEnabled(false);
}
@RandomlyFails // NB-Core-Build #1189
public void testLeakingNbPresenterDescriptor () throws InterruptedException, InvocationTargetException {
try {
Class.forName("java.lang.AutoCloseable");
} catch (ClassNotFoundException ex) {
// this test is known to fail due to JDK bugs 7070542 & 7072167
// which are unlikely to be fixed on JDK6. Thus, if AutoCloseable
// is not present, skip the test
return;
}
WizardDescriptor wizardDescriptor = new WizardDescriptor(getPanels(), null);
wizardDescriptor.setModal (false);
Dialog dialog = DialogDisplayer.getDefault ().createDialog (wizardDescriptor);
WeakReference<WizardDescriptor> w = new WeakReference<WizardDescriptor> (wizardDescriptor);
SwingUtilities.invokeAndWait (new EDTJob(dialog, true));
assertShowing("button is visible", true, dialog);
SwingUtilities.invokeAndWait (new EDTJob(dialog, false));
assertShowing("button is no longer visible", false, dialog);
boolean cancelled = wizardDescriptor.getValue() !=
WizardDescriptor.FINISH_OPTION;
Dialog d = new JDialog();
// workaround for JDK bug 6575402
JPanel p = new JPanel();
d.setLayout(new BorderLayout());
d.add(p, BorderLayout.CENTER);
JButton btn = new JButton("Button");
p.add(btn, BorderLayout.NORTH);
SwingUtilities.invokeAndWait (new EDTJob(d, true));
assertShowing("button is visible", true, btn);
dialog.setBounds(Utilities.findCenterBounds(dialog.getSize()));
SwingUtilities.invokeAndWait (new EDTJob(d, false));
assertShowing("button is no longer visible", false, btn);
assertNull ("BufferStrategy was disposed.", dialog.getBufferStrategy ());
RepaintManager rm = RepaintManager.currentManager(dialog);
rm.setDoubleBufferingEnabled(!rm.isDoubleBufferingEnabled());
rm.setDoubleBufferingEnabled(!rm.isDoubleBufferingEnabled());
dialog = null;
wizardDescriptor = null;
SwingUtilities.invokeAndWait (new Runnable() {
@Override
public void run() {
Frame f = new Frame();
f.setPreferredSize( new Dimension(100,100));
f.setVisible( true );
JDialog dlg = new JDialog(f);
dlg.setVisible(true);
}
});
assertGC ("Dialog disappears.", w);
}
public static void register(JComponent component) {
if (RepaintManager.currentManager(component) != INSTANCE) {
RepaintManager.setCurrentManager(INSTANCE);
}
INSTANCE.addLogComponent(component);
}
public static void disableDoubleBuffering(Component c)
{
RepaintManager currentManager = RepaintManager.currentManager(c);
currentManager.setDoubleBufferingEnabled(false);
}
public static void enableDoubleBuffering(Component c) {
RepaintManager currentManager = RepaintManager.currentManager(c);
currentManager.setDoubleBufferingEnabled(true);
}
private static void doTest() {
RepaintManager repaintManager = RepaintManager.currentManager(frame);
textArea.append("DoubleBufferMaximumSize: " + repaintManager.getDoubleBufferMaximumSize() + "\n");
}
/**
* Setting the <code>RepaintManager</code> like seen
* <a href=http://java.sun.com/products/java-media/2D/samples/index.html>on the official examples for Java2D</a>
* (link last time checked on 12.08.2008).<br>
* This imitates the "On Screen" method used there and in some cases drastically improves performance (even when
* DoubleBuffering of this <code>JComponent</code> is off the DoubleBuffering might still be on because the
* DoubleBuffering is inherited from the main <code>JFrame</code>!).
*
* @param x the x coordinate for the bounding box to repaint
* @param y the y coordinate for the bounding box to repaint
* @param width the width
* @param height the height
*/
public void paintImmediately(int x, int y, int width, int height){
RepaintManager repaintManager = null;
boolean save = true;
if (!isDoubleBuffered()) {
repaintManager = RepaintManager.currentManager(this);
save = repaintManager.isDoubleBufferingEnabled();
repaintManager.setDoubleBufferingEnabled(false);
}
super.paintImmediately(x, y, width, height);
if (repaintManager != null) repaintManager.setDoubleBufferingEnabled(save);
}