9 Replies - 1332 Views - Last Post: 22 December 2010 - 05:20 PM Rate Topic: -----

#1 giggly kisses  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 80
  • View blog
  • Posts: 391
  • Joined: 29-March 09

paint program

Posted 22 December 2010 - 03:13 PM

I'm working on a paint program just to see if I can do it. I'm having trouble having it actually paint though. I can get it to drag a circle around but it doesn't keep the old circles, it just moves the circle around. I want it to make a trail of circles but I can't figure out how. Heres my code:

paint class:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Paint extends JFrame implements ActionListener, MouseListener, MouseMotionListener {
	private final int WIDTH = 500, HEIGHT = 400;
	private PaintPanel paintPanel;
	private JPanel buttonPanel;
	private JButton paintButton, eraseButton, drawButton, greenButton, redButton, yellowButton,
					blueButton, blackButton, whiteButton, purpleButton;
	private Icon paintIcon, eraseIcon, drawIcon;
	private JMenuBar menuBar;
	private JMenu fileMenu, editMenu, helpMenu;
	private JMenuItem newItem, openItem, saveItem, exitItem;
	private boolean drag = false;
	
	public Paint() {
		setTitle("Paint");
		setSize(HEIGHT, WIDTH);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLayout(new BorderLayout());
		
		buildMenu();
		buildCanvasPanel();
		buildButtonPanel();
		
		paintPanel.addMouseListener(this);
		paintPanel.addMouseMotionListener(this);
		paintButton.addActionListener(this);
		eraseButton.addActionListener(this);
		drawButton.addActionListener(this);
		exitItem.addActionListener(this);
		
		setVisible(true);
	}
	
	public void buildCanvasPanel() {
		paintPanel = new PaintPanel();
		paintPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
		paintPanel.setPreferredSize(new Dimension(400, 0));
		paintPanel.repaint();
		
		add(paintPanel, BorderLayout.CENTER);
	}
	
	public void buildMenu() {
		menuBar = new JMenuBar();
		fileMenu = new JMenu("File");
		editMenu = new JMenu("Edit");
		helpMenu = new JMenu("Help");
		newItem = new JMenuItem("New");
		openItem = new JMenuItem("Open");
		saveItem = new JMenuItem("Save");
		exitItem = new JMenuItem("Exit");
		
		fileMenu.add(newItem);
		fileMenu.add(openItem);
		fileMenu.add(saveItem);
		fileMenu.addSeparator();
		fileMenu.add(exitItem);
		
		menuBar.add(fileMenu);
		menuBar.add(editMenu);
		menuBar.add(helpMenu);
		
		setJMenuBar(menuBar);
	}
	
	public void buildButtonPanel() {
		buttonPanel = new JPanel();
		
		paintButton = new JButton();
		eraseButton = new JButton();
		drawButton = new JButton();
		
		paintIcon = new ImageIcon("paintbrush.png");
		eraseIcon = new ImageIcon("erase.png");
		drawIcon = new ImageIcon("pencil.png");
		
		paintButton.setIcon(paintIcon);
		eraseButton.setIcon(eraseIcon);
		drawButton.setIcon(drawIcon);
		
		buttonPanel.setBackground(Color.BLACK);
		buttonPanel.add(paintButton);
		buttonPanel.add(eraseButton);
		buttonPanel.add(drawButton);
		
		buttonPanel.setPreferredSize(new Dimension(50, 10));
		add(buttonPanel, BorderLayout.WEST);
	}
	
	public void buildColorPanel() {
		
	}
	
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == paintButton) {
			paintButton.setEnabled(false);
			eraseButton.setEnabled(true);
			drawButton.setEnabled(true);
		}
		
		if(e.getSource() == eraseButton) {
			eraseButton.setEnabled(false);
			paintButton.setEnabled(true);
			drawButton.setEnabled(true);
		}
		
		if(e.getSource() == drawButton) {
			drawButton.setEnabled(false);
			eraseButton.setEnabled(true);
			paintButton.setEnabled(true);
		}
		
		if(e.getSource() == exitItem) {
			System.exit(0);
		}
	}
	
	public void mouseClicked(MouseEvent e) {
		
	}

	public void mouseEntered(MouseEvent e) {
		
	}

	public void mouseExited(MouseEvent e) {
		
	}

	public void mousePressed(MouseEvent e) {
		System.out.println("A");
		paintPanel.setX(e.getX());
		paintPanel.setY(e.getY());
		paintPanel.repaint();
		drag = true;
	}

	public void mouseReleased(MouseEvent e) {
		drag = false;
	}
	
	public void mouseDragged(MouseEvent e) {
		paintPanel.setX(e.getX());
		paintPanel.setY(e.getY());
		
		
		//if(drag == true) {
			paintPanel.repaint();
		//}
	}

	public void mouseMoved(MouseEvent e) {
		
	}
	
	public static void main(String[] args) {
		new Paint();
	}
}




paintPanel class:
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class PaintPanel extends JPanel {
	private int x = 0, y = 0;
	
	public PaintPanel() {
		super();
		setBackground(Color.WHITE);
	}
	
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		
		g.fillOval(x, y, 10, 10);
	}
	
	public void setX(int x) {
		this.x = x;
	}
	
	public void setY(int y) {
		this.y = y;
	}
}




Is This A Good Question/Topic? 0
  • +

Replies To: paint program

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: paint program

Posted 22 December 2010 - 03:19 PM

In your paintComponent() method do not call

super.paintComponent(g);

what the father paintComponent() does is to erase the background and fill it with the registered background color
Was This Post Helpful? 1
  • +
  • -

#3 giggly kisses  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 80
  • View blog
  • Posts: 391
  • Joined: 29-March 09

Re: paint program

Posted 22 December 2010 - 03:40 PM

Thanks a lot!
Was This Post Helpful? 0
  • +
  • -

#4 giggly kisses  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 80
  • View blog
  • Posts: 391
  • Joined: 29-March 09

Re: paint program

Posted 22 December 2010 - 04:18 PM

I'm trying to change the color of the PaintPanel I made and I'm using the setBackground() method and its not working. Heres the updated code:


Paint code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Paint extends JFrame implements ActionListener, MouseListener, MouseMotionListener {
	private final int WIDTH = 500, HEIGHT = 400;
	private PaintPanel paintPanel;
	private JPanel buttonPanel;
	private JButton paintButton, eraseButton, drawButton, greenButton, redButton, yellowButton,
					blueButton, blackButton, whiteButton, purpleButton;
	private Icon paintIcon, eraseIcon, drawIcon;
	private JMenuBar menuBar;
	private JMenu fileMenu, editMenu, helpMenu;
	private JMenuItem newItem, openItem, saveItem, exitItem;
	private boolean drag = false;
	
	public Paint() {
		setTitle("Paint");
		setSize(HEIGHT, WIDTH);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLayout(new BorderLayout());
		
		buildMenu();
		buildCanvasPanel();
		buildButtonPanel();
		
		paintPanel.addMouseListener(this);
		paintPanel.addMouseMotionListener(this);
		paintButton.addActionListener(this);
		eraseButton.addActionListener(this);
		drawButton.addActionListener(this);
		exitItem.addActionListener(this);
		
		setVisible(true);
	}
	
	public void buildCanvasPanel() {
		paintPanel = new PaintPanel();
		paintPanel.setBackground(Color.WHITE);
		paintPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
		paintPanel.setPreferredSize(new Dimension(400, 0));
		paintPanel.repaint();
		
		add(paintPanel, BorderLayout.CENTER);
	}
	
	public void buildMenu() {
		menuBar = new JMenuBar();
		fileMenu = new JMenu("File");
		editMenu = new JMenu("Edit");
		helpMenu = new JMenu("Help");
		newItem = new JMenuItem("New");
		openItem = new JMenuItem("Open");
		saveItem = new JMenuItem("Save");
		exitItem = new JMenuItem("Exit");
		
		fileMenu.add(newItem);
		fileMenu.add(openItem);
		fileMenu.add(saveItem);
		fileMenu.addSeparator();
		fileMenu.add(exitItem);
		
		menuBar.add(fileMenu);
		menuBar.add(editMenu);
		menuBar.add(helpMenu);
		
		setJMenuBar(menuBar);
	}
	
	public void buildButtonPanel() {
		buttonPanel = new JPanel();
		
		paintButton = new JButton();
		eraseButton = new JButton();
		drawButton = new JButton();
		
		paintIcon = new ImageIcon("paintbrush.png");
		eraseIcon = new ImageIcon("erase.png");
		drawIcon = new ImageIcon("pencil.png");
		
		paintButton.setIcon(paintIcon);
		eraseButton.setIcon(eraseIcon);
		drawButton.setIcon(drawIcon);
		
		buttonPanel.setBackground(Color.BLACK);
		buttonPanel.add(paintButton);
		buttonPanel.add(eraseButton);
		buttonPanel.add(drawButton);
		
		buttonPanel.setPreferredSize(new Dimension(50, 10));
		add(buttonPanel, BorderLayout.WEST);
	}
	
	public void buildColorPanel() {
		
	}
	
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == paintButton) {
			paintButton.setEnabled(false);
			eraseButton.setEnabled(true);
			drawButton.setEnabled(true);
			
			paintPanel.buttonState("paint");
		}
		
		if(e.getSource() == eraseButton) {
			eraseButton.setEnabled(false);
			paintButton.setEnabled(true);
			drawButton.setEnabled(true);
			
			paintPanel.buttonState("erase");
		}
		
		if(e.getSource() == drawButton) {
			drawButton.setEnabled(false);
			eraseButton.setEnabled(true);
			paintButton.setEnabled(true);
			
			paintPanel.buttonState("draw");
		}
		
		if(e.getSource() == exitItem) {
			System.exit(0);
		}
	}
	
	public void mouseClicked(MouseEvent e) {
		
	}

	public void mouseEntered(MouseEvent e) {
		
	}

	public void mouseExited(MouseEvent e) {
		
	}

	public void mousePressed(MouseEvent e) {
		System.out.println("A");
		paintPanel.setX(e.getX());
		paintPanel.setY(e.getY());
		paintPanel.repaint();
		drag = true;
	}

	public void mouseReleased(MouseEvent e) {
		drag = false;
	}
	
	public void mouseDragged(MouseEvent e) {
		paintPanel.setX(e.getX());
		paintPanel.setY(e.getY());
		
		
		if(drag == true) {
			paintPanel.repaint();
		}
	}

	public void mouseMoved(MouseEvent e) {
		
	}
	
	public static void main(String[] args) {
		new Paint();
	}
}





PaintPanel class:
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class PaintPanel extends JPanel {
	private int x = 0, y = 0;
	private String state;
	
	public PaintPanel() {
		super();
		setBackground(Color.WHITE);
	}
	
	public void paintComponent(Graphics g) {
		if(state == "paint") {
			g.fillOval(x, y, 10, 10);
		}
		
		if(state == "erase") {
			g.setColor(Color.GRAY);
			g.fillRect(x, y, 10, 10);
		}
	}
	
	public void setX(int x) {
		this.x = x;
	}
	
	public void setY(int y) {
		this.y = y;
	}
	
	public void buttonState(String state) {
		this.state = state;
	}
}




I tried doing it in the constructor of the PaintPanel and the paint class inside the buildCanvasPanel() method. Niether work.
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: paint program

Posted 22 December 2010 - 04:25 PM

You can't have everything :)
That was done by your super.paintComponent(g); call
This is the role of this method, to erase the screen and repaint it with the registered background.
Two alternatives:
- or you call super.paintComponent(g); only when required to repaint the background
- or you set the color:
g.setColor(thecolor of the background I want);
and fill a rectangle the size of your panel
g.fillRect(0, 0, getWidth(), getHeight());

actually the real way of doing this is to have an arrayAlist of all the oval you want to draw
in the paintComponent() you call super.paintComponent(g);
then you parse you arrayList and re-draw all your ovals
Was This Post Helpful? 0
  • +
  • -

#6 giggly kisses  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 80
  • View blog
  • Posts: 391
  • Joined: 29-March 09

Re: paint program

Posted 22 December 2010 - 04:35 PM

View Postpbl, on 22 December 2010 - 03:25 PM, said:

You can't have everything :)
That was done by your super.paintComponent(g); call
This is the role of this method, to erase the screen and repaint it with the registered background.
Two alternatives:
- or you call super.paintComponent(g); only when required to repaint the background
- or you set the color:
g.setColor(thecolor of the background I want);
and fill a rectangle the size of your panel
g.fillRect(0, 0, getWidth(), getHeight());

actually the real way of doing this is to have an arrayAlist of all the oval you want to draw
in the paintComponent() you call super.paintComponent(g);
then you parse you arrayList and re-draw all your ovals


Well the only reason I would like to do this is so I can erase, or show the illusion of erasing. To erase I wanted to have brush that is an oval that is the same color as the background. I can't seen to find the same color as the default panel background color.
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: paint program

Posted 22 December 2010 - 05:01 PM

g.setColor(getBackground());
g.drawOval(....
Was This Post Helpful? 0
  • +
  • -

#8 giggly kisses  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 80
  • View blog
  • Posts: 391
  • Joined: 29-March 09

Re: paint program

Posted 22 December 2010 - 05:08 PM

View Postpbl, on 22 December 2010 - 04:01 PM, said:

g.setColor(getBackground());
g.drawOval(....


This set it to white for some reason.
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: paint program

Posted 22 December 2010 - 05:14 PM

some reason ???? :)

 public PaintPanel() {  
         super();  
         setBackground(Color.WHITE);  <--- that is the reason



as super.paintComponent(g); was never called
it never painted the background that color
but getBackground() returns that registered color

do not change the background to WHITE if you can leave with window GRAY
or drawRectangle() once as I have showed to you
Was This Post Helpful? 2
  • +
  • -

#10 giggly kisses  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 80
  • View blog
  • Posts: 391
  • Joined: 29-March 09

Re: paint program

Posted 22 December 2010 - 05:20 PM

View Postpbl, on 22 December 2010 - 04:14 PM, said:

some reason ???? :)

 public PaintPanel() {  
         super();  
         setBackground(Color.WHITE);  <--- that is the reason



as super.paintComponent(g); was never called
it never painted the background that color
but getBackground() returns that registered color

do not change the background to WHITE if you can leave with window GRAY
or drawRectangle() once as I have showed to you

Posted Image
It works now, I thought I got rid of that line but I didn't take it out of my Paint class. Thanks again!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1