How to "do something" on Swing component resizing? - java

I've a class which extends JPanel. I overwrote protected void paintComponent(Graphics g).
There is a variable which has to be recalculated when the panel's dimensions change. How do I do that in a proper way?

Like Adam Paynter suggested, you can also add an inner class to your code, like this:
class ResizeListener extends ComponentAdapter {
public void componentResized(ComponentEvent e) {
// Recalculate the variable you mentioned
}
}
The code you have entered between the innermost brackets will be executed everytime the component get resized.
Then you add this listener to your component with
myJPanel.addComponentListener(new ResizeListener());
You can get your component by using e.getComponent(). This way you can call any method of your component from inside the inner class like
e.getComponent().getWeight();

I suppose you could override the various setSize and resize methods and perform the calculation there. However, you may not find all the places where the size can be changed. You may want to have your class implement ComponentListener and simply listen to itself for resize events.
Warning: I am not a Swing expert.
Warning: I have not compiled this code.
public class MyJPanel extends JPanel implements ComponentListener {
public MyJPanel() {
this.addComponentListener(this);
}
public void paintComponent(Graphics g) {
// Paint, paint, paint...
}
public void componentResized(ComponentEvent e) {
// Perform calculation here
}
public void componentHidden(ComponentEvent e) {}
public void componentMoved(ComponentEvent e) {}
public void componentShown(ComponentEvent e) {}
}

If I understand the question correctly then you should read the section from the Swing tutorial on How to Write a Component Listener which shows you how to listen for a change in a components size.

If the calculation isn't time consuming, I would just re-calculate the value each time in paintComponent().
Otherwise, you can save a value that is the size of the component and check it against the new size in paintComponent. If the size changed, then recalculate, otherwise don't.
private Dimension size;
protected void paintComponent(Graphics g){
if (!size.equals(getSize())){
size = getSize();
// recalculate value
}
}
Or, you can do the calculation on a resize event.
//in the constructor add the line
addComponentListener(resizeListener);
private ComponentListener resizeListener = new ComponentAdapter(){
public void componentResized(ActionEvent e){
// recalculate value
}
};

The simplest way is to implement a ComponentListener:
myjpanel.addComponentListener(new ComponentAdapter() {
#Override
public void componentResized(ComponentEvent e) {
//recalculate variable
}
});
Here, I have used a ComponentAdapter because I only intend on overriding componentResized().

Here's what I use (where CoordinatePlane is a JPanel):
I'm not an expert
public CoordinatePlane() {
setBackground(Color.WHITE);
this.addComponentListener(new ComponentAdapter(){
public void componentResized(ComponentEvent e) {
//YOUR CODE HERE
}
});
}

It resizes automatically if it's
inside a BorderLayout panel and
put there as BorderLayout.CENTER component.
If it doesn't work, you probably have forgotten one of these two.

This simple example is drawing a red circle in the resized frame....
import java.awt.*;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import javax.swing.*;
import java.awt.geom.*;
public class RedCircle extends JFrame implements ComponentListener {
int getWidth;
int getHeight;
public RedCircle() {
super("Red Circle");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addComponentListener(this);
pack();
setVisible(true);
}
public void componentResized(ComponentEvent e) {
getWidth = e.getComponent().getWidth();
getHeight = e.getComponent().getHeight();
Panel pane = new Panel(getWidth,getHeight);
add(pane);
}
public static void main(String[] args) {
RedCircle rc = new RedCircle();
}
public void componentMoved(ComponentEvent e) {
}
public void componentShown(ComponentEvent e) {
}
public void componentHidden(ComponentEvent e) {
}
}
class Panel extends JPanel {
int panelWidth;
int panelHeight;
public Panel(Integer getWidth,Integer getHeight) {
panelWidth = getWidth;
panelHeight = getHeight;
}
public void paintComponent(Graphics comp) {
super.paintComponent(comp);
Graphics2D comp2D = (Graphics2D) comp;
int realWidth = panelWidth - 17;
int realHeight = panelHeight - 40;
float Height = (realHeight);
float Width = (realWidth);
// draw the Red Circle
comp2D.setColor(Color.red);
Ellipse2D.Float redCircle = new Ellipse2D.Float(0F, 0F, Width, Height);
comp2D.fill(redCircle);
}
}

Related

Drawing lines on canvas using Mouse without Swing in java

My Quesition is similar to this question
Drawing lines with mouse on canvas : Java awt
My problem is that When the windows is minimized and maximized , the drawn lines are gone everytime
But my working is quite different because i used only awt components and without swing.
import java.awt.*;
import java.awt.event.*;
class Drawing extends WindowAdapter implements MouseMotionListener, MouseListener, ComponentListener {
Frame f;
Canvas c;
int X=400,Y=400;
int px=-1,py=-1;
int x,y;
public Drawing() {
f=new Frame("Drawing - Canvas");
f.addWindowListener(this);
f.addComponentListener(this);
f.setSize(X,Y);
c=new Canvas();
f.add(c);
c.addMouseMotionListener(this);
c.addMouseListener(this);
f.setVisible(true);
}
public void componentResized(ComponentEvent e) {}
public void componentHidden(ComponentEvent e) {}
public void componentMoved(ComponentEvent e) {}
public void componentShown(ComponentEvent e) {}
public void mouseMoved(MouseEvent e) {}
public void mouseClicked(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseDragged(MouseEvent e) {
int x,y;
x=e.getX();
y=e.getY();
Graphics g=c.getGraphics();
if(px!=-1) {
g.drawLine(px,py,x,y);
}
else {
g.drawLine(x,y,x,y);
}
px=x;
py=y;
}
public void mouseReleased(MouseEvent e) {
this.X=400; this.Y=400;
this.px=-1; this.py=-1;
}
public void windowClosing(WindowEvent e) {
f.dispose();
}
public static void main(String[] args) {
Drawing c=new Drawing();
}
}
Can someone help me out on these problems ?
getGraphics is NEVER the write way to perform custom paint.
Start by having a read through Painting in AWT and Swing and Performing Custom Painting for a better understanding how painting works and how you should work with it.
The simple answer is, you need to maintain a model of what has been painted, so that on each paint pass you can re-draw it.
For example Resize the panel without revalidation, Draw trail of circles with mouseDragged, Why is my line not drawing?

How to use the repaint method in Java Swing

I am very new to the Graphics portion of Java. I have created a frame and on it I have added a panel whose color has been set to Green. Now on clicking that panel I want to draw a circle using a test class's object called Mypanel. But it does not happen. Please guide !
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
class Mypanel extends JPanel
{
#Override
public void paintComponent(Graphics g)
{
g.drawOval(15, 15, 5, 5);
}
}
public class algo extends javax.swing.JFrame {
public algo() {
initComponents();
jPanel1.setBackground(Color.GREEN);
}
Mypanel p = new Mypanel() ;
private void jPanel1MouseClicked(java.awt.event.MouseEvent evt) {
p.repaint();
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new algo().setVisible(true);
}
});
}
}
If I were to guess I would say that I am not supposed to use the repaint method, but I was told that this was to be used.
That code as supplied would not compile. For better help sooner, post a Minimal, Complete, and Verifiable example or Short, Self Contained, Correct Example.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Mypanel extends JPanel {
boolean clicked = false;
Mypanel() {
setBackground(Color.GREEN);
MouseListener mouseListener = new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
clicked = true;
repaint();
}
};
this.addMouseListener(mouseListener);
}
#Override
public Dimension getPreferredSize() {
return new Dimension(400, 100);
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (clicked) {
g.drawOval(15, 15, 50, 50);
}
}
}
public class algo extends JFrame {
public algo() {
initComponents();
pack();
//jPanel1.setBackground(Color.GREEN); ?!?
}
protected final void initComponents() {
add(new Mypanel());
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new algo().setVisible(true);
}
});
}
}
There are a few things to correct in your example...
When you create the frame (i.e. in the constructor) you'll want to call super(). This is the first thing the constructor has to do. Then, you'll probably want to set an initial width/height, and set the background color of the frame green.
You need to add a mouse listener so that the mouseClicked method is actually called. Then have it add the 'MyPanel' object to the frame, and call repaint.
I think that's roughly what you're going for.

Why doesn't the ball show up in the frame if I add the ball after the for loop?

The program makes a ball glide across from top left to bottom right and works. But if I were to shift the line
frame.getContentPane().add(ball);
from its current position to after the for loop, why doesn't the ball show up on the frame.
I agree that the ball should no longer move, because all the shifting done in the for loop happens even before I add the ball to the JFrame,but I don't understand why the ball doesn't show up on the screen when I ultimately add it to the frame.
Here's the code of the working program, if you shift the line mentioned above to after the for loop, the ball no longer shows up
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Animate
{
private JFrame frame;
private int x,y;
public static void main(String args[])
{
Animate ballRoll = new Animate();
ballRoll.go();
}
public void go()
{
frame = new JFrame();
frame.setSize(500,500);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MyRoll ball = new MyRoll();
frame.getContentPane().add(ball);
for(x = 5;x<=350;x++)
{
y=x;
try
{
Thread.sleep(50);
}
catch(Exception e)
{
System.out.println("dsfsd");
}
ball.repaint();
}
}
class MyRoll extends JPanel
{
public void paintComponent(Graphics g)
{
g.setColor(Color.BLACK);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
g.setColor(Color.ORANGE);
g.fillOval(x, y, 100, 100);
}
}
}
Some points to remember:
Don't use Thread.sleep() that sometime hangs the whole swing application instead try with Swing Timer that is most suitable for swing application.
Read more How to Use Swing Timers
Don't forget to call super.paintComponent() in overridden paintComponent() method.
Call frame.setVisible(true) in the end after adding all the components.
Use frame.pack() instead of frame.setSize(500,500) that fits the components as per component's preferred size.
Override getPreferredSize() to set the preferred size of the JPanel in case of custom painting.
Use SwingUtilities.invokeLater() or EventQueue.invokeLater() to make sure that EDT is initialized properly.
Read more
Why to use SwingUtilities.invokeLater in main method?
SwingUtilities.invokeLater
Should we use EventQueue.invokeLater for any GUI update in a Java desktop application?
Sample code: (change it as per your custom painting)
private Timer timer;
...
timer = new javax.swing.Timer(50, new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
y = ++x;
ball.repaint();
if (x > 350) {
timer.stop();
}
}
});
timer.setRepeats(true);
timer.start();
public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
Animate ballRoll = new Animate();
ballRoll.go();
}
});
}
class MyRoll extends JPanel {
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
...
}
#Override
public Dimension getPreferredSize() {
return new Dimension(..., ...);
}
}

Java (Swing): MouseMoved not working at all

This is driving me nuts. It must be an extremely simple problem, but I can't possibly see it.
Basically mouseMoved is NEVER called. Below is the code.
public class MouseMotionThing {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
JFrame jFrame = new JFrame();
jFrame.setContentPane(new ContentPane());
jFrame.setSize(400, 400);
jFrame.setVisible(true);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
});
}
}
And my panel. This implementation uses a MouseInputListener:
class ContentPane extends JPanel implements MouseInputListener {
int x = 0, y = 0;
public ContentPane() {
setOpaque(true);
addMouseListener(this);
addMouseMotionListener(this);
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawLine(0, 0, x, y);
}
#Override
public void mouseDragged(MouseEvent e) {
System.out.println("Mouse Dragged!");
}
#Override
public void mouseMoved(MouseEvent e) {
System.out.println("CALLED MOUSE MOVED");
x = e.getX();
y = e.getY();
repaint();
}
#Override
public void mouseClicked(MouseEvent e) {
}
#Override
public void mousePressed(MouseEvent e) {
}
#Override
public void mouseReleased(MouseEvent e) {
}
#Override
public void mouseEntered(MouseEvent e) {
}
#Override
public void mouseExited(MouseEvent e) {
}
}
whereas this alternative implementation uses only a MouseMotionListener:
class ContentPane extends JPanel implements MouseMotionListener {
int x = 0, y = 0;
public ContentPane() {
setOpaque(true);
addMouseMotionListener(this);
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawLine(0, 0, x, y);
}
#Override
public void mouseDragged(MouseEvent e) {
System.out.println("Mouse Dragged!");
}
#Override
public void mouseMoved(MouseEvent e) {
System.out.println("CALLED MOUSE MOVED");
x = e.getX();
y = e.getY();
repaint();
}
}
In none of the above alternative implementations is mouseMoved EVER called. I'm adding the right listeners every single time, but it's simply not working. mouseDragged works fine though. What am I missing?
LE: I tested the code on Ubuntu 12.10 & JDK 7, worked fine. Then when I went back to my W8 machine, it started working. I did absolutely nothing more than restart my laptop. I couldn't reproduce the problem nor track it down, but I will come back if I get it again and manage to find something.
It's not clear where things may have gone awry; it may help to do a full build. For reference, I've re-factored your sscce to use a MouseAdapter and remove a leaking this from the JPanel.
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class MouseMotionThing {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
JFrame jFrame = new JFrame();
jFrame.add(new MousePanel());
jFrame.pack();
jFrame.setSize(400, 400);
jFrame.setVisible(true);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
});
}
private static class MousePanel extends JPanel {
Point p = new Point();
public MousePanel() {
setOpaque(true);
addMouseMotionListener(new MouseHandler());
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawLine(0, 0, p.x, p.y);
}
private class MouseHandler extends MouseAdapter {
#Override
public void mouseDragged(MouseEvent e) {
update(e);
}
#Override
public void mouseMoved(MouseEvent e) {
update(e);
}
private void update(MouseEvent e) {
System.out.println(e.paramString());
MousePanel.this.p = e.getPoint();
MousePanel.this.repaint();
}
}
}
}
I happened to come across a similar issue in one of the applications I contribute to. I couldn't find anything else that pointed to the root cause, so I started doing some Java AWT logging as illustrated on Oracle's Logging Overview page.
After analyzing parts of the file, I found that there were 5 mouse buttons being registered to the JVM:
<record>
<date>2015-06-18T15:45:54</date>
<millis>1434656754395</millis>
<sequence>70</sequence>
<logger>sun.awt.windows.WDesktopProperties</logger>
<level>FINE</level>
<class>sun.awt.windows.WDesktopProperties</class>
<method>setIntegerProperty</method>
<thread>1</thread>
<message>awt.mouse.numButtons=5</message>
</record>
I also realized that other mouse events (MOUSE_ENTERED, MOUSE_EXITED, MOUSE_DRAGGED) had a extModifiers value of Button5, which is weird because I was just using a touch pad and no other button is pressed. I'm not sure if this would prevent the MOUSE_MOVED event from being fired - I'm guessing this would take some research into lower level Java.
<record>
<date>2015-06-18T15:45:55</date>
<millis>1434656755026</millis>
<sequence>329</sequence>
<logger>java.awt.event.EventDispatchThread</logger>
<level>FINEST</level>
<class>java.awt.EventDispatchThread</class>
<method>pumpOneEventForFilters</method>
<thread>13</thread>
<message>Dispatching: java.awt.event.MouseEvent[MOUSE_ENTERED,(388,387),absolute(388,387),button=0,extModifiers=Button5,clickCount=0] on frame0</message>
</record>
I did some research on extra mouse buttons and found another Oracle page talking about Desktop Properties. I changed the startup parameters of Java to include -Dsun.awt.enableExtraMouseButtons=false, and voila, my application started working again.
I tested a very simple Java application with listening to MOUSE_MOVED on a variety of Windows JRE's, and they all exhibited the same issue when not setting this parameter.
If extra mouse buttons aren't going to be used in your application, then this may be a workaround for you.
Well, it seemed that I had those problems when I used my A4TECH G10-770F mouse on Windows 8. If I turned it off, things went back to normal. I didn't look into it in greater detail though - I already had another mouse and used that one instead.

repaint() not called when running app from Eclipse?

I never get "paint" written to my command line window when I use Eclipse and Run->cmd to run the program. It works fine if I run System.out.print() from paintComponent in another program. Someone who can help?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GUI extends JPanel implements KeyListener, ActionListener
{
private static final long serialVersionUID = 1L;
JFrame frmMain = new JFrame("Kodning");
JTextField text = new JTextField();
JPanel pan = new JPanel();
static char bokstav;
static int x=10, y=80;
boolean convert = false;
String s;
Timer t = new Timer(10, this);
public static void main(String[] args)
{
#SuppressWarnings("unused")
GUI g = new GUI();
}
public GUI()
{
frmMain.setSize(600, 120);
frmMain.setLayout(new GridLayout(2, 1));
frmMain.addWindowListener(hornStang());
frmMain.add(text);
frmMain.add(pan);
frmMain.setFocusable(true);
frmMain.setVisible(true);
frmMain.addKeyListener(this);
text.addKeyListener(this);
pan.addKeyListener(this);
t.start();
}
private static WindowAdapter hornStang()
{
return new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
};
}
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()== KeyEvent.VK_ENTER)
{
System.out.println("dechifrera");
repaint();
deshiffrera(text.getText());
}
}
public void keyReleased(KeyEvent arg0){}
public void keyTyped(KeyEvent arg0){}
public void deshiffrera(String s)
{
s = this.s;
repaint();
}
#override
public void paintComponent(Graphics g)
{
System.out.println("paint");
for(int i=0;i<s.length();i++)
{
bokstav = s.charAt(i);
switch (bokstav)
{
case 'a':nere(g); hoger(g); prick(g, 0); break;
//en massa case
default:break;
}
x=x+12;
}
}
#Override
public void actionPerformed(ActionEvent e)
{
repaint();
}
}
The component must be added to a visible window/frame/component for it's paintComponent to be called.
GUI is only added as a KeyListener but is neither added to the JFrame, nor any other visible component in the code above. There is no reason for calling paintComponent since the component is not being displayed at all.
There are a number of issues with your code:
Your GUI panel is not in the frame (shouldn't it be added instead of pan?)
String s is uninitialized, which causes a NullPointerException
paint should be overridden instead of paintComponents
paint should not change the state of the component, because it can be called any time.
etc...
You probably miss the output of "System.out.println("paint");" ?
GUI-Apps under Windows cant write to the console (they dont have a console, because it would suck if every GUI-App would also open a black window).
There are two java-interpreters under windows: "javaw.exe" which is a GUI-App and silently discards any System.out-writes. And "java.exe" which is a console-app and allows writing to the console. Try to start your program with "java.exe"
I use this with AWT (not 100% sure whether it's working in Swing too...)
Graphics g = _yourcomponent_.getGraphics();
if (g != null) {
_yourcomponent_.paint(g);
// below the estimated code for Swing:
_yourcomponent_.paintComponent(g);
}

Categories