How to improve painting performance of DataGridView?

I recently had some slowness issues with DataGridView and the solution was the following code public static void DoubleBuffered(this DataGridView dgv, bool setting) { Type dgvType = dgv.GetType(); PropertyInfo pi = dgvType.GetProperty(“DoubleBuffered”, BindingFlags.Instance | BindingFlags.NonPublic); pi.SetValue(dgv, setting, null); } It turns double buffering on for DataGridView objects. Just call DoubleBuffered() on your DGV. Hope it … Read more

android set custom font to a paint

If by “custom font” you mean a font that you are supplying as an asset, the following code should work: Typeface plain = Typeface.createFromAsset(assetManager, pathToFont); Typeface bold = Typeface.create(plain, Typeface.DEFAULT_BOLD) Paint paint = new Paint(); paint.setTypeface(bold); canvas.drawText(“Sample text in bold”,0,0,paint);

Images in paintComponent only show up after resizing the window

You need to call frame.pack() to do the initial layout. Resizing the window automatically causes the layout to be fixed, but frame.setSize(…) does not*. Move frame.setVisible(true) to the end of your run method (i.e. after you’ve constructed all the UI elements) and put frame.pack() just before frame.setVisible(true). (Thanks Hovercraft and MadProgrammer for pointing this out) … Read more

How to get the pixel color on touch?

This is the one I’ve used, and it looks simpler than the methods you’ve tried. In my custom view class, I have this: – (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [[event allTouches] anyObject]; CGPoint loc = [touch locationInView:self]; self.pickedColor = [self colorOfPoint:loc]; } colorOfPoint is a method in a category on UIView, with … Read more

Difference between paint() and paintcomponent()?

Quoting from documentation of paint() method This method actually delegates the work of painting to three protected methods: paintComponent, paintBorder, and paintChildren. … A subclass that just wants to specialize the UI (look and feel) delegate’s paint method should just override paintComponent. It looks like the paint() method actually draws the component, including the border … Read more

Drawing rectangle on a JPanel

1) for Swing JComponent you have to use paintComponent() instead of paint() method 2) your JComponent doesn’t returns PreferredSize for example import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.event.ComponentEvent; import javax.swing.JComponent; import javax.swing.JFrame; public class CustomComponent extends JFrame { private static final long serialVersionUID = 1L; public CustomComponent() { setTitle(“Custom Component Graphics2D”); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); … Read more

Dynamic Graphics Object Painting

There are various strategies you might pursue for this. If the objects are never removed from the drawing once done, use a BufferedImage, put it in a (ImageIcon in a) JLabel. When it comes time to update: Get the graphics instance of the image and draw the new element. Dispose of the graphics object. Call … Read more