Re: [JAVA2D] Null pointer when using paintComponent and html text in JButton
Thanks for the tip. I have added methods to check property changes to the size and text. I also read somewhere that I should not override the paintComponent method but that I should use the paint method instead. The clip is normally set in the paint method of JComponent. If I do that it also works and gives the following method. /** */ public void paint(final Graphics g) { if (isEnabled()) { super.paint(g); return; } if (redraw) { final BufferedImage buf = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB); final Graphics2D g2 = (Graphics2D) buf.getGraphics(); super.paint(g2); final float f = 0.18f; final int heightWidth = 3; final float[] myKernel = { 0, f, 0, f, f, f, 0, f, 0 }; final ConvolveOp op = new ConvolveOp(new Kernel(heightWidth, heightWidth, myKernel)); img = op.filter(buf, null); g2.dispose(); redraw = false; } g.drawImage(img, 0, 0, null); } PS. How can I post nicely formatted code? [Message sent by forum member 'uncletall' (uncletall)] http://forums.java.net/jive/thread.jspa?messageID=286943 === To unsubscribe, send email to [EMAIL PROTECTED] and include in the body of the message signoff JAVA2D-INTEREST. For general help, send email to [EMAIL PROTECTED] and include in the body of the message help.
Re: [JAVA2D] Null pointer when using paintComponent and html text in JButton
[EMAIL PROTECTED] wrote: Thanks for the tip. I have added methods to check property changes to the size and text. I also read somewhere that I should not override the paintComponent method but that I should use the paint method instead. The clip is normally set in the paint method of JComponent. I'll leave that to Swing experts (you may wan to confirm this on the Swing forum). If I do that it also works and gives the following method. /** */ public void paint(final Graphics g) { if (isEnabled()) { super.paint(g); return; } if (redraw) { final BufferedImage buf = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB); final Graphics2D g2 = (Graphics2D) buf.getGraphics(); super.paint(g2); final float f = 0.18f; final int heightWidth = 3; final float[] myKernel = { 0, f, 0, f, f, f, 0, f, 0 }; final ConvolveOp op = new ConvolveOp(new Kernel(heightWidth, heightWidth, myKernel)); img = op.filter(buf, null); g2.dispose(); redraw = false; } g.drawImage(img, 0, 0, null); } Looks pretty good to me. I assume redraw is updated when the size or text changes, right? PS. How can I post nicely formatted code? I believe you need to use [ code ] and [ / code ] (w/o spaces) tags. Thanks, Dmitri === To unsubscribe, send email to [EMAIL PROTECTED] and include in the body of the message signoff JAVA2D-INTEREST. For general help, send email to [EMAIL PROTECTED] and include in the body of the message help.
Re: [JAVA2D] Null pointer when using paintComponent and html text in JButton
[EMAIL PROTECTED] wrote: I have found a solution or work around for my own question. The exception is thrown in Rectangle.intersect(Rectangle r) that was called with a null parameter as a result of BoxView.paint getting a null pointer back from g.getClipBounds(). As a workaround I have changed my paintComponents method as follows: /// public void paintComponent(Graphics g) { super.paintComponent(g); if (isEnabled()) { return; } BufferedImage buf = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB); Graphics2D g2 = (Graphics2D) buf.getGraphics(); g2.setClip(g.getClip()); // Simply copy the clip super.paintComponent(g2); float[] my_kernel = { 0.10f, 0.10f, 0.10f, 0.10f, 0.20f, 0.10f, 0.10f,0.10f, 0.10f }; ConvolveOp op = new ConvolveOp(new Kernel(3, 3, my_kernel)); Image img = op.filter(buf, null); g.drawImage(img, 0, 0, null); g2.dispose(); } One question remains, is this a bug in the Java2D or my programming error? I'd say it may be a bug in javax.text code - they don't expect a null clip. Also, a suggestion on your code. I'm not sure if it's just for illustration, but I would suggest not to create a new image on each repaint as you do currently. You should only create a new image if the size of the component changed (or if you have some other attributes like text that can change). Thanks, Dmitri === To unsubscribe, send email to [EMAIL PROTECTED] and include in the body of the message signoff JAVA2D-INTEREST. For general help, send email to [EMAIL PROTECTED] and include in the body of the message help.
[JAVA2D] Null pointer when using paintComponent and html text in JButton
Hi, I am trying to use html text to the BlurJButton from thhsi example code. http://codeidol.com/java/swing/Basic-JComponents/Blur-Disabled-Components/ However, I get a null pointer when I use html text, using normal text causes no problems. Any help or hints would be welcome. Thanks. This is my full code: public class MyButton extends JButton { /** * serialVersionUID. */ private static final long serialVersionUID = 1241274818258250433L; /** * Constructs a default button. * * @param arg0 *Button name */ public MyButton(final String arg0) { super(arg0); setName(arg0); setVerticalTextPosition(SwingConstants.CENTER); setHorizontalTextPosition(SwingConstants.CENTER); setMargin(new Insets(0, 0, 0, 0)); } private static String formatText(final String text) { return String.format(htmlcenter%s/center/html, text); } /** * Set the text for this button. The text will be formatted as a html tag. * @param text * Text. */ @Override public void setText(final String text) { super.setText(formatText(text)); } public void paintComponent(Graphics g) { super.paintComponent(g); if (isEnabled()) { return; } BufferedImage buf = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB); super.paintComponent(buf.createGraphics()); // - Exception from here float[] my_kernel = { 0.10f, 0.10f, 0.10f, 0.10f, 0.20f, 0.10f, 0.10f, 0.10f, 0.10f }; ConvolveOp op = new ConvolveOp(new Kernel(3, 3, my_kernel)); Image img = op.filter(buf, null); g.drawImage(img, 0, 0, null); } } This is the exception: Exception in thread AWT-EventQueue-0 java.lang.NullPointerException at javax.swing.text.BoxView.paint(Unknown Source) at javax.swing.text.html.BlockView.paint(Unknown Source) at javax.swing.plaf.basic.BasicHTML$Renderer.paint(Unknown Source) at javax.swing.plaf.basic.BasicButtonUI.paint(Unknown Source) at com.sun.java.swing.plaf.windows.WindowsButtonUI.paint(Unknown Source) at javax.swing.plaf.ComponentUI.update(Unknown Source) at javax.swing.JComponent.paintComponent(Unknown Source) at mypackage.MyButton.paintComponent(MyButton.java:64) at javax.swing.JComponent.paint(Unknown Source) at javax.swing.JComponent.paintChildren(Unknown Source) at javax.swing.JComponent.paint(Unknown Source) at javax.swing.JComponent.paintChildren(Unknown Source) at javax.swing.JComponent.paint(Unknown Source) at javax.swing.JComponent.paintChildren(Unknown Source) at javax.swing.JComponent.paint(Unknown Source) at javax.swing.JComponent.paintChildren(Unknown Source) at javax.swing.JComponent.paint(Unknown Source) at javax.swing.JComponent.paintChildren(Unknown Source) at javax.swing.JComponent.paint(Unknown Source) at javax.swing.JComponent.paintChildren(Unknown Source) at javax.swing.JComponent.paint(Unknown Source) at javax.swing.JLayeredPane.paint(Unknown Source) at javax.swing.JComponent.paintChildren(Unknown Source) at javax.swing.JComponent.paintToOffscreen(Unknown Source) at javax.swing.BufferStrategyPaintManager.paint(Unknown Source) at javax.swing.RepaintManager.paint(Unknown Source) at javax.swing.JComponent.paint(Unknown Source) at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source) at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source) at sun.awt.SunGraphicsCallback.runComponents(Unknown Source) at java.awt.Container.paint(Unknown Source) at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source) at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source) at javax.swing.RepaintManager.seqPaintDirtyRegions(Unknown Source) at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(Unknown Source) at java.awt.event.InvocationEvent.dispatch(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source)