MouseListener Help Java

Any Component can have a MouseListener. JLabel is nice for a colored rectangle, as long as you make it opaque. Addendum: Having recommended MouseAdapter elsewhere, I should mention that one instance is enough. Addendum: This update adds the mouse listener in the ColorLabel constructor. import java.awt.Color; import java.awt.Dimension; import java.awt.EventQueue; import java.awt.GridLayout; import java.awt.event.MouseAdapter; import … Read more

Android color picker to be included in the activity

Your class should implement ColorPickerDialog.OnColorChangedListener public class MainActivity implements ColorPickerDialog.OnColorChangedListener { private Paint mPaint; mPaint = new Paint(); // on button click new ColorPickerDialog(this, this, mPaint.getColor()).show(); } ColorPicker Dialog public class ColorPickerDialog extends Dialog { public interface OnColorChangedListener { void colorChanged(int color); } private OnColorChangedListener mListener; private int mInitialColor; private static class ColorPickerView extends View … Read more

Creating a draw rectangle (filled with black color) function in Java for a grid

Don’t use paint, use paintComponent and don’t forget to call super.paintComponent JComponent may not be the best choice, JPanel is probably a better choice What’s wrong with Graphics#fillRect(int, int, int, int)? You might to take a look at Performing Custom Painting and 2D Graphics for more details. I’d advice against trying to have a second … Read more

Paint algorithm leaving white pixels at the edges when I color [duplicate]

You are using exact color fill for dithered/smoothed/antialiased/lossy_compressed image. That leaves pixels with even slightly different color uncolored (borders) creating artifacts. There are 2 easy ways how to remedy it: match close colors instead of exact if you got 2 RGB colors (r0,g0,b0) and (r1,g1,b1) right now (if I see it right) your checking is … Read more

JLayeredPane and painting

As you found, a BufferedImage is an effective way to cache complex content for efficient rendering; CellTest is an example. A flyweight renderer, shown here, is another approach. Finally, I’ve re-factored your instructive example in a way that may make experimentation easier. import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.RenderingHints; import … Read more