Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 2 of 2 FirstFirst 12
Results 26 to 43 of 43

Thread: Drawing in Java

  1. #26
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Drawing in Java

    Illegal static declaration in inner class Window.Point
    Those classes are declared inside of another class. They need to be declared as static.
    I said that earlier in post#9
    If you don't understand my answer, don't ignore it, ask a question.

  2. #27
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Drawing in Java

    Hmmm,I see but if i declare the classes Static I cant acceses the method fromInput? How can i make this thing draw it out,I'm really trying my hardest but I'm just so lost like what changes do i need to do in order to make the program FINALLY working

  3. #28
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Drawing in Java

    cant acceses the method fromInput?
    Look at post#22 for how to call the fromInput method.

    What is in the In class? How are you supposed to use it to find and read the data file?
    I see it is used in the fromInput methods.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #29
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Drawing in Java

    I tried calling the fromInput method like that and it doesn't work; If i put both the class Point as well as the fromInput method static it compiles errors.In the assignment it says that the classes Point Line Rectangle shouldn't be static,but only the fromInput methods.But even when I try it like that it still doesn't work. The In.class is used to Open and read through the txt file. The In.readInt() reads the next Integrer value it finds, the In.readChar() the next Charachter it finds.

  5. #30
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Drawing in Java

    it doesn't work
    AND
    it compiles errors
    Please explain. Copy and paste the full text of any error messages.

    Here is the Point class I am using and that works:
    //-----------------------------------
    static class Point   {                
    	int x;
    	int y;
     
    	Point(int x, int y) {
    		this.x = x;
    		this.y = y;
     
     
    	}	
       static Point fromInput() {  
           int x = In.readInt();
           int y = In.readInt();
     
    	if (In.done()) return new Point(x, y);
            else return null;
       }
       public void drawMe(MyWindow mw) {      //<<<<<<< ADDED
          mw.drawPoint(x,y);  
       }
    }
    If you don't understand my answer, don't ignore it, ask a question.

  6. #31
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Drawing in Java

    So if i paste this into the code with the class Window and all of that it should work? Okay i will try and let you know what happens

  7. #32
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Drawing in Java

    all of that it should work
    Probably not. There needs to be changes to all the classes as per the Point class example.

    Compare my code with yours in post#16.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #33
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Drawing in Java

    Yea yea I got that I need to do it for all classes like you did for Point.Sorry my laptop ran out of batter I'll try it now.

    --- Update ---

    Window.java:323: error: cannot find symbol
    public void drawMe(MyWindow mw) { //<<<<<<< ADDED
    ^
    symbol: class MyWindow
    location: class Point
    Window.java:405: error: cannot find symbol
    w.drawCircle(x, y, r );
    ^
    symbol: variable w
    location: class Circle
    Window.java:419: error: cannot find symbol
    r.draw(w);
    ^
    symbol: method draw(Window)
    location: variable r of type Rectangle
    Window.java:423: error: cannot find symbol
    c.draw(w);
    ^
    symbol: method draw(Window)
    location: variable c of type Circle
    4 errors

    C:\Users\Hp\Desktop\figuren>

    What should I change, I only did it for point to test and this came out

    EDIT: I saw that the name of MyWindow and mw are diffrent and taht is why it was compiling it like that,i fixed it but now only the Window pops up but no image?
    Last edited by arhzz; April 29th, 2020 at 02:46 PM.

  9. #34
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Drawing in Java

    Window.java:323: error: cannot find symbol MyWindow
    See the last line in post#2

    Window.java:405: error: cannot find symbol
    w.drawCircle(x, y, r );
    ^
    symbol: variable w
    Where is the variable w declared? The compiler can not find it.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #35
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Drawing in Java

    Hey just want to say that i finally managed to compile the program and it works now (partially) Thank you so much for your help,I'd never do it without you honestly.SO now it actually only prints out the first line, it doesnt actually go to the end of the txt.file So i'm thinking an for loop that will tell it to go till the end? that could work right?

  11. #36
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Drawing in Java

    Yes, you will need a loop to continue reading through the input file until the last record has been processed.
    A for loop is usually used when you know before the loop starts, how many iterations it will do.
    A while loop is used when the end of the loop condition is detected while the loop is running.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #37
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Drawing in Java

    Hmmm,I see so actuall using a while would be much better in my case.I'll try a few things and we'll see what happens,Thanks for the advice!

    --- Update ---

    Okay so i tried it a few ways and it isnt working so hot;

     
    public static void main(String[] args){
    	In.open("shapes.txt");
    	int width = In.readInt();
    	int height = In.readInt();
    	do {
    	Window w = Window.create(width,height);
    	char ch = In.readChar();
    	switch(ch){
    		case 'L':
    		Line l = Line.fromInput();
    		l.draw(w);
    		break;
    		case 'R':
    		Rectangle r = Rectangle.fromInput();
    		r.draw(w);
    		break;
    		case 'C':
    		Circle c = Circle.fromInput();
    		c.draw(w);
    		break;
    	 }
    	}while(In.done());
    In.close();
    }
    }

    Is my condition wrong, or the "position" of my do while loop

    EDIT:

    I got the code now to get the picture out, but it is "dislocated" not like it is susposed to be, what could be the cause of that

    public static void main(String[] args){
    	In.open("shapes.txt");
    	int width = In.readInt();
    	int height = In.readInt();
    	Window w = Window.create(width,height);
    	do{
    	char ch = In.readChar();
    	switch(ch){
    		case 'L':
    		Line l = Line.fromInput();
    		l.draw(w);
    		break;
    		case 'R':
    		Rectangle r = Rectangle.fromInput();
    		r.draw(w);
    		break;
    		case 'C':
    		Circle c = Circle.fromInput();
    		c.draw(w);
    		break;
    	 }
    	}while(In.done());
    In.close();
    }
    }
    Last edited by arhzz; April 29th, 2020 at 04:06 PM.

  13. #38
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Drawing in Java

    it is "dislocated" not like it is susposed to be
    What does that mean?
    Take some graph paper and use the instructions in the data file to draw the image by hand to see what its contents represents.
    If you don't understand my answer, don't ignore it, ask a question.

  14. #39
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Drawing in Java

    I did, and it is susposed to be a house with a sun in the top left corner, but the image that pops up on my screen, the roof of the house is way off as well as one of the windows, although the door and other window are in the right place. Some parts are right others arent.

  15. #40
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Drawing in Java

    Did your manual drawing match what it was supposed to be
    or did it match what the program drew?
    If you don't understand my answer, don't ignore it, ask a question.

  16. #41
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Drawing in Java

    Actually the profesor,gave us what the image should look like accoriding to the coordinates that the txt contains.

  17. #42
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Drawing in Java

    Did your manual drawing match what it was supposed to be (ie what you got from the professor)
    or did it match what the program drew?
    If you don't understand my answer, don't ignore it, ask a question.

  18. The Following User Says Thank You to Norm For This Useful Post:

    arhzz (April 30th, 2020)

  19. #43
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Drawing in Java

    Yea,it did match what I got from the professor.

    EDIT: I figured it out, the readChar in rectangle and circle were "eating" the first number in numbers like 50 40 etc... So after removing that It works like a charm.
    Thanks so much for your help Norm.
    Last edited by arhzz; April 30th, 2020 at 01:41 AM.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Java not drawing my string
    By bartreijmer in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 1st, 2013, 08:09 AM
  2. Drawing Image in Java
    By Shania_01 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 15th, 2013, 12:10 PM
  3. [SOLVED] Drawing lines using angles in Java HELP
    By shadysback in forum What's Wrong With My Code?
    Replies: 7
    Last Post: October 8th, 2012, 02:10 PM
  4. [SOLVED] Java Graphics: Drawing one object into another
    By zaxahmed in forum Object Oriented Programming
    Replies: 5
    Last Post: August 22nd, 2011, 08:06 AM
  5. Incremental image drawing in Java
    By ea25 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 14th, 2011, 07:58 AM