2 Replies - 290 Views - Last Post: 13 November 2012 - 07:12 AM Rate Topic: -----

#1 Tree55Topz  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 84
  • Joined: 06-September 12

Paint program

Posted 12 November 2012 - 07:31 PM

Hello, I am trying to create a simple paint program but I am running into a problem. I have created the frame, made a class that handles the painting, and am using an image to draw on(I tried using a JPanel but it seemed to only let me make shapes like circles, rectangles, etc rather than freehand) but when I make an instance of paintStuff my image does not seem to be shown and nothing happens when i move my mouse. any suggestions? Thank you!
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;

import javax.swing.*;

public class Paint {
	public static void main(String[] args){
		gui frame = new gui();
		frame.setVisible(true);
	}

}

class gui extends JFrame implements ActionListener{
	private JButton Red, Green, Blue, Eraser, Clear;
	private JPanel p1, p2;
	private Graphics2D graphic;
	private paintStuff canvas;
	
	
	public gui(){
		
		
		canvas = new paintStuff();
		canvas.setVisible(true);
		
		
		//Title and dimensions
		setTitle("Paint Stuff!!");
		setSize(500,500);
		setLayout(new BorderLayout());
		
		//Set up JButtons
		Red = new JButton("Red");
		Green = new JButton("Green");
		Blue = new JButton("Blue");
		Eraser = new JButton("Eraser");
		Clear = new JButton("Clear");
		
		//JPanels
		p1 = new JPanel();
		p2= new JPanel();
		
		p1.setLayout(new FlowLayout());
		p1.add(Red);
		p1.add(Green);
		p1.add(Blue);
		p1.add(Eraser);
		p1.add(Clear);
		add(p1,BorderLayout.SOUTH);
		
		add(canvas,BorderLayout.CENTER);
		
		
		
	}
	
	public void actionPerformed(ActionEvent e){
		
	}

	
}

class paintStuff extends JComponent{

	 int newX, newY, oldX, oldY;
	 Graphics2D G;
	 Image canvas;
	 
	public paintStuff(){
		
		addMouseListener(new MouseAdapter(){
			public void mousePressed(MouseEvent e){
				oldX = e.getX();
				oldY = e.getY();
			}
			
		});
		addMouseMotionListener(new MouseAdapter(){
			public void mouseDragged(MouseEvent e){
				newX = e.getX();
				newY = e.getY();
				G.drawLine(oldX, oldY, newX, newY);
				repaint();
				oldX = newX;
				oldY = newY;
			}
		});
	}
	public void paintComponent(Graphics g){
		canvas = createImage(500,500);
		G = (Graphics2D)canvas.getGraphics();
		G.drawImage(canvas,0,0,Color.black,null);
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Paint program

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2720
  • View blog
  • Posts: 11,438
  • Joined: 20-September 08

Re: Paint program

Posted 13 November 2012 - 06:25 AM

Quote

I tried using a JPanel but it seemed to only let me make shapes like circles, rectangles, etc rather than freehand


That's exactly what you should be doing. Using an Image is no different - just redundant in this case. btw, class names in Java should begin uppercase, variable names, lower case.

class paintStuff extends JPanel {
    int newX;
    int newY;
    int oldX;
    int oldY;

    public paintStuff() {
        addMouseListener(new MouseAdapter() {
                public void mousePressed(MouseEvent e) {
                    oldX = e.getX();
                    oldY = e.getY();
                }
            });
        addMouseMotionListener(new MouseAdapter() {
                public void mouseDragged(MouseEvent e) {
                    newX = e.getX();
                    newY = e.getY();
                    oldX = newX;
                    oldY = newY;
                    repaint();
                }
            });
    }

    public void paintComponent(Graphics g) {
        g.drawLine(oldX, oldY, newX, newY);
    }
}

This post has been edited by g00se: 13 November 2012 - 07:23 AM
Reason for edit:: typo

Was This Post Helpful? 0
  • +
  • -

#3 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1398
  • View blog
  • Posts: 3,097
  • Joined: 05-April 11

Re: Paint program

Posted 13 November 2012 - 07:12 AM

I love the design decisions you need to make when making a paint program
Imo. a small paint application is perfect for a school assignment AFTER you have learned about MVC

I don't think you have put much thought into your design yet ^^ ?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1