We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,515 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

paintComponent() output error..

hi. i'm trying to make a program that will connect two points that i click in my JPanel. i'm trying to connect the two points with a line. i displayed the values of my (x1, y1) and (x2,y2) coordinates whenever i click using the mouse and there is no error in the values. but when the line is displayed, it the line doesn't seem to follow the coordinates i specify but instead outputs in a different location and is distorted. most of the lines will appear to be cut by something, i think it's the rectangle created by the line because i used setBounds(). also, i add a System.out.println("") inside my paintComponent function and i noticed that it printed multiple times (increasing by 1 after every click) even though it should only print once. can anyone help me with this? thanks!

here are two of the classes which contribute to the error:

CLASS # 1:

import java.awt.event.MouseEvent;
    import javax.swing.JPanel;
    
    
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    /**
     *
     * @author Arch. Don Saborrido
     */
    public class twixtBoard extends JPanel implements java.awt.event.MouseListener{
        int x1 = 0, x2, y1 = 0, y2;
        DrawLine line;
        //DrawLine line;
    
    
        public twixtBoard(){
           //requestFocus();
            setLayout(null);
            setVisible(false);
            setBounds(0,0,600,450);
            setOpaque(false);
            setFocusable(false);
            addListener();
        }
    
        public void addListener(){
            addMouseListener(this);
        }
    
        public void mouseClicked(MouseEvent e) {
            if (x1 == 0 || y1 == 0){
                        x1 = e.getX();
                        y1 = e.getY();
                        System.out.println(x1 + " " + y1);
            }
            else{
                x2 = e.getX();
                y2 = e.getY();
                //System.out.println(x2 + " " + y2);
                line = new DrawLine(x1, y1, x2, y2);
                line.setBounds(x1, y1, x2, y2);
                System.out.println("" + line.getLocation());
                //line.setOpaque(false);
                add(line);
                x1 = x2;
                y1 = y2;
                repaint();
            }
        }
    
        public void mousePressed(MouseEvent e) {
            //throw new UnsupportedOperationException("Not supported yet.");
        }
    
        public void mouseReleased(MouseEvent e) {
            ///throw new UnsupportedOperationException("Not supported yet.");
        }
    
        public void mouseEntered(MouseEvent e) {
            //throw new UnsupportedOperationException("Not supported yet.");
        }
    
        public void mouseExited(MouseEvent e) {
            //throw new UnsupportedOperationException("Not supported yet.");
        }
    }

CLASS # 2 (Paint Class):

/*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    /**
     *
     * @author Arch. Don Saborrido
     */
    import java.awt.BasicStroke;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Stroke;
    import java.awt.geom.GeneralPath;
    //import java.awt.geom.Line2D;
    import javax.swing.JPanel;
    
    public class DrawLine extends JPanel{
        int x1 = 0, x2 = 0, y1 = 0, y2 = 0;
        //Line2D line;
        Stroke[] s = new Stroke[] {new BasicStroke(10.0f, BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND)};
        //new BasicStroke(25.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL),
        //new BasicStroke(25.0f, BasicStroke.CAP_SQUARE,BasicStroke.JOIN_MITER)
        GeneralPath path = new GeneralPath();
    
        public DrawLine(int start_x, int start_y, int end_x, int end_y){
            x1 = start_x;
            y1 = start_y;
            x2 = end_x;
            y2 = end_y;
            System.out.println(x1+ " " + y1+ " " + x2+ " " + y2+ " ");
        }
    
        @Override
        protected void paintComponent(Graphics g) {
            System.out.println("entered paint");
            Graphics2D g2 = (Graphics2D) g;
            g2.setColor(Color.BLACK);
            g2.setStroke(s[0]);
            path.moveTo(x1,y1);
            System.out.println("x1 = " + x1 + " y1 = " + y1);
            path.lineTo(x2,y2);
            System.out.println("x2 = " + x2 + " y2 = " + y2);
            System.out.println("" + path.getBounds2D());
            g2.draw(path);
            //line = new Line2D.Float(x1, y1, x2, y2);
            //if(x1 != x2 && y1 != y2)
                //g2.draw(line);
        }
    }
3
Contributors
2
Replies
3 Hours
Discussion Span
1 Year Ago
Last Updated
3
Views
janfritz
Newbie Poster
1 post since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
mKorbel
Nearly a Posting Virtuoso
1,215 posts since Feb 2011
Reputation Points: 482
Solved Threads: 239
Skill Endorsements: 12

I think the best solution is to:
- make a class with:
- four variables: x1, x2, y1 and y2.
- keep a ArrayList<LineClass> of your generated lines
- draw each line in the ArrayList on your JPanel using g.drawline(line.x1, line.y1, line.x2, line.y2) where line is the element of the ArrayList

hiddepolen
Posting Whiz in Training
297 posts since Oct 2010
Reputation Points: 82
Solved Threads: 36
Skill Endorsements: 1

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0828 seconds using 2.75MB