Jump to content

WhiteBoard - Paint program

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
14 replies to this topic

#1
Khaotic

Khaotic

    Learning Programmer

  • Members
  • PipPipPip
  • 62 posts
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....

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);
        }
    }
}

Check out my site: www.khaoticirc.net

#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
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:

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
Khaotic

Khaotic

    Learning Programmer

  • Members
  • PipPipPip
  • 62 posts
So I am kinda confused. Do I need to still do the repaint?
Check out my site: www.khaoticirc.net

#4
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
It is required almost always. Unless you paint every child component manually in the public void paint(Graphics g)
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
Khaotic

Khaotic

    Learning Programmer

  • Members
  • PipPipPip
  • 62 posts
Do you know why the dots still skip?
Check out my site: www.khaoticirc.net

#6
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
skip?

#7
Khaotic

Khaotic

    Learning Programmer

  • Members
  • PipPipPip
  • 62 posts
makes dots rather than a constant drag
Check out my site: www.khaoticirc.net

#8
Khaotic

Khaotic

    Learning Programmer

  • Members
  • PipPipPip
  • 62 posts
By the way, what does overriding the paint message actually do?
Check out my site: www.khaoticirc.net

#9
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
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'?

#10
Khaotic

Khaotic

    Learning Programmer

  • Members
  • PipPipPip
  • 62 posts
Paint method* Sorry
Check out my site: www.khaoticirc.net

#11
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
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.

#12
Khaotic

Khaotic

    Learning Programmer

  • Members
  • PipPipPip
  • 62 posts
how is the overridden way different from the normal way?
Check out my site: www.khaoticirc.net