What is "Paint" event handler in Java? - java

What is the event handler in Java (using net beans, Swing) that resembles the Paint in C#?
The event which shall be fired when the form is restored, resized ... etc
public void paint(Graphics g2){
g2 = pnlDrawing.getGraphics();
g2.clearRect(0, 0, size, size);
g2.setColor(Color.BLACK);
g2.fillRect(size/2-1, 0, 2, size); // draw y axis
g2.fillRect(0, size/2-1, size, 2); // draw x axis
//set the font
g2.setFont(new Font("Arial", 2, 12));
// write the values on the X axis
for(int i=0; i<=10; i++){
if(i == 0)
continue;
int pos1 = (size/2-1)-i*30;
int pos2 = (size/2-1)+i*30;
g2.draw3DRect(pos1, size/2-3, 1, 5, true);
g2.drawString(String.valueOf(-i),pos1-10,size/2-3+20);
g2.draw3DRect(pos2, size/2-3, 1, 5, true);
g2.drawString(String.valueOf(i),pos2-5,size/2-3+20);
}
for(int i=0; i<=10; i++){
if(i == 0)
continue;
int pos1 = (size/2-1)-i*30;
int pos2 = (size/2-1)+i*30;
g2.draw3DRect(size/2-3, pos1, 5, 1, true);
g2.drawString(String.valueOf(i),size/2-3+10,pos1+5);
g2.draw3DRect(size/2-3, pos2, 5, 1, true);
g2.drawString(String.valueOf(-i),size/2-3+10,pos2+5);
}
pnlDrawing.invalidate();
}

The method:
public void paint(Graphics g)
in the java.awt.Component class (which is the superclass for all Swing components) is the callback method for painting. So any repainting of the components that needs to be done will eventually call this method, so you can override it if you wish to perform your own painting.
== UPDATE ==
You need to subclass a component to get the paint callback, e.g.
public class MyPanel extends JPanel {
public void paint(Graphics g) {
// do your painting here
}
}
Or you could use an anonymous inner class if you don't want to create a whole new class, e.g.
pnlDrawing = new JPanel() {
public void paint(Graphics g) {
// Your painting code
}
}

You should override paintComponent method because paint is also responsible for drawing child components, border and doing some other stuff.

The event which shall be fired when
the form is restored, resized ... etc
I'm not sure I understand your question. Swing will automatically make sure the painting is done for you and there is no "paint event" that you can listen for. If you want to understand more about painting then you can read up on Painting in AWT and Swing.
If you want to know what event is fired when a form is:
a) resized - use a ComponentListener
b) restored - use a WindowListener
In the above cases read the appropriate section from the Swing Tuturial. You will also find a section on Custom Painting which explains why paint should be done in the paintComponent() method.

In a better world, you wouldn't have to reinvent the wheel, but in the current one, you do.
so here is a BetterJPanel which allows you to add and remove paint events without having to subclass JPanel every time.
BetterJPanel
import java.util.function.*;
import java.util.*;
import javax.swing.*;
import java.awt.*;
public class BetterJPanel extends JPanel
{
protected Map<String, ArrayList<Function<Map<String, Object>,Void>>> events;
public final static long serialVersionUID = -4405124172582504448L;
public BetterJPanel()
{
super();
this.events = new HashMap<String, ArrayList<Function<Map<String, Object>, Void>>>();
}
public void paint(Graphics g)
{
ArrayList<Function<Map<String, Object>,Void>> paintEvents = null;
if (this.events.containsKey("paint")) {
paintEvents = this.events.get("paint");
for (Function<Map<String, Object>,Void> evt : paintEvents) {
Map<String, Object> eventArguments = new HashMap<String, Object>();
eventArguments.put("graphics", g);
evt.apply(eventArguments);
}
}
}
public void on(String eventName, Function<Map<String, Object>, Void> cb)
{
if (!events.containsKey(eventName)) {
ArrayList<Function<Map<String, Object>,Void>> tmp = new ArrayList<Function<Map<String, Object>,Void>>();
events.put(eventName, tmp);
}
events.get(eventName).add(cb);
}
public void off(String eventName, Function<Map<String, Object>, Void> cb)
{
ArrayList<Function<Map<String, Object>,Void>> events = null;
if (this.events.containsKey(eventName)) {
events = this.events.get(eventName);
events.remove(cb);
}
}
}
Here's a demonstration of using it(note no subclassing of JPanel anymore):
import java.util.function.*;
import java.awt.*;
import javax.swing.*;
import java.util.*;
public final class TestBetterJPanel
{
public static void main(String[] args)
{
JFrame f = new JFrame("Better JPanel Testing");
BetterJPanel p = new BetterJPanel();
p.on("paint", evt -> {
Graphics g = (Graphics)evt.get("graphics");
g.setColor(Color.BLACK);
g.fillRect(0, 0, 800, 500);
return null;
});
p.on("paint", evt -> {
Graphics g = (Graphics)evt.get("graphics");
g.setColor(new Color(0x33, 0x66, 0xff));
g.fillRect(0, 0, 400, 500);
return null;
});
p.setPreferredSize(new Dimension(800, 500));
f.add(p);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
The black rectangle is drawn by first event, the blue one by the second event.
Why go through all this trouble? reusability: jjs(run with jjs -cp . test.js, comes with all new Java versions):
var Thread = java.lang.Thread;
var BetterJPanel = Java.type("BetterJPanel");
var JPanel = javax.swing.JPanel;
var Dimension = java.awt.Dimension;
var Color = java.awt.Color;
var JFrame = javax.swing.JFrame;
var frame = new JFrame("Hello, World!");
var panel = new BetterJPanel();
panel.setPreferredSize(new Dimension(800, 500));
frame.add(panel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
var x = 20;
var y = 20;
panel.on("paint", function(e) {
var g = e.graphics;
g.setColor(Color.BLACK);
g.fillRect(0, 0, 800, 500);
g.setColor(new Color(0x33, 0x66, 0x99));
g.fillOval(x, y, 30, 30);
});
new Thread(function() {
for (;;) {
x += 1;
y += 1;
if (x > 800) {
x = 0;
}
if (y > 500) {
y = 0;
}
panel.repaint();
Thread.sleep(1000 / 60);
}
}).start();
frame.setVisible(true);
This ball is moving southeast at ~1 pixel per 1/60 of a second, starting over on the other side when it leaves the canvas.
Hopefully when Java9 comes around, this kind of stuff starts popping up in core Java.

repaint() . it makes a request to call paint() eventually though, if I'm not mistaken. paint() is the real painter method but repaint() is fired in those restore, resize etc.

Related

Can't Use .addKeyListener(this) for a static JPanel, but need the JPanel to stay static - Java

I am trying to make a simple program where an oval follows your mouse cursor, and if you enter "r", "g", or "b" on the keyboard, the oval changes color accordingly.
However, I cannot get my KeyListener to work. Here is my issue. I have a static JPanel, because I need it to be accessible in all functions and methods. However, Java does not let you do this with a static JPanel. I need the JPanel to be static so I can set the color in the keyPressed(KeyEvent e) function.
I understand the basics of Java fairly well, and am grasping some more complicated concepts. Please try to explain if there is any complex code. Thank you!
Here is the code in Drivers.java, the main class.
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import javax.swing.*;
public class Drivers implements KeyListener
{
// panel.red = panel.red - 3;
// panel.green = panel.green - 3;
// panel.blue = panel.blue - 3;
public static JFrame frame = new JFrame();
public static ShapesPanel panel = new ShapesPanel().addKeyListener(this);
// Notice the error we get with the addKeyListener(this);
public static void main(String[] args)
{
// Creates new pointer info
PointerInfo info;
// Creates a point (for mouse tracking)
Point point;
JLabel label = new JLabel();
panel.add(label);
// Set window size
panel.setPreferredSize(new Dimension(300, 350));
// Set panel inside frame
frame.setContentPane(panel);
// Transparent 16 x 16 pixel cursor image.
BufferedImage cursorImg = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB);
// Create a new blank cursor.
Cursor blankCursor = Toolkit.getDefaultToolkit().createCustomCursor(
cursorImg, new Point(0, 0), "blank cursor");
// Set the blank cursor to the JFrame.
frame.getContentPane().setCursor(blankCursor);
// Compile everything into the frame
frame.pack();
// Set frame to close on red exit button
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Get screen size
Dimension sSize = Toolkit.getDefaultToolkit().getScreenSize();
// Position frame
frame.setLocation(sSize.width / 2 - frame.getWidth(), sSize.height / 2 - frame.getHeight());
// Make frame visible
frame.setVisible(true);
// Set name of frame
frame.setTitle("Graphical User Interface");
// While loop to draw oval
while(true)
{
// Repaint the panel
panel.repaint();
// Get mouse info (for tracking)
info = MouseInfo.getPointerInfo();
// Set mouse location data to point
point = info.getLocation();
// Create variables to store coordinates of oval from mouse point location
int x = (int) point.getX();
int y = (int) point.getY();
// Assign those coordinate variables to oval
panel.x = x;
panel.y = y;
// System.out.println("X: " + x);
// System.out.println("Y: " + y);
// System.out.println("X: " + point.getX());
// System.out.println("Y: " + point.getY());
// Try-catch to sleep, to reduce some memory
try
{
Thread.sleep(10);
}
catch(InterruptedException e)
{
}
}
}
// If key is pressed
public void keyPressed(KeyEvent e)
{
// If key is R, change color and print that key has been pressed
if (e.getKeyCode() == KeyEvent.VK_R)
{
System.out.println("R");
panel.red = 255;
panel.green = 0;
panel.blue = 0;
}
// If key is G, change color and print that key has been pressed
if (e.getKeyCode() == KeyEvent.VK_G)
{
System.out.println("G");
panel.red = 0;
panel.green = 255;
panel.blue = 0;
}
// If key is B, change color and print that key has been pressed
if (e.getKeyCode() == KeyEvent.VK_B)
{
System.out.println("B");
panel.red = 0;
panel.green = 0;
panel.blue = 255;
}
}
// Doesn't do anything.. yet
#Override
public void keyReleased(KeyEvent e)
{
}
#Override
public void keyTyped(KeyEvent e)
{
}
}
And, here is the code in ShapesPanel.java:
import java.awt.*;
import javax.swing.*;
public class ShapesPanel extends JPanel
{
// Create x and y variables, and set as default to zero
public int x = 0, y = 0;
// Create RGB variables, used for changing color
public int red = 0, green = 0, blue = 0;
private static final long serialVersionUID = 1L;
// Create new paintComponent function, using an override
#Override
public void paintComponent(Graphics g)
{
// Create new Graphics2D g2 version
Graphics2D g2 = (Graphics2D) g;
// Reset screen, so there are no trails
g2.clearRect(0, 0, getWidth(), getHeight());
// Set background, currently unfunctional
// g2.setBackground(new Color(235, 150, 30));
// Set color palette, using RGB variables
g2.setColor(new Color(red, green, blue));
// Use color palette to draw oval, using x and y variables
g2.fillOval(x, y, 100, 100);
}
}
I have a static JPanel, because I need it to be accessible in all functions and methods.
This is not a good reason for making a field static.
However, Java does not let you do this with a static JPanel.
This is not true at all. You can add KeyListeners or any other similar construct just as easily to static and non-static fields. Your problem has nothing to do with restrictions on use of static fields. It's all because you're trying to use this in a static context where this doesn't exist.
Note that your compiler error could go away with something as simple as:
public static ShapesPanel panel = new ShapesPanel().addKeyListener(new Drivers());
I need the JPanel to be static so I can set the color in the keyPressed(KeyEvent e) function.
This again is not a good reason for the field to be static. A Swing listener has direct access to the listened to component any time and at all times via the XxxEvent parameter's getSource() method. For instance, if you used a KeyListener, then its method's KeyEvent parameter has a getSource() method which will return the component (here your drawing JPanel) that is being listened to. If you need references to other components or objects, then pass them into the listener via a constructor setter parameter.
Your main problem is that you're trying to use this in a static context, and this doesn't exist within this context.
First thing is to not make your panel field static. You state that you know Java pretty well, but then give a wrong reason for making it static. Instead make it an instance field and pass the instance where needed.
You've got many other issues with this code including:
You've a huge static main method. This method should be much smaller and its job should be to create the key objects of your program, set them running and that's it.
You've a Swing program that has a while (true) and a Thread.sleep(...) within code that on later iterations (when you've structured your program better and have started all Swing code on the event thread) risk being called on the Swing event thread. You will want to get rid of these guys and consider using a Swing Timer instead.
Your paintComponent method does not call the super's method, breaking the Swing painting chain.
You would be much better off not using a KeyListener but instead using Key Bindings.
Your while (true) block isn't even needed. Don't poll for the mouse's position but instead use a MouseListener and/or MouseMotionListener for this.
For example:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.event.*;
import java.util.HashMap;
import java.util.Map;
import javax.swing.*;
public class KeyBindingTest {
// start gui
private static void createAndShowGui() {
KeyBindingPanel mainPanel = new KeyBindingPanel();
JFrame frame = new JFrame("Key Binding Example");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
// start all in a thread safe manner
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
class KeyBindingPanel extends JPanel {
private static final long serialVersionUID = 1L;
private static final int PREF_W = 600;
private static final int PREF_H = PREF_W;
private static final Color BACKGROUND = Color.WHITE;
private Color ovalColor = Color.blue;
private int ovalX = PREF_W / 2;
private int ovalY = PREF_H / 2;
private int ovalWidth = 100;
public KeyBindingPanel() {
setName("Key Binding Eg");
setBackground(BACKGROUND);
final Map<Color, Integer> colorKeyMap = new HashMap<>();
colorKeyMap.put(Color.BLUE, KeyEvent.VK_B);
colorKeyMap.put(Color.RED, KeyEvent.VK_R);
colorKeyMap.put(Color.GREEN, KeyEvent.VK_G);
// set Key Bindings
int condition = WHEN_IN_FOCUSED_WINDOW;
InputMap inputMap = getInputMap(condition);
ActionMap actionMap = getActionMap();
for (final Color color : colorKeyMap.keySet()) {
int keyCode = colorKeyMap.get(color);
KeyStroke keyStroke = KeyStroke.getKeyStroke(keyCode, 0);
inputMap.put(keyStroke, keyStroke.toString());
actionMap.put(keyStroke.toString(), new ColorAction(color));
}
MyMouse myMouse = new MyMouse();
addMouseMotionListener(myMouse);
}
public void setOvalColor(Color color) {
ovalColor = color;
repaint();
}
public void setOvalPosition(Point p) {
ovalX = p.x;
ovalY = p.y;
repaint();
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setColor(ovalColor);
int x = ovalX - ovalWidth / 2;
int y = ovalY - ovalWidth / 2;
g2.fillOval(x, y, ovalWidth, ovalWidth);
}
#Override // make panel bigger
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(PREF_W, PREF_H);
}
}
class ColorAction extends AbstractAction {
private static final long serialVersionUID = 1L;
private Color color;
public ColorAction(Color color) {
this.color = color;
}
#Override
public void actionPerformed(ActionEvent e) {
// get reference to bound component
KeyBindingPanel panel = (KeyBindingPanel) e.getSource();
panel.setOvalColor(color);
}
}
class MyMouse extends MouseAdapter {
#Override
public void mouseMoved(MouseEvent e) {
// get reference to listened-to component
KeyBindingPanel panel = (KeyBindingPanel) e.getSource();
panel.setOvalPosition(e.getPoint());
}
}
You may ask, why use a Map<Color, Integer> when creating the Key Bindings?
Doing this allows me to use the for loop to avoid code repetition. Often more concise code is easier to understand and to debug. It also makes it easier to enhance the program later. For instance, if later I want to add Color.CYAN and associate it with the c char, all I have to do is add another entry to my Map:
colorKeyMap.put(Color.CYAN, KeyEvent.VK_C);
and boom it's done. If I needed more than a 1-1 association, then I'd consider using an Enum or separate class to hold the related attributes together.

Java JPanel getGraphics()

Since Java only supports single inheritance, I desire to paint directly on an instance of a JPanel that is a member of the class Panel. I grab an instance of Graphics from the member and then paint whatever I desire onto it.
How can I not inherit from JComponent or JPanel and still utilize getGraphics() for painting on this without overriding public void paintComponent(Graphics g)?
private class Panel {
private JPanel panel;
private Graphics g;
public Panel() {
panel = new JPanel();
}
public void draw() {
g = panel.getGraphics();
g.setColor(Color.CYAN);
g.draw(Some Component);
panel.repaint();
}
}
The panel is added to a JFrame that is made visible prior to calling panel.draw(). This approach is not working for me and, although I already know how to paint custom components by inheriting from JPanel and overriding public void paintComponent(Graphics g), I did not want to inherit from JPanel.
Here are some very simple examples which show how to paint outside paintComponent.
The drawing actually happens on a java.awt.image.BufferedImage, and we can do that anywhere, as long as we're on the Event Dispatch Thread. (For discussion of multithreading with Swing, see here and here.)
Then, I'm overriding paintComponent, but only to paint the image on to the panel. (I also paint a little swatch in the corner.)
This way the drawing is actually permanent, and Swing is able to repaint the panel if it needs to without causing a problem for us. We could also do something like save the image to a file easily, if we wanted to.
import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
/**
* Holding left-click draws, and
* right-clicking cycles the color.
*/
class PaintAnyTime {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new PaintAnyTime();
}
});
}
Color[] colors = {Color.red, Color.blue, Color.black};
int currentColor = 0;
BufferedImage img = new BufferedImage(256, 256, BufferedImage.TYPE_INT_ARGB);
Graphics2D imgG2 = img.createGraphics();
JFrame frame = new JFrame("Paint Any Time");
JPanel panel = new JPanel() {
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
// Creating a copy of the Graphics
// so any reconfiguration we do on
// it doesn't interfere with what
// Swing is doing.
Graphics2D g2 = (Graphics2D) g.create();
// Drawing the image.
int w = img.getWidth();
int h = img.getHeight();
g2.drawImage(img, 0, 0, w, h, null);
// Drawing a swatch.
Color color = colors[currentColor];
g2.setColor(color);
g2.fillRect(0, 0, 16, 16);
g2.setColor(Color.black);
g2.drawRect(-1, -1, 17, 17);
// At the end, we dispose the
// Graphics copy we've created
g2.dispose();
}
#Override
public Dimension getPreferredSize() {
return new Dimension(img.getWidth(), img.getHeight());
}
};
MouseAdapter drawer = new MouseAdapter() {
boolean rButtonDown;
Point prev;
#Override
public void mousePressed(MouseEvent e) {
if (SwingUtilities.isLeftMouseButton(e)) {
prev = e.getPoint();
}
if (SwingUtilities.isRightMouseButton(e) && !rButtonDown) {
// (This just behaves a little better
// than using the mouseClicked event.)
rButtonDown = true;
currentColor = (currentColor + 1) % colors.length;
panel.repaint();
}
}
#Override
public void mouseDragged(MouseEvent e) {
if (prev != null) {
Point next = e.getPoint();
Color color = colors[currentColor];
// We can safely paint to the
// image any time we want to.
imgG2.setColor(color);
imgG2.drawLine(prev.x, prev.y, next.x, next.y);
// We just need to repaint the
// panel to make sure the
// changes are visible
// immediately.
panel.repaint();
prev = next;
}
}
#Override
public void mouseReleased(MouseEvent e) {
if (SwingUtilities.isLeftMouseButton(e)) {
prev = null;
}
if (SwingUtilities.isRightMouseButton(e)) {
rButtonDown = false;
}
}
};
PaintAnyTime() {
// RenderingHints let you specify
// options such as antialiasing.
imgG2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
imgG2.setStroke(new BasicStroke(3));
//
panel.setBackground(Color.white);
panel.addMouseListener(drawer);
panel.addMouseMotionListener(drawer);
Cursor cursor =
Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR);
panel.setCursor(cursor);
frame.setContentPane(panel);
frame.pack();
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
It's also possible to set up a JLabel with an ImageIcon, although personally I don't like this method. I don't think JLabel and ImageIcon are required by their specifications to see changes we make to the image after we've passed it to the constructors.
This way also doesn't let us do stuff like painting the swatch. (For a slightly more complicated paint program, on the level of e.g. MSPaint, we'd want to have a way to select an area and draw a bounding box around it. That's another place we'd want to be able to paint directly on the panel, in addition to drawing to the image.)
import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
/**
* Holding left-click draws, and
* right-clicking cycles the color.
*/
class PaintAnyTime {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new PaintAnyTime();
}
});
}
Color[] colors = {Color.red, Color.blue, Color.black};
int currentColor = 0;
BufferedImage img = new BufferedImage(256, 256, BufferedImage.TYPE_INT_ARGB);
Graphics2D imgG2 = img.createGraphics();
JFrame frame = new JFrame("Paint Any Time");
JLabel label = new JLabel(new ImageIcon(img));
MouseAdapter drawer = new MouseAdapter() {
boolean rButtonDown;
Point prev;
#Override
public void mousePressed(MouseEvent e) {
if (SwingUtilities.isLeftMouseButton(e)) {
prev = e.getPoint();
}
if (SwingUtilities.isRightMouseButton(e) && !rButtonDown) {
// (This just behaves a little better
// than using the mouseClicked event.)
rButtonDown = true;
currentColor = (currentColor + 1) % colors.length;
label.repaint();
}
}
#Override
public void mouseDragged(MouseEvent e) {
if (prev != null) {
Point next = e.getPoint();
Color color = colors[currentColor];
// We can safely paint to the
// image any time we want to.
imgG2.setColor(color);
imgG2.drawLine(prev.x, prev.y, next.x, next.y);
// We just need to repaint the
// label to make sure the
// changes are visible
// immediately.
label.repaint();
prev = next;
}
}
#Override
public void mouseReleased(MouseEvent e) {
if (SwingUtilities.isLeftMouseButton(e)) {
prev = null;
}
if (SwingUtilities.isRightMouseButton(e)) {
rButtonDown = false;
}
}
};
PaintAnyTime() {
// RenderingHints let you specify
// options such as antialiasing.
imgG2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
imgG2.setStroke(new BasicStroke(3));
//
label.setPreferredSize(new Dimension(img.getWidth(), img.getHeight()));
label.setBackground(Color.white);
label.setOpaque(true);
label.addMouseListener(drawer);
label.addMouseMotionListener(drawer);
Cursor cursor =
Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR);
label.setCursor(cursor);
frame.add(label, BorderLayout.CENTER);
frame.pack();
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class SomeComponent extends JComponent {
private Graphics2D g2d;
public void paintComponent(Graphics g) {
g2d = (Graphics2D) g.create();
g2d.setColor(Color.BLACK);
g2d.scale(scale, scale);
g2d.drawOval(0, 0, importance, importance);
}
public Graphics2D getG2d() {
return g2d;
}
public void setG2d(Graphics2D g2d) {
this.g2d = g2d;
}
}
then you can do the following
get the SomeComponent instance in the panel and modify it
Graphics2D x= v.getPanel().get(i).getG2d;
x.setColor(Color.BLUE);
v.getPanel().get(i).setG2d(x);
v.getPanel().repaint();
v.getPanel().revalidate();
V is a class that extends JFrame and contains the panel in it AND
i is instance of SomeComponent

Proper way to use JLabels to update an image

I am creating a GUI, and am fairly new to swing and awt. I am trying to create a gui that, upon launch, sets the background to an image, then uses a method to create a slideshow of sorts. I have attempted it, and I am not attached to the code so I am able to take both revisions and/or whole new concepts.
EDIT(9/15/13): I am having trouble with the slideshow, I cant seem to get it to work.
Here is my current code.
public class MainFrame extends JFrame{
JLabel backgroundL = null;
private JLabel bakckgroundL;
BufferedImage backimg;
Boolean busy;
double width;
double height;
public MainFrame() throws IOException {
initMainframe();
}
public void initMainframe() throws IOException {
//misc setup code, loads a default jpg as background
setTitle("Pemin's Aura");
busy = true;
String backgroundDir = "resources/frame/background.jpg";
backimg = ImageIO.read(new File(backgroundDir));
backgroundL = new JLabel(new ImageIcon(backimg));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
refreshframe();
setVisible(true);
busy = false;
}
public void adjSize() { // the attempted start of a fullscreen mode
GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(this);
width = this.getWidth();
height = this.getHeight();
setVisible(true);
}
public void setmastheadText() {//unfinished code
busy = true;
busy = false;
}
public void setbackground() {
add(backgroundL);
}
public void refreshframe() { //should refresh image?
setSize(2049, 2049);
setSize(2048, 2048);
}
public void loadingscreen() throws IOException, InterruptedException {
//this is the code in question that is faulty.
if (busy == false) {
busy = true;
String backgroundDir1 = "resources/frame/background.jpg";
String backgroundDir2 = "resources/frame/scr1.jpg";
String backgroundDir3 = "resources/frame/scr2.jpg";
BufferedImage backimg1 = ImageIO.read(new File(backgroundDir1));
BufferedImage backimg2 = ImageIO.read(new File(backgroundDir2));
BufferedImage backimg3 = ImageIO.read(new File(backgroundDir3));
backgroundL = new JLabel(new ImageIcon(backimg1));
Thread.sleep(2000);
setbackground();
setVisible(true);
backgroundL = new JLabel(new ImageIcon(backimg2));
setbackground();
setVisible(true);
Thread.sleep(2000);
bakckgroundL = new JLabel(new ImageIcon(backimg3));
setbackground();
setVisible(true);
if(backimg != null) {
backgroundL = new JLabel(new ImageIcon(backimg));;
}
}
busy = false;
}//end of loading screen
See ImageViewer for a working example of displaying images using a Swing based Timer.
See also How to use Swing Timers.
And while I'm here, another (prettier) example of animating an image. It uses this Mercator map of land masses. The image can be tiled horizontally, and therefore be scrolled left/right as needed.
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;
import javax.swing.*;
import java.net.URL;
import javax.imageio.ImageIO;
public class WorldView {
public static void main(String[] args) throws Exception {
URL url = new URL("http://i.stack.imgur.com/P59NF.png");
final BufferedImage bi = ImageIO.read(url);
Runnable r = new Runnable() {
#Override
public void run() {
int width = 640;
int height = 316;
Graphics2D g = bi.createGraphics();
float[] floats = new float[]{0f, .4f, .55f, 1f};
Color[] colors = new Color[]{
new Color(20, 20, 20, 0),
new Color(0, 10, 20, 41),
new Color(0, 10, 20, 207),
new Color(0, 10, 20, 230),};
final LinearGradientPaint gp2 = new LinearGradientPaint(
new Point2D.Double(320f, 0f),
new Point2D.Double(0f, 0f),
floats,
colors,
MultipleGradientPaint.CycleMethod.REFLECT);
final BufferedImage canvas = new BufferedImage(
bi.getWidth(), bi.getHeight() + 60,
BufferedImage.TYPE_INT_RGB);
final JLabel animationLabel = new JLabel(new ImageIcon(canvas));
ActionListener animator = new ActionListener() {
int x = 0;
int y = 30;
#Override
public void actionPerformed(ActionEvent e) {
Graphics2D g = canvas.createGraphics();
g.setColor(new Color(55, 75, 125));
g.fillRect(0, 0, canvas.getWidth(), canvas.getHeight());
int offset = (x % bi.getWidth());
g.drawImage(bi, offset, y, null);
g.drawImage(bi, offset - bi.getWidth(), y, null);
g.setPaint(gp2);
g.fillRect(0, 0, canvas.getWidth(), canvas.getHeight());
g.dispose();
animationLabel.repaint();
x++;
}
};
Timer timer = new Timer(40, animator);
timer.start();
JOptionPane.showMessageDialog(null, animationLabel);
timer.stop();
}
};
// Swing GUIs should be created and updated on the EDT
// http://docs.oracle.com/javase/tutorial/uiswing/concurrency
SwingUtilities.invokeLater(r);
}
}
Here is a version of that image with the equator added (it is 44 pixels 'south' of the center of the image).
You're calling Thread.sleep(...) and likely on the EDT or Swing event thread (full name is the Event Dispatch Thread). This thread is responsible for all Swing painting/drawing and user interactions, and so sleeping it will only serve to freeze your entire GUI. Instead you should use a Swing Timer to allow you to swap a JLabel's ImageIcon.
So, briefly:
Don't call Thread.sleep(...) on the Swing event thread (Event Dispatch Thread or EDT).
Do use a Swing Timer to do your repeating delayed actions.
Don't make and add many JLabels. Just make and add one.
Do Swap the ImageIcon that the JLabel displays by calling setIcon(...) on the label.
Better (cleaner) to write if (busy == false) { as if (!busy) {
e.g.,
ImageIcon[] icons = {...}; // filled up with your ImageIcons
if (!busy) {
int timerDelay = 2000;
new Timer(timerDelay, new ActionListener() {
private int i = 0;
public void actionPerfomed(ActionEvent e) {
myLabel.setIcon(icons(i));
i++;
if (i == icons.length) {
((Timer)e.getSource).stop();
}
};
}).start();
}

How to implement oval GradientPaint?

We know that there are a class named RadialGradientPaint in Java and we can use it to have a gradient painting for circle.
But I want to have an oval (ellipse) GradientPaint. How to implement oval GradientPaint?
Use an AffineTransform when drawing the RadialGradientPaint. This would require a scale instance of the transform. It might end up looking something like this:
import java.awt.*;
import java.awt.MultipleGradientPaint.CycleMethod;
import java.awt.geom.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class OvalGradientPaint {
public static void main(String[] args) {
Runnable r = new Runnable() {
#Override
public void run() {
// the GUI as seen by the user (without frame)
JPanel gui = new JPanel(new BorderLayout());
gui.setBorder(new EmptyBorder(2, 3, 2, 3));
gui.add(new OvalGradientPaintSurface());
gui.setBackground(Color.WHITE);
JFrame f = new JFrame("Oval Gradient Paint");
f.add(gui);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// See http://stackoverflow.com/a/7143398/418556 for demo.
f.setLocationByPlatform(true);
// ensures the frame is the minimum size it needs to be
// in order display the components within it
f.pack();
// should be done last, to avoid flickering, moving,
// resizing artifacts.
f.setVisible(true);
}
};
// Swing GUIs should be created and updated on the EDT
// http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
SwingUtilities.invokeLater(r);
}
}
class OvalGradientPaintSurface extends JPanel {
public int yScale = 150;
public int increment = 1;
RadialGradientPaint paint;
AffineTransform moveToOrigin;
OvalGradientPaintSurface() {
Point2D center = new Point2D.Float(100, 100);
float radius = 90;
float[] dist = {0.05f, .95f};
Color[] colors = {Color.RED, Color.MAGENTA.darker()};
paint = new RadialGradientPaint(center, radius, dist, colors,CycleMethod.REFLECT);
moveToOrigin = AffineTransform.
getTranslateInstance(-100d, -100d);
ActionListener listener = new ActionListener() {
#Override
public void actionPerformed(ActionEvent ae) {
if (increment < 0) {
increment = (yScale < 50 ? -increment : increment);
} else {
increment = (yScale > 150 ? -increment : increment);
}
yScale += increment;
repaint();
}
};
Timer t = new Timer(15, listener);
t.start();
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
AffineTransform moveToCenter = AffineTransform.
getTranslateInstance(getWidth()/2d, getHeight()/2d);
g2.setPaint(paint);
double y = yScale/100d;
double x = 1/y;
AffineTransform at = AffineTransform.getScaleInstance(x, y);
// We need to move it to the origin, scale, and move back.
// Counterintutitively perhaps, we concatentate 'in reverse'.
moveToCenter.concatenate(at);
moveToCenter.concatenate(moveToOrigin);
g2.setTransform(moveToCenter);
// fudge factor of 3 here, to ensure the scaling of the transform
// does not leave edges unpainted.
g2.fillRect(-getWidth(), -getHeight(), getWidth()*3, getHeight()*3);
}
#Override
public Dimension getPreferredSize() {
return new Dimension(500, 200);
}
}
Original image: The original static (boring) 'screen shot' of the app.
RadialGradientPaint provides two ways to paint itself as an ellipse instead of a circle:
Upon construction, you can specify a transform for the gradient. For example, if you provide the following transform: AffineTransform.getScaleInstance(0.5, 1), your gradient will be an upright oval (the x dimension will be half that of the y dimension).
Or, you can use the constructor that requires a Rectangle2D be provided. An appropriate transform will be created to make the gradient ellipse bounds match that of the provided rectangle. I found the class documentation helpful: RadialGradientPaint API. In particular, see the documentation for this constructor.

java swing: in paintComponent method how to know what to repaint?

My component is bigger than the screen and parts of it are not shown (I will use scrollbars).
When I receive a call in paintComponent(g) how do I know what area should I paint?
I'm not sure if this is what you mean, but the problem is you will have to call repaint() on the JScrollPane each time you get a call in paintComponent(Graphics g) of the JPanel or else updates on the JPanel will not be visible in the JScrollPane.
Also I see you want to use JScrollBar (or maybe you confused the terminology)? I'd recommend a JScrollPane
I made a small example which is a JPanel with a grid that will change its colour every 2 seconds (Red to black and vice versa). The JPanel/Grid is larger then the JScrollPane; regardless we have to call repaint() on the JScrollPane instance or else the grid wont change colour:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
public class Test {
public static void main(String[] args) throws Exception {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new Test().createAndShowUI();
}
});
}
private void createAndShowUI() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
initComponents(frame);
frame.setPreferredSize(new Dimension(400, 400));
frame.pack();
frame.setVisible(true);
}
private void initComponents(JFrame frame) {
JScrollPane jsp = new JScrollPane();
jsp.setViewportView(new Panel(800, 800, jsp));
frame.getContentPane().add(jsp);
}
}
class Panel extends JPanel {
private int across, down;
private Panel.Tile[][] tiles;
private Color color = Color.black;
private final JScrollPane jScrollPane;
public Panel(int width, int height, JScrollPane jScrollPane) {
this.setPreferredSize(new Dimension(width, height));
this.jScrollPane = jScrollPane;
createTiles();
changePanelColorTimer();//just something to do to check if its repaints fine
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
for (int i = 0; i < across; i++) {
for (int j = 0; j < down; j++) {
g.setColor(color);
for (int k = 0; k < 5; k++) {
g.drawRect(tiles[i][j].x + k, tiles[i][j].y + k, tiles[i][j].side - k * 2, tiles[i][j].side - 2 * k);
}
}
}
updateScrollPane();//refresh the pane after every paint
}
//calls repaint on the scrollPane instance
private void updateScrollPane() {
jScrollPane.repaint();
}
private void createTiles() {
across = 13;
down = 9;
tiles = new Panel.Tile[across][down];
for (int i = 0; i < across; i++) {
for (int j = 0; j < down; j++) {
tiles[i][j] = new Panel.Tile((i * 50), (j * 50), 50);
}
}
}
//change the color of the grid lines from black to red and vice versa every 2s
private void changePanelColorTimer() {
Timer timer = new Timer(2000, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if (color == Color.black) {
color = Color.red;
} else {
color = Color.black;
}
}
});
timer.setInitialDelay(2000);
timer.start();
}
private class Tile {
int x, y, side;
public Tile(int inX, int inY, int inSide) {
x = inX;
y = inY;
side = inSide;
}
}
}
In the Panel class if we comment the line updateScrollPane(); in paintComponent(Graphics g) we wont see the grid change colour.
You can find out the area that actually has to be painted by querying the clip bounds of the Graphics object.
The JavaDoc seems to be a bit out-dated for this method: It says, that it may return a null clip. However, this is obviously never the case (and other Swing classes also rely on the clip never being null!).
The follwing MCVE illustrates the difference between using a the clip or painting the whole component:
It contains a JPanel with a size of 800x800 in a scroll pane. The panel paints a set of rectangles, and prints how many rectangles have been painted.
One can use the "Use clip bounds" checkbox to enable and disable using the clip. When the clip is used, only the visible area of the panel is repainted, and the number of rectangles is much lower. (Note that the test whether a rectangle has to be painted or not is rather simple here: It only performs an intersection test of the rectangle with the visible region. For a real application, one would directly use the clip bounds to find out which rectangles have to be painted).
This example also shows some of the tricky internals of scroll panes: When the blinking is switched off, and the scroll bars are moved, one can see that - although the whole visible area changes - only a tiny area actually has to be repainted (namely the area that has become visible due to the scrolling). The other part is simply moved as-it-is, by blitting the previous contents. This behavior can be modified with JViewport.html#setScrollMode.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
public class PaintRegionTest
{
public static void main(String[] args) throws Exception
{
SwingUtilities.invokeLater(new Runnable()
{
#Override
public void run()
{
createAndShowGUI();
}
});
}
private static void createAndShowGUI()
{
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final PaintRegionPanel paintRegionPanel = new PaintRegionPanel();
paintRegionPanel.setPreferredSize(new Dimension(800, 800));
final Timer timer = new Timer(1000, new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
paintRegionPanel.changeColor();
}
});
timer.setInitialDelay(1000);
timer.start();
JScrollPane scrollPane = new JScrollPane(paintRegionPanel);
frame.getContentPane().setLayout(new BorderLayout());
frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
JPanel controlPanel = new JPanel(new FlowLayout());
final JCheckBox blinkCheckbox = new JCheckBox("Blink", true);
blinkCheckbox.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
if (blinkCheckbox.isSelected())
{
timer.start();
}
else
{
timer.stop();
}
}
});
controlPanel.add(blinkCheckbox);
final JCheckBox useClipCheckbox = new JCheckBox("Use clip bounds");
useClipCheckbox.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
paintRegionPanel.setUseClipBounds(
useClipCheckbox.isSelected());
}
});
controlPanel.add(useClipCheckbox);
frame.getContentPane().add(controlPanel, BorderLayout.SOUTH);
frame.setPreferredSize(new Dimension(400, 400));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
class PaintRegionPanel extends JPanel
{
private Color color = Color.BLACK;
private boolean useClipBounds = false;
void setUseClipBounds(boolean useClipBounds)
{
this.useClipBounds = useClipBounds;
}
void changeColor()
{
if (color == Color.BLACK)
{
color = Color.RED;
}
else
{
color = Color.BLACK;
}
repaint();
}
#Override
protected void paintComponent(Graphics gr)
{
super.paintComponent(gr);
Graphics2D g = (Graphics2D)gr;
g.setColor(color);
Rectangle clipBounds = g.getClipBounds();
Rectangle ownBounds = new Rectangle(0,0,getWidth(),getHeight());
System.out.println("clipBounds: " + clipBounds);
System.out.println(" ownBounds: " + ownBounds);
Rectangle paintedRegion = null;
if (useClipBounds)
{
System.out.println("Using clipBounds");
paintedRegion = clipBounds;
}
else
{
System.out.println("Using ownBounds");
paintedRegion = ownBounds;
}
int counter = 0;
// This loop performs a a simple test see whether the objects
// have to be painted. In a real application, one would
// probably use the clip information to ONLY create the
// rectangles that actually have to be painted:
for (int x = 0; x < getWidth(); x += 20)
{
for (int y = 0; y < getHeight(); y += 20)
{
Rectangle r = new Rectangle(x + 5, y + 5, 10, 10);
if (r.intersects(paintedRegion))
{
g.fill(r);
counter++;
}
}
}
System.out.println("Painted "+counter+" rectangles ");
}
}
An aside: For many application cases, such an "optimization" should hardly be necessary. The painted elements are intersected against the clip anyhow, so one will probably not gain much performance. When "preparing" the elements to be painted is computationally expensive, one can consider this as one option. (In the example, "preparing" refers to creating the Rectangle instance, but there may be more complicated patterns). But in these cases, there may also be more elegant and easier solutions than manually checking the clip bounds.
All answers are wrong. So I decided to answer the question despide the fact that the question is two years old.
I believe that the correct answer is calling g.getClipBounds() inside of paintComponent(Graphics g) method. It will return the rectangle in the control's coordinate system of the area which is invalidated and must be redrawn.

Categories