Results 1 to 9 of 9
  1. #1
    igorland is offline Member
    Join Date
    Jan 2013
    Posts
    25
    Rep Power
    0

    Default Problems with lines

    Hello. I have been fighting drawing lines for 2 weeks already and nothing helps.

    I am to draw three lines on a panel after pressing a button. I am using two methods. The first one is as follows (I have only included the code referring to the button action):

    Java Code:
    	btnPnl5Calculate.addActionListener(new ActionListener() {
    			
    			@Override
    			public void actionPerformed(ActionEvent arg0) {
    				
    				...				
    				// Setting coordinates for the graph
    				
    				graphIndexX1 = (int) (indexTotal * 6 + 622);
    				graphIndexX2 = (int) graphIndexX1;
    				graphIndexY1 = 697;
    				graphIndexY2 = (int) (-0.002013 * weightTakeOffPlaneTotal + 1039.2);
    				
    				drawLineIndex();
    					
    				btnPnl5toPnl6.setEnabled(true);
    				lblPnl5Next.setVisible(true);
    
    				
    			}
    		});
    
                         public void drawLineIndex()	{
    		   Graphics g = this.getGraphics();
    		   Graphics2D g2d = (Graphics2D) g;
    		   g2d.setColor(Color.BLUE);
                                    g2d.setStroke(new BasicStroke(4, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 1, new float[]{9}, 0));
    		   RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                                   rh.put(RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_QUALITY);
            
                                   g2d.drawLine(graphIndexX1, graphIndexY1, graphIndexX2, graphIndexY2);
            
    	                  }
    It works except that when I call this:
    btnPnl5toPnl6.setEnabled(true);
    lblPnl5Next.setVisible(true);

    The graph is either partially or completely not visible unless I hit the button again. The other problem is that when I go to another panel and return to the panel with the button, the line is not there anymore. I have to press the button again.

    (continued on another page)
    Last edited by igorland; 05-22-2013 at 02:20 AM.

  2. #2
    igorland is offline Member
    Join Date
    Jan 2013
    Posts
    25
    Rep Power
    0

    Default Re: Problems with lines

    Then I tried the following:
    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.LinkedList;
    
    import javax.swing.JButton;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    public class Draw extends JPanel{
    
    private static class Line{
        final int x1; 
        final int y1;
        final int x2;
        final int y2;   
        final Color color;
    
        public Line(int x1, int y1, int x2, int y2, Color color) {
            this.x1 = x1;
            this.y1 = y1;
            this.x2 = x2;
            this.y2 = y2;
            this.color = color;
        }               
    }
    
    private final LinkedList<Line> lines = new LinkedList<Line>();
    
    public void addLine(int x1, int x2, int x3, int x4) {
        addLine(x1, x2, x3, x4, Color.black);
    }
    
    public void addLine(int x1, int x2, int x3, int x4, Color color) {
        lines.add(new Line(x1,x2,x3,x4, color));        
        repaint();
    }
    
    public void clearLines() {
        lines.clear();
        repaint();
    }
    
    
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        for (Line line : lines) {
            g.setColor(line.color);
            g.drawLine(line.x1, line.y1, line.x2, line.y2);
        }
    }
    
    }
    So, in the main class, I have this:

    Java Code:
                                Draw graph = new Draw();
    		graph.setPreferredSize(new Dimension(1308, 960));
    		graph.setVisible(true);
    
    		
    		btnPnl5Calculate.addActionListener(new ActionListener() {
    			
    			@Override
    			public void actionPerformed(ActionEvent arg0) {
    				
    				
    				graphIndexX1 = (int) (indexTotal * 6 + 622);
    				graphIndexX2 = (int) graphIndexX1;
    				graphIndexY1 = 697;
    				graphIndexY2 = (int) (-0.002013 * weightTakeOffPlaneTotal + 1039.2);
    				
    				graph.clearLines();
    				graph.addLine(graphIndexX1, graphIndexX2, graphIndexY1, graphIndexY2, Color.BLUE);
    This one does not work completely. No lines!!!
    Is there something wrong in the beginning?

    Java Code:
    	public static void main(String[] args) {
    		try {
    			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    		} catch (Throwable e) {
    			e.printStackTrace();
    		}
    		EventQueue.invokeLater(new Runnable() {
    			public void run() {
    				try {
    					dispatchMain frame = new dispatchMain();
    					frame.setVisible(true);
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
    			}
    		});
    	}
    
    	/**
    	 * Create the frame.
    	 */
    	
    	public dispatchMain() {
    		
    		// JFrame
    		
    		setIconImage(Toolkit.getDefaultToolkit().getImage(dispatchMain.class.getResource("/com/igorland/resources/img/iconFrame.png")));
    		setTitle("B747-400 Flight Planning and Management System (v 1.0)");
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setBounds(100, 100, 1308, 960);
    		contentPane = new JPanel();
    		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    		setContentPane(contentPane);
    		contentPane.setLayout(new CardLayout(0, 0));
    
    ...
    Thanks a lot! I hope I won't go crazy! ))

  3. #3
    Junky's Avatar
    Junky is online now Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,781
    Rep Power
    7

    Default Re: Problems with lines

    Try resizing your window. If the lines magically appear then you have a paint issue.

  4. #4
    igorland is offline Member
    Join Date
    Jan 2013
    Posts
    25
    Rep Power
    0

    Default Re: Problems with lines

    Is the Absolute Layout to blame for not seeing the lines when I first click the button? Is there also a way to keep the lines on the panel if going to another panel and back.

  5. #5
    igorland is offline Member
    Join Date
    Jan 2013
    Posts
    25
    Rep Power
    0

    Angry Re: Problems with lines

    This is it. Going crazy! Why? Why? Why?

    Java Code:
    import java.awt.BorderLayout;
    
    
    public class mainFrame extends JFrame {
    
    	private JPanel contentPane;
    	private JPanel panelCard;
    	private JPanel panelA;
    	private JPanel panelB;
    	private JButton btnDraw;
    	
    	final Draw comp = new Draw();
    
    	/**
    	 * Launch the application.
    	 */
    	public static void main(String[] args) {
    		EventQueue.invokeLater(new Runnable() {
    			public void run() {
    				try {
    					mainFrame frame = new mainFrame();
    					frame.setVisible(true);
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
    			}
    		});
    	}
    
    	/**
    	 * Create the frame.
    	 */
    	public mainFrame() {
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setBounds(100, 100, 902, 579);
    		contentPane = new JPanel();
    		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    		setContentPane(contentPane);
    		contentPane.setLayout(new CardLayout(0, 0));
    		
    		panelCard = new JPanel();
    		contentPane.add(panelCard, "1");
    		panelCard.setLayout(new CardLayout(0, 0));
    		
    		panelA = new JPanel();
    		panelCard.add(panelA, "2");
    		panelA.setLayout(null);
    		
    		JLabel lblNewLabel = new JLabel("PANEL A");
    		lblNewLabel.setBounds(325, 13, 56, 16);
    		panelA.add(lblNewLabel);
    		
    		JButton btnA = new JButton("NEXT");
    		btnA.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				CardLayout cardLayout = (CardLayout)(panelCard.getLayout());
    				cardLayout.show(panelCard, "3");
    			}
    		});
    		btnA.setBounds(401, 420, 97, 25);
    		panelA.add(btnA);
    		
    		panelB = new JPanel();
    		panelCard.add(panelB, "3");
    		panelB.setLayout(null);
    		
    		JLabel lblNewLabel_1 = new JLabel("PANEL B");
    		lblNewLabel_1.setBounds(330, 63, 56, 16);
    		panelB.add(lblNewLabel_1);
    		
    		JButton btnB = new JButton("NEXT");
    		btnB.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				CardLayout cardLayout = (CardLayout)(panelCard.getLayout());
    				cardLayout.show(panelCard, "2");
    			}
    		});
    		btnB.setBounds(420, 439, 97, 25);
    		panelB.add(btnB);
    	
    		btnDraw = new JButton("DRAW");
    		btnDraw.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				
    			    comp.setPreferredSize(new Dimension(902, 579));
    			    contentPane.add(comp, BorderLayout.CENTER);
    				
    	        	comp.clearLines();
    	            int x1 = (int) (Math.random()*320);
    	            int x2 = (int) (Math.random()*320);
    	            int y1 = (int) (Math.random()*200);
    	            int y2 = (int) (Math.random()*200);
    	            int x11 = (int) (Math.random()*320);
    	            int x12 = (int) (Math.random()*320);
    	            int y11 = (int) (Math.random()*200);
    	            int y12 = (int) (Math.random()*200);
    	            Color randomColor = new Color((float)Math.random(), (float)Math.random(), (float)Math.random());
    	            comp.addLine(x1, y1, x2, y2, randomColor);
    	            comp.addLine(x11, y11, x12, y12, randomColor);
    
    			}
    		});
    		btnDraw.setBounds(91, 439, 97, 25);
    		panelB.add(btnDraw);
    	}
    
    }
    Another class:

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.LinkedList;
    
    import javax.swing.JButton;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    public class Draw extends JComponent{
    
    private static class Line{
        final int x1; 
        final int y1;
        final int x2;
        final int y2;   
        final Color color;
    
        public Line(int x1, int y1, int x2, int y2, Color color) {
            this.x1 = x1;
            this.y1 = y1;
            this.x2 = x2;
            this.y2 = y2;
            this.color = color;
        }               
    }
    
    private final LinkedList<Line> lines = new LinkedList<Line>();
    
    public void addLine(int x1, int x2, int x3, int x4) {
        addLine(x1, x2, x3, x4, Color.black);
    }
    
    public void addLine(int x1, int x2, int x3, int x4, Color color) {
        lines.add(new Line(x1,x2,x3,x4, color));        
        repaint();
    }
    
    public void clearLines() {
        lines.clear();
        repaint();
    }
    
    
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        for (Line line : lines) {
            g.setColor(line.color);
            g.drawLine(line.x1, line.y1, line.x2, line.y2);
        }
    }
    
    }

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,501
    Rep Power
    5

    Default Re: Problems with lines

    OK, I got it to draw lines. But it isn't pretty. Do the following:

    1. Comment out every setBounds
    2. Comment out the two setLayout(null)
    3. Move the first two statements in actionListener outside the actionListener (to the mainFrame constructor)
    4. Change the second of those statements --> contentPane.add(comp... to panelB.add(comp...
    5. add repaint() as the last statement in the actionListener
    6. add pack() as the last statement in the mainFrame constructor
    7. Try running it.

    This may not look like you want, but it will draw lines. The panels need to be repositioned. I highly recommend you read about layout managers in the tutorials. Try some simple ones. Then adapt one to your application. Stay away from absolute positioning. And finally, don't subclass JFrame. But get the program working first.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  7. #7
    igorland is offline Member
    Join Date
    Jan 2013
    Posts
    25
    Rep Power
    0

    Default Re: Problems with lines

    Hello, Jim. Thank you so much! Your approach works except for one thing. I am trying to draw lines overlaying a picture (graph). With the absolute layout disabled, the lines do not overlay. They are drawn in the area where there are not objects. So, if I do random lines, it works; if I specify the coordinates overlaying the graph, the lines are not visible. Thanks anyways for trying to help!

    Regards,
    Igor

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,501
    Rep Power
    5

    Default Re: Problems with lines

    I was trying to get you back to where you could see the lines. I think part of the problem is in the absolute positioning. But I did not investigate further. If I think of anything else I will let you know.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  9. #9
    igorland is offline Member
    Join Date
    Jan 2013
    Posts
    25
    Rep Power
    0

    Default Re: Problems with lines

    Jim. You did help me a lot. I changed the layout to GridBagLayout and it is working now.
    Good: as I said, it is working the way I intended to have it working.
    Bad: I spent a couple of months creating 8 panels with about 100 objects on them. I can flush it down the toilet now and start from scratch again. But I guess, I can't help it. Big lesson learnt: never use Absolute Layout. )))

Similar Threads

  1. Problems drawing lines on a Graph
    By rhexis in forum New To Java
    Replies: 0
    Last Post: 02-17-2012, 05:24 PM
  2. Replies: 3
    Last Post: 10-25-2011, 06:29 PM
  3. Cannot Add new Lines
    By capjoel in forum New To Java
    Replies: 3
    Last Post: 04-01-2011, 06:33 PM
  4. What do these lines mean?
    By coding in forum New To Java
    Replies: 5
    Last Post: 02-02-2011, 03:01 AM
  5. Anyway to fix the lines so they dun shift?
    By PeterFeng in forum New To Java
    Replies: 0
    Last Post: 01-14-2009, 10:26 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •