1,105,334 Community Members

paintComponent() output error..

Member Avatar
janfritz
Newbie Poster
1 post since Jan 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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);
        }
    }
Member Avatar
mKorbel
Nearly a Posting Virtuoso
1,346 posts since Feb 2011
Reputation Points: 274 [?]
Q&As Helped to Solve: 277 [?]
Skill Endorsements: 14 [?]
 
0
 
Member Avatar
hiddepolen
Posting Whiz in Training
293 posts since Oct 2010
Reputation Points: 32 [?]
Q&As Helped to Solve: 36 [?]
Skill Endorsements: 1 [?]
 
1
 

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

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: