need help w/ entry level homework...
I am in an entry level Java class and am supposed to implement 3 classes, ItalianFlag, ItalianFlagViewer and ItalianFlagComponent and use as much object oriented design as possible so one can specify the coordinates x and y and width to draw the Italian flag. I am supposed to test the design by drawing two flags in my viewer.
My problem is that I have the Italian Flag designed, but cannot get it to appear twice on my viewer. I'll post my codes in hopes someone can point out my flaw. I am actually happy I got this far...
Code java:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JComponent;
public class ItalianFlagComponent extends JComponent{
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
ItalianFlag flag1 = new ItalianFlag(0,0,0, Color.black);
ItalianFlag flag2 = new ItalianFlag(200, 200, 200, Color.black);
flag1.draw(g2);
flag2.draw(g2);
}
}
Code java:
import java.awt.geom.*;
import java.awt.*;
public class ItalianFlag {
public int x,y;
public Color c;
public ItalianFlag(double x, double y, double aWidth) {
}
public ItalianFlag(double x, double y, double aWidth, Color c){
this.c= c;
}
public void draw(Graphics2D g2) {
Rectangle leftRectangle = new Rectangle(x+15, y+10, 45, 45);
Rectangle rightRectangle = new Rectangle (x+100, y+10, 45, 45);
Point2D.Double r1= new Point2D.Double(x+15, y+10);
Point2D.Double r2= new Point2D.Double(x+100, y+10);
Line2D.Double top= new Line2D.Double(r1,r2);
Point2D.Double r3= new Point2D.Double(x+45, y+55);
Point2D.Double r4= new Point2D.Double(x+115, y+55);
Line2D.Double bottom= new Line2D.Double(r3,r4);
g2.setColor(Color.GREEN);
g2.fill(leftRectangle);
g2.setColor(Color.RED);
g2.fill(rightRectangle);
g2.setColor(c);
g2.draw(leftRectangle);
g2.draw(rightRectangle);
g2.draw(top);
g2.draw(bottom);
}
}
Code java:
import javax.swing.*;
public class ItalianFlagViewer {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(300,400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ItalianFlagComponent component = new ItalianFlagComponent();
frame.add(component);
frame.setVisible(true);
}
}
I hope that's not too jumbled, I'm not sure how to clearly post codes where they are easily readable.
Thanks,
Brandon
Re: need help w/ entry level homework...
(Sorry for this default newbie response)
A few things before we can help you, remember these on all your other posts:
1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code. This makes it easier for everyone to read your code.
2. Explain to us what exactly your code is doing that is wrong. Give full details of errors and provide us with as much information about the situation as possible.
3. Give us an example of what the output should look like when done correctly. This gives everyone an understanding of what the final product should be, compared to what you have now.
Make those changes to your post, and do those on all your future posts, and you will receive help MUCH faster. Until those are done, I really can't help you much, as I don't have a full understanding to what is going on.
Ok, now that that is out in the open...
1) Can you specify the purpose of each class?
2) You seemed to have included the ItalianFlagComponent class twice, and that confused me a bit.
3) Out of curiosity, can you get 1 flag to show up? I've never tried doing this sort of thing, but what I know about Graphics2d tells me you shouldn't be able to draw a component by just making that component have a draw method. That usually requires some sort of inheritance, especially when we are talking about drawing. However, I am not entirely sure on that. I am unsure how sending Graphics2D objects through methods affects the ability to draw.
Re: need help w/ entry level homework...
Sorry, I have very little experience with programming, and posting code online. Sorry if my ettiquitte is is bad.... I'll try to clean it up. As far as the java tags are concerned, do I put highlight=java] and [/highlight before and after each class file that I post, or after all that I needed to post combined? Right now, I have 3 classes, from what I understand, ItalianFlag creates the object of the Italian flag. ItalianFlagViewer creates the JFrame window, and ItalianFlagComponent extends the JFrame componenet and creates the variable instructions for ItalianFlag. Again, I have been having trouble setting up the structure of my codes. I have been able to run it sucessfully in the regard that I get the Italian flag to open in a new JFrame window, but I cannot get it to run the two flags in the same window. I hope this helps and after I promise to use java tags next time (assuming I figure that out too)...
thanks again,
Brandon
Ok... think I've got the java tag thing down... i'll update my original post!
Re: need help w/ entry level homework...
You don't seem to have included the ItalianFlagViewer class above. Instead, you have included the ItalianFlag class and then the ItalianFlagComponent class twice.
Since you can get the 1 flag to paint, it would mean that they way you are doing it is correct, but needs to be tweaked. That is very good to know. Without seeing the ItalianFlagViewer class, I can assume that the problem is one of these two most common issues with Graphics:
1) When 1 Flag is drawn, the other is erased because of incorrectly overriding methods
2) The Second Flag is drawn directly over the 1st Flag, appearing as if only 1 Flag is drawn, while there are actually two.
Those are the two most common issues with Graphics and Graphics2D, and that is where I would assume the problem is. Another, less likely possibility, is that the 2nd Flag is printed outside of the viewable area of the ItalianFlagViewer. That is less likely because of the way drawing is done in JFrames. However, that problem still does occasionally happen.
BTW, your code tags are correct. I'm sure you can visibly see how much easier it is to read your code since it is now Color Coded and clearly separated. It also helps that the JAVA Standard Object Names are linked to their appropriate APIs as it makes it faster for people to help you to review the APIs for the Objects you are using. All in all, most of the more respected helpers on the forum will overlook your posts if your code isn't in tags. You'll probably see more people get involved now that your code is in tags.
Re: need help w/ entry level homework...
ok, fixed it to include the ItalianFlagViewer just for continuity sake. I think you are right that it is not the problem, but what do I know? And yes, I completely understand what the java tags help! thanks for making me a better poster.
Re: need help w/ entry level homework...
Are you able to provide a picture of the output?
I think I found your issue, it is obvious now that I actually payed attention to your constructors for ItalianFlag. Notice how you have two contructors, where you send them the x,y,width, and Color:
Code java:
public ItalianFlag(double x, double y, double aWidth) {
}
public ItalianFlag(double x, double y, double aWidth, Color c){
this.c= c;
}
Notice what you forgot? In the second Constructor you set the Object's c variable. However, in both Constructors you do not set the Object's x,y, and width variables. What does this mean? It means that each time you create an ItalianFlag object, its x and y variables default to 0 since you didnt set them.
What does this mean for your second flag? It means that the issue you have is #2 on the 2 most common issue with Graphics and Graphics2D. Fix your Constructors so that they set the x and y variables for the class, and that should fix your problem. I know you know how to do that, since you did it with the c variable in the second constructor.
Re: need help w/ entry level homework...
THANK YOU SO MUCH! I can't believe I for got to do that... well, actually I can. Maybe I won't do it again in the future, but I probably will. Again, thanks!