Results 1 to 20 of 27
Thread: Java GUI drawing a line
- 04-10-2011, 03:56 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 40
- Rep Power
- 0
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.
But no matter what the line will not be drawn. Here is the code that I am working with.Java Code:public void paint(Graphics g){ drawLine(x1, y1, x2, y2); }
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); } }
- 04-10-2011, 04:42 PM #2
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
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.
- 04-10-2011, 04:52 PM #3
Member
- Join Date
- Nov 2010
- Posts
- 40
- Rep Power
- 0
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.
- 04-10-2011, 04:59 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 04-10-2011, 09:12 PM #5
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.
- 04-11-2011, 12:22 AM #6
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
You should NOT override paint(), especially paint the paint method of the JFrame.
Please read the provided link to the tutorial.
-
- 04-11-2011, 02:40 AM #8
You can override paint() in JFrame but you must call super.paint(g) if you want all children painted too.
-
- 04-11-2011, 02:44 AM #10
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
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.
- 04-11-2011, 02:50 AM #11
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.
-
No it's not. Again, please read the tutorials.
Sorry, but you are wrong. Please read this article: Painting in AWT and SwingIf 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.
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 GuyLast edited by Fubarable; 04-11-2011 at 03:24 AM.
-
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.
- 04-11-2011, 04:11 AM #14
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 ;)
- 04-11-2011, 04:20 AM #15
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
> 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.
- 04-11-2011, 04:23 AM #16
Fine :)
@OP
:DJava Code:public class MySuperAwesomeDrawing extends JComponent { protected void paintComponent(Graphics g) { g.drawLine(x1,y1,x2,y2); } }
- 04-14-2011, 05:28 AM #17
Member
- Join Date
- Nov 2010
- Posts
- 40
- Rep Power
- 0
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
The screenshot is attachedJava Code:package pr3; public class ProgramMain { public static void main(String[] args){ TemperaturePanel TP = new TemperaturePanel(); } }
- 04-14-2011, 05:38 AM #18
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
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.
- 04-14-2011, 05:50 AM #19
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?
- 04-14-2011, 05:53 AM #20
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
Yes, but he should not be overriding the paint() method to begin with as has already been discussed to death.You misspelled the paint() method, it is entirely lowercase.
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.
Similar Threads
-
Drawing coordinates in Java GUI
By javausr in forum Java 2DReplies: 5Last Post: 12-29-2010, 10:49 AM -
tracing java application line by line using netbeans
By chandrasekhar123 in forum NetBeansReplies: 1Last Post: 08-03-2010, 02:46 PM -
Formatting java command line output - Multi line string
By dricco in forum New To JavaReplies: 2Last Post: 07-02-2010, 02:20 PM -
drawing Line by dragging mouse !!!
By h9h in forum Java 2DReplies: 14Last Post: 10-23-2009, 05:10 AM -
Help with Drawing a line
By Rgfirefly24 in forum New To JavaReplies: 1Last Post: 08-06-2007, 08:40 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks