import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class whiteBoard extends JFrame implements MouseMotionListener{
JPanel panel = new JPanel();
int x,y;
boolean repaint = false;
public whiteBoard() {
super();
setSize(600,600);
setTitle("WhiteBoard");
panel.setBackground(Color.white);
add(panel);
addMouseMotionListener(this);
setDefaultCloseOperation(EXIT_ON_CLOSE);
// super.setOpaque(false);
setVisible(true);
}
public static void main(String arg[]){
whiteBoard window = new whiteBoard();
}
public void mouseDragged(MouseEvent e){
x = e.getX();
y = e.getY();
repaint = !repaint;
repaint();
}
public void mouseMoved(MouseEvent me){
}
public void paint(Graphics g){
if(repaint){
g.setColor(Color.blue);
g.fillOval(x,y,10,10);
}
}
}
WhiteBoard - Paint program
Started by Khaotic, Jun 20 2010 09:15 AM
14 replies to this topic
#1
Posted 20 June 2010 - 09:15 AM
I've tried a paint program, but for some reason, the background isnt being set. It is taking on the background of whatever is under it. Like the backgroud isnt being set. Also, it isnt being updated immediately, so it's more like drawing dots....
Check out my site: www.khaoticirc.net
|
|
|
#2
Posted 20 June 2010 - 10:03 AM
If you override the paint method of something, the first line in the paint method should/could be super.paint(g). It causes all child components to be drawn (including the JPanel).
You will notice a problem though. As doing so, will cause the blue line/dots to be gone. as the super is repainted all the time.
A possible solution is creating a BufferedImage, painting the dots on the image. And in the overriden paintmethod, paint the bufferedimage every time.
Note that you are NOT painting on the panel, but on the JFrame. With the bufferedimage, the jpanel becomes totally useless.
Some pieces of code:
I didn't do super.paint(g) in the paint method, as it causes flickering with me...
Maybe it would be better to create another class which extends JPanel, and there you override the paint method.
You will notice a problem though. As doing so, will cause the blue line/dots to be gone. as the super is repainted all the time.
A possible solution is creating a BufferedImage, painting the dots on the image. And in the overriden paintmethod, paint the bufferedimage every time.
Note that you are NOT painting on the panel, but on the JFrame. With the bufferedimage, the jpanel becomes totally useless.
Some pieces of code:
img = new BufferedImage(600,600, BufferedImage.TYPE_INT_RGB);
Graphics g = img.getGraphics();
g.setColor(Color.white);
g.fillRect(0,0,600,600);
Graphics g = img.getGraphics();
g.setColor(Color.blue);
g.fillOval(x,y,10,10);
@Override
public void paint(Graphics g) {
g.drawImage(img, 0,0,null);
}
I didn't do super.paint(g) in the paint method, as it causes flickering with me...
Maybe it would be better to create another class which extends JPanel, and there you override the paint method.
#3
Posted 20 June 2010 - 11:39 AM
So I am kinda confused. Do I need to still do the repaint?
Check out my site: www.khaoticirc.net
#4
Posted 20 June 2010 - 11:45 AM
It is required almost always. Unless you paint every child component manually in the public void paint(Graphics g)
It will become required if you put something else than the image on the JFrame.
In the code (yours that i changed a little) I now add a JLabel to the BorderLayout.SOUTH of the JFrame.
If you do not call super.paint(g) in the overridden public void paint(Graphics g) It will not display the label as it isn't drawn anymore. The only thing you currently draw is the image.
If it is called, it wil draw the JLabel.
That's the reason it didn't set the background of the JPanel with your code. In fact, it didn't paint the JPanel at all. It normally gets painted in the paint method from the JFrame, but because you override the paint method of the JFrame, it uses your code instead of its default. Unless you do super.paint(g) ofcourse.
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class whiteBoard extends JFrame implements MouseMotionListener {
JPanel panel = new JPanel();
int x,y;
boolean repaint = false;
BufferedImage img;
Timer timer;
public whiteBoard() {
super();
img = new BufferedImage(600,600, BufferedImage.TYPE_INT_RGB);
Graphics g = img.getGraphics();
g.setColor(Color.white);
g.fillRect(0,0,600,600);
setSize(600,800);
setTitle("WhiteBoard");
add(panel);
JLabel label = new JLabel("Test");
add(label, BorderLayout.SOUTH);
addMouseMotionListener(this);
setDefaultCloseOperation(EXIT_ON_CLOSE);
// super.setOpaque(false);
setVisible(true);
}
public static void main(String arg[]){
whiteBoard window = new whiteBoard();
}
public void mouseDragged(MouseEvent e){
x = e.getX();
y = e.getY();
makeCircle();
repaint();
}
private void makeCircle() {
Graphics g = img.getGraphics();
g.setColor(Color.blue);
g.fillOval(x,y,10,10);
}
public void mouseMoved(MouseEvent me){
}
@Override
public void paint(Graphics g) {
g.drawImage(img, 0,0,null);
}
}
It will become required if you put something else than the image on the JFrame.
In the code (yours that i changed a little) I now add a JLabel to the BorderLayout.SOUTH of the JFrame.
If you do not call super.paint(g) in the overridden public void paint(Graphics g) It will not display the label as it isn't drawn anymore. The only thing you currently draw is the image.
If it is called, it wil draw the JLabel.
That's the reason it didn't set the background of the JPanel with your code. In fact, it didn't paint the JPanel at all. It normally gets painted in the paint method from the JFrame, but because you override the paint method of the JFrame, it uses your code instead of its default. Unless you do super.paint(g) ofcourse.
#5
Posted 21 June 2010 - 11:40 AM
#6
Posted 21 June 2010 - 11:50 AM
skip?
#7
Posted 21 June 2010 - 12:04 PM
#8
Posted 21 June 2010 - 12:09 PM
By the way, what does overriding the paint message actually do?
Check out my site: www.khaoticirc.net
#9
Posted 21 June 2010 - 12:11 PM
Because you move your mouse to fast :D
No seriously, the mousemotion just can't respond fast enough to keep up with the mouse movement.
A workaround is to store the position of the mouse: x1, y1
The next time the mouse is moved, you notice that x1 and y1 is not null so you remember position x2, y2.
In the paint method, instead of drawing dots, you draw lines from x1,y1 to x2,y2 (when they are not null). Then set x1,y1,x2,y2 to null and it can be repeated.
To adjust the thickness of the line:
Graphics2D g2 = (Graphics2D) g;
g2.setStroke(new BasicStroke(10)); --> 10 thickness in pixels i think.
Then use g2 as you would use g.
paint 'message'?
No seriously, the mousemotion just can't respond fast enough to keep up with the mouse movement.
A workaround is to store the position of the mouse: x1, y1
The next time the mouse is moved, you notice that x1 and y1 is not null so you remember position x2, y2.
In the paint method, instead of drawing dots, you draw lines from x1,y1 to x2,y2 (when they are not null). Then set x1,y1,x2,y2 to null and it can be repeated.
To adjust the thickness of the line:
Graphics2D g2 = (Graphics2D) g;
g2.setStroke(new BasicStroke(10)); --> 10 thickness in pixels i think.
Then use g2 as you would use g.
paint 'message'?
#10
Posted 21 June 2010 - 12:15 PM
#11
Posted 21 June 2010 - 12:20 PM
It puts you in control of what is drawn on the screen. If you have a JFrame and you override its paint method and leave it totally empty, it will display an empty frame. Even if you added labels, buttons or anything else to it.
Whatever you draw to the Graphics g, will be drawn on the screen.
Whatever you draw to the Graphics g, will be drawn on the screen.
#12
Posted 21 June 2010 - 12:28 PM
how is the overridden way different from the normal way?
Check out my site: www.khaoticirc.net


Sign In
Create Account


Back to top









