Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: So im making a freehand Line,Rect,Oval; but problems

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default So im making a freehand Line,Rect,Oval; but problems

    I got some problems on how to get the freehand Line and such working atleast if i figure out how to do the line then it will be easy to convert it over to work for both the Oval and the Rectangle.

    So here is the deal im making a paint program I got everything set out in interfaces and different classes.

    Im using the mouse dragged, pressed and released to make the line. I do get the line to work how i want it but it doesnt draw itself on the board. I got the clicked function for the regular drawings. So when you click you can click on a Oval,Rect and line and it will make a static line which you can alter with height and width to make it longer/shorter/bigger/smaller. Thats allright, but we are also implementing a freehand implementation. This is where im stuck. So here is a code snippet:

    Init phase;
    public class View extends JFrame {
     
     
    	Model model = new Model();
    	TegnePanel tegnePanel = new TegnePanel();
    	JPanel jp2 = new JPanel();
     
    	Color currentFarge = Color.BLUE;
    	boolean current = true;
    	public int currentWidth = 10;
    	public int currentHeight = 10;
    	private FigurType currentFigurType = FigurType.RECTANGLE;
    	public enum FigurType {
    		LINE,RECTANGLE,OVAL,DELETE
    	}
    	int xStart, yStart;
    	int xSlutt, ySlutt;
    	int wDra, hDra;

    mouseListenerPhase;
    @Override
    		public void mousePressed(MouseEvent e) {
    			e.consume();
    			xStart = e.getX();
    			yStart = e.getY();
     
    		}
     
    		@Override
    		public void mouseReleased(MouseEvent e) {
    			xSlutt = e.getX();
    			ySlutt = e.getY();
    			repaint();
    		}

    mouseMotionListenerPhase;

    public void mouseDragged(MouseEvent event){
     
    			if (currentFigurType == FigurType.LINJE){
    				Graphics g = getGraphics();
    				xSlutt = event.getX();
    				ySlutt = event.getY();
    				Color c = currentFarge;
     
    				g.drawLine(xStart, yStart, xSlutt, ySlutt);
     
    				repaint();

    Translations: xSlutt = xEnd,


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: So im making a freehand Line,Rect,Oval; but problems

    That's not how painting is done. If you're calling getGraphics() on a component, you're probably doing something wrong.

    I recommend you read through this: Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing) and come back with an SSCCE if you still have questions.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Replies: 10
    Last Post: September 16th, 2011, 07:49 PM
  2. Making a pacman game having some problems with drawing
    By Matta in forum What's Wrong With My Code?
    Replies: 22
    Last Post: June 9th, 2011, 06:18 PM
  3. Getting a red Oval to displayed on screen
    By warnexus in forum What's Wrong With My Code?
    Replies: 8
    Last Post: January 13th, 2011, 08:27 PM
  4. How to Read a file line by line using BufferedReader?
    By JavaPF in forum File Input/Output Tutorials
    Replies: 0
    Last Post: May 19th, 2008, 06:32 AM
  5. How to Read a file line by line using BufferedReader?
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: May 19th, 2008, 06:32 AM