Page 1 of 2 12 LastLast
Results 1 to 20 of 27
  1. #1
    seanfmglobal is offline Member
    Join Date
    Nov 2010
    Posts
    40
    Rep Power
    0

    Default Java GUI drawing a line

    Hello everyone. I have been trying to figure this out for a long time and can't seem to get it right. I am trying to create lines for my graph and I was going by using paint component.

    Java Code:
    public void paint(Graphics g){
    drawLine(x1, y1, x2, y2);
    }
    But no matter what the line will not be drawn. Here is the code that I am working with.

    Java Code:
    package pr3;
    
    import java.awt.Color;
    import java.awt.Graphics;
    
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    
    public class TemperaturePanel{
    
    	
    	public static void main(String[] args) {
    	
    	
    		
    		
    	
    		
    	JFrame frame = new JFrame ("Project 3 by C. Ross: Highs and lows with predictions one day in advance for March, 2011");
    	frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);	
    	labelPanel mainPan = new labelPanel();
    	
    	/*
    	 * creation of all the panels for all the texts
    	 */
    	JPanel chart = new JPanel();
    	JPanel negT = new JPanel();
    	JPanel negTn = new JPanel();
    	JPanel zero = new JPanel();
    	JPanel ten = new JPanel();
    	JPanel twenty = new JPanel();
    	JPanel thirty = new JPanel();
    	JPanel fourty = new JPanel();
    	JPanel fifty = new JPanel();
    	JPanel sixty = new JPanel();
    	JPanel seventy = new JPanel();
    	JPanel eighty = new JPanel();
    	JPanel ninty = new JPanel();
    	JPanel hundred = new JPanel();
    	JPanel hundredTen = new JPanel();
    	/*
    	 * Setting the panel up: background, location, label, label color, and adding the label to the panel
    	 * 
    	 * Negative 20
    	 */
    	
    	negT.setBackground(Color.black);
    	negT.setBounds(25, 420, 25, 25);
    	JLabel tester = new JLabel ("-20");
    	tester.setForeground(Color.white);
    	negT.add(tester);
    	
    	/*
    	 * Negative 10
    	 */
    	
    	negTn.setBackground(Color.black);
    	negTn.setBounds(25, 400, 25, 25);
    	JLabel negTenLabel = new JLabel("-10");
    	negTenLabel.setForeground(Color.white);
    	negTn.add(negTenLabel);
    	
    	/*
    	 * zero
    	 */
    	
    	zero.setBackground(Color.black);
    	zero.setBounds(25, 380, 25, 25);
    	JLabel zeroLabel = new JLabel("0");
    	zeroLabel.setForeground(Color.white);
    	zero.add(zeroLabel);
    	
    	/*
    	 * ten
    	 */
    	
    	ten.setBackground(Color.black);
    	ten.setBounds(25, 360, 25, 25);
    	JLabel tenLabel = new JLabel("10");
    	tenLabel.setForeground(Color.white);
    	ten.add(tenLabel);
    	
    	/*
    	 * twenty
    	 */
    	
    	twenty.setBackground(Color.black);
    	twenty.setBounds(25, 340, 25, 25);
    	JLabel twentyLabel = new JLabel("20");
    	twentyLabel.setForeground(Color.white);
    	twenty.add(twentyLabel);
    	
    	/*
    	 * thirty
    	 */
    	
    	
    	thirty.setBackground(Color.black);
    	thirty.setBounds(25, 320, 25, 25);
    	JLabel thirtyLabel = new JLabel("30");
    	thirtyLabel.setForeground(Color.white);
    	thirty.add(thirtyLabel);
    	
    	
    	/*
    	 * fourty
    	 */
    	
    	fourty.setBackground(Color.black);
    	fourty.setBounds(25, 300, 25, 25);
    	JLabel fourtyLabel = new JLabel("40");
    	fourtyLabel.setForeground(Color.white);
    	fourty.add(fourtyLabel);
    	
    	/*
    	 * fifty
    	 */
    	
    	fifty.setBackground(Color.black);
    	fifty.setBounds(25, 280, 25, 25);
    	JLabel fiftyLabel = new JLabel("50");
    	fiftyLabel.setForeground(Color.white);
    	fifty.add(fiftyLabel);
    	
    	/*
    	 * sixty
    	 */
    	
    	
    	
    	
    	sixty.setBackground(Color.black);
    	sixty.setBounds(25, 260, 25, 25);
    	JLabel sixtyLabel = new JLabel("60");
    	sixtyLabel.setForeground(Color.white);
    	sixty.add(sixtyLabel);
    	
    	/*
    	 * seventy
    	 */
    	
    	seventy.setBackground(Color.black);
    	seventy.setBounds(25, 240, 25, 25);
    	JLabel seventyLabel = new JLabel("70");
    	seventyLabel.setForeground(Color.white);
    	seventy.add(seventyLabel);
    	
    	/*
    	 * eighty
    	 */
    	
    	eighty.setBackground(Color.black);
    	eighty.setBounds(25, 220, 25, 25);
    	JLabel eightyLabel = new JLabel("80");
    	eightyLabel.setForeground(Color.white);
    	eighty.add(eightyLabel);
    	
    	
    	/*
    	 * ninty
    	 */
    	
    	ninty.setBackground(Color.black);
    	ninty.setBounds(25, 200, 25, 25);
    	JLabel nintyLabel = new JLabel("90");
    	nintyLabel.setForeground(Color.white);
    	ninty.add(nintyLabel);
    	
    	/*
    	 * hundred
    	 */
    	
    	hundred.setBackground(Color.black);
    	hundred.setBounds(25, 180, 25, 25);
    	JLabel hundredLabel = new JLabel("100");
    	hundredLabel.setForeground(Color.white);
    	hundred.add(hundredLabel);
    	
    	/*
    	 * hundredTen
    	 */
    	
    	
    	hundredTen.setBackground(Color.black);
    	hundredTen.setBounds(25, 160, 25, 25);
    	JLabel hundredTenLabel = new JLabel("110");
    	hundredTenLabel.setForeground(Color.white);
    	hundredTen.add(hundredTenLabel);
    	
    	/*
    	 * Whitespace for the graph
    	 */
    	
    	chart.setBounds(50, 50, 690, 400);
    	chart.setBackground(Color.white);
    	
    	
    	
    	
    	/*
    	 * Adding everything to the frame	
    	 */
    	
    	frame.getContentPane().add(negT);
    	frame.getContentPane().add(negTn);
    	frame.getContentPane().add(zero);
    	frame.getContentPane().add(ten);
    	frame.getContentPane().add(twenty);
    	frame.getContentPane().add(thirty);
    	frame.getContentPane().add(fourty);
    	frame.getContentPane().add(fifty);
    	frame.getContentPane().add(sixty);
    	frame.getContentPane().add(seventy);
    	frame.getContentPane().add(eighty);
    	frame.getContentPane().add(ninty);
    	frame.getContentPane().add(hundred);
    	frame.getContentPane().add(hundredTen);
    	frame.getContentPane().add(chart);
    	frame.getContentPane().add(mainPan);
    	frame.pack();
    	frame.setVisible(true);
    	
    	
    	
    	
    	
    	}
    
    	
    	public void paint(Graphics g){
    		g.drawLine(50, 50, 300, 300);
    		g.setColor(Color.yellow);
    	}
    }

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    7

    Default

    Why would you create a 100 line program to test 2 lines of code? Keep your programs short and simple when testing a new concept. In the future post your Short, Self Contained, Correct Example that demonstrates the problem.

    In the meantime read the section from the Swing tutorial on Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing) for a working example.

  3. #3
    seanfmglobal is offline Member
    Join Date
    Nov 2010
    Posts
    40
    Rep Power
    0

    Default

    Yeah I do realize that the code is in fact long but I definitely would not create a large program to test 2 lines of code. This is a project I'm working on for my CS class that is why the code is relatively long. I posted the entire code so that whomever answering this question can see exactly how I am going to implement this code into my project. I can read and look at examples all day but when trying to write them in this project I come up with a problem. The lines are not being drawn. When writing the code like it is in my book or online the examples are usually for that specific piece of code not in conjunction with the rest of my program. Sorry for the confusion.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,344
    Blog Entries
    7
    Rep Power
    20

    Default

    Your TemperaturePanel1 class doesn't extend (indirectly) from the JComponent class so nothing is going to call your paint( ... ) method (that should be a paintComponent( ... ) method b.t.w.) Your class structure doesn't make any sense.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    Your problem is that paint(Graphics) is never called. You need to have a class that extends JComponent and override it's paintComponent() or extends JFrame in your main class and override it's paint(g) (however, you need to call super.paint(g) in both methods).
    Last edited by ra4king; 04-11-2011 at 02:35 AM.

  6. #6
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    7

    Default

    You should NOT override paint(), especially paint the paint method of the JFrame.

    Please read the provided link to the tutorial.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by camickr View Post
    You should NOT override paint(), especially paint the paint method of the JFrame.

    Please read the provided link to the tutorial.
    Amen! Why lose all the benefits of Swing graphics by not overriding paintComponent?

  8. #8
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    You can override paint() in JFrame but you must call super.paint(g) if you want all children painted too.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by ra4king View Post
    You can override paint() in JFrame but you must call super.paint(g) if you want all children painted too.
    It's not a matter of can or can't, it's a matter of it is very bad practice to override JFrame's paint method rather than the paintComponent method of a JComponent such as JPanel. The Oracle Swing graphics tutorials explain this all.

  10. #10
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    7

    Default

    There are many things you can do. Doing custom painting by overriding the paint method of a frame is not the advice you give to people who want to do painting the proper way. Please read the question and the tutorial.

  11. #11
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    Well it's better than creating a whole new JComponent and overriding it's paintComponent() and then having to deal with positioning it with other components ;)

    If you want to do heavy drawing, then yes, use a JComponent, but for the OP's program, overriding paint() in JFrame is what he needs.

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by ra4king View Post
    Well it's better than creating a whole new JComponent and overriding it's paintComponent() and then having to deal with positioning it with other components ;)
    No it's not. Again, please read the tutorials.


    If you want to do heavy drawing, then yes, use a JComponent, but for the OP's program, overriding paint() in JFrame is what he needs.
    Sorry, but you are wrong. Please read this article: Painting in AWT and Swing


    In particular this quote:
    Swing programs should override paintComponent() instead of overriding paint(). Although the API allows it, there is generally no reason to override paintBorder() or paintComponents() (and if you do, make sure you know what you're doing!). This factoring makes it easier for programs to override only the portion of the painting which they need to extend. For example, this solves the AWT problem mentioned previously where a failure to invoke super.paint() prevented any lightweight children from appearing.

    Also please read the chapters on graphics in the Core Java books, volume I and II by Cay S. Horstmann and Gary Cornell
    Also please read Filthy Rich Clients: Developing Animated and Graphical Effects for Desktop Java Applications by Haase and Guy
    Last edited by Fubarable; 04-11-2011 at 03:24 AM.

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    In fact I highly recommend the Haase and Guy book that I link to above. You can either buy it as a book or as a PDF download. If you get it, please have a look at Chapter 2 on Swing Rendering Fundamentals, the section on the paint() method which will outline many reasons for not overriding this method in any Swing application unless there's an extremely good reason for doing so (there isn't here). The exception they give is if you were creating a translucent component for then it would be important to have control over the painting of child components as well, but otherwise override paintComponent in a JComponent or JPanel, not paint in a JFrame or in a Swing component.

  14. #14
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    Yes, I know. I have been using Swing for a long time and have a ton of experience with it. I was saying that for a simple drawLine, the easiest and most painless route is to override JFrame's paint(), call super.paint(g), and add a quick call to drawLine(). Otherwise, for any light drawing, using a JComponent and overriding it's paintComponent() is the recommended one here. For heavy graphics and games, Canvas + BufferStrategy is the most recommended.

    BTW - That link about Paint in AWT and Swing, I have read that such a long time ago, it brought memories of my confusion when I first started learning graphics. Since then I have created many games, decompiled the entire JRE, and created my own Look and Feel ;)

  15. #15
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    7

    Default

    > I was saying that for a simple drawLine, the easiest and most painless route is to override JFrame's paint(), call super.paint(g), and add a quick call to drawLine

    For crying out loud please read the question and answer the question in the context of the question. This question is NOT about drawing a line in a frame.

    The posted code shows other components on the frame which means the custom drawing needs to interact with the other Swing components. The graph is just one component on the GUI. You would never override paint() on the frame. Keep the answers simple and straight forward.

  16. #16
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    Fine :)

    @OP
    Java Code:
    public class MySuperAwesomeDrawing extends JComponent {
        protected void paintComponent(Graphics g) {
            g.drawLine(x1,y1,x2,y2);
        }
    }
    :D

  17. #17
    seanfmglobal is offline Member
    Join Date
    Nov 2010
    Posts
    40
    Rep Power
    0

    Default

    ok I've fixed a lot of horrible coding in my program so now i have a few classes im working with. I still cannot seem to draw the lines over the white chart area. Any help will be greatly appreciated. I've tried two ways to do it and I've placed the code in different order to double check if anything was showing up over it. I placed a println in the paint method to see if it ever gets called and no it does not get called. Please let me know what you think. I will include the source and a screenshot of how the chart looks like now.



    This is the TemperaturePanel Class
    Java Code:
    package pr3;
    
    import java.awt.Color;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    
    @SuppressWarnings("serial")
    public class TemperaturePanel extends JComponent{
    	private static double helper = 50;
    	private static double spaceIncrease = 28.57;
    	private static String[] labelNumbers = {"-20", "-10", "0", "10", "20", "30", "40", "50", "60", "70", "80", "90", "100", "110"};
    	private static JPanel[] allNumbers;
    	private static ProjectLines PL = new ProjectLines();
    
    			
    		
    		
    		
    	
    	public TemperaturePanel(){
    	
    	JFrame frame = new JFrame ("Project 3 by C. Ross: Highs and lows with predictions one day in advance for March, 2011");
    	frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);	
    	labelPanel mainPan = new labelPanel();
    	
    	
    	
    	allNumbers = getNumbers();
    	
    	JPanel chart = new JPanel();
    	
    	chart.setBounds(50, 50, 690, 400);
    	chart.setBackground(Color.white);
    	frame.getContentPane().add(PL);
    	
    	for(int i = 0; i < 14; i++){
    		frame.getContentPane().add(allNumbers[i]);
    	}
    	
    	
    	frame.getContentPane().add(chart);
    	frame.getContentPane().add(new ProjectLines());
    	frame.getContentPane().add(mainPan);
    	
    	frame.pack();
    	frame.setVisible(true);
    	}
    	
    	
    	
    	private static JPanel[] getNumbers(){
    		JPanel[] ret = new JPanel[14];
    		
    		
    		for(int index = 13; index >= 0; index--){
    			ret[index] = new JPanel();
    			ret[index].setBackground(Color.black);
    			ret[index].setBounds(25, (int) helper, 25, 25);
    			JLabel addMe = new JLabel(labelNumbers[index]);
    			addMe.setForeground(Color.WHITE);
    			ret[index].add(addMe);
    			helper = helper + spaceIncrease;
    			}
    		return ret;
    	}
    }
    
    //}

    This is the ProjectLines class.... This has the paint in it.

    Java Code:
    package pr3;
    
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import javax.swing.JComponent;
    
    
    public class ProjectLines extends JComponent{
    
    	
    		
    		/**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    
    		public void Paint(Graphics g){
    			super.paint(g);
    			Graphics2D g2d = (Graphics2D) g;	
    			g2d.setColor(Color.red);
    			g2d.drawLine(100, 100, 220, 220);
    			g2d.drawLine(120, 120, 240, 240);
    			System.out.println("paint test");
    		
    	}
    	
    }

    And this is the ProgramMain class

    Java Code:
    package pr3;
    
    public class ProgramMain {
    
    	public static void main(String[] args){
    		TemperaturePanel TP = new TemperaturePanel();
    	}
    }
    The screenshot is attached
    Attached Thumbnails Attached Thumbnails Java GUI drawing a line-chartprogram.jpg  

  18. #18
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    7

    Default

    There are many things wrong.

    You where given a link to the tutorial for a reason. Read the tutorial and download the demo program. Then you can change the demo program to draw a line instead of drawing a string. Then once that works you can add some more changes. The idea is to start with somthing simple that work and then make one change at a time. If it works you can move on. If it doesn't work then you have a simple SSCCE to post.

  19. #19
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    You misspelled the paint() method, it is entirely lowercase. JFrame has the default BorderLayout, which means you can only add 1 component to the center of the screen. After creating JFrame, try playing around with FlowLayout: "frame.setLayout(new FlowLayout())". Also why create 2 objects ProjectLines?

  20. #20
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    7

    Default

    You misspelled the paint() method, it is entirely lowercase.
    Yes, but he should not be overriding the paint() method to begin with as has already been discussed to death.

    That is why we keep pointing to the tutorial which has a working example. It alloww the poster to develop good habbits by starting with proper code.

Page 1 of 2 12 LastLast

Similar Threads

  1. Drawing coordinates in Java GUI
    By javausr in forum Java 2D
    Replies: 5
    Last Post: 12-29-2010, 10:49 AM
  2. tracing java application line by line using netbeans
    By chandrasekhar123 in forum NetBeans
    Replies: 1
    Last Post: 08-03-2010, 02:46 PM
  3. Replies: 2
    Last Post: 07-02-2010, 02:20 PM
  4. drawing Line by dragging mouse !!!
    By h9h in forum Java 2D
    Replies: 14
    Last Post: 10-23-2009, 05:10 AM
  5. Help with Drawing a line
    By Rgfirefly24 in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 08:40 AM

Tags for this Thread

Posting Permissions

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