d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > I Seriously Need Help With Basic Java. > Please. I Have A Online Class .
12Next
Add Reply New Topic New Poll
Member
Posts: 4,448
Joined: Nov 23 2004
Gold: 1,644.50
Sep 16 2016 09:16pm
I got myself into a intro to java course and it's proving to be too difficult for me to teach myself. I have a homework assignment due in a couple of days that might be easier fr someone more experienced.

PLEASE can I have some help? It's drawing a lighthouse with coordinates.

I will compensate if need be. We'll work out the details, but I seriously need a hand.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Sep 16 2016 09:39pm
what are you having problems with?

you're gonna have to start spending a lot more time on it. if you dont understand what you're doing now, you're gonna keep falling farther behind.

(don't pm me)
Member
Posts: 4,448
Joined: Nov 23 2004
Gold: 1,644.50
Sep 16 2016 09:42pm
ASSIGNMENT:
For your chapter 3 assignment you are to use the drawing shapes to create the DrawLighthouse.java program.
The coordinates for the base are provided so that you are able to create the project as closely as you can to the view.
g.setColor(black); (everything under this setting will be black until another color is set, everything below this will be black)
Base:
g.setColor(black);
g.fillRect(100, 550, 200, 30);
Specifications (bottom to top): tower, gray fill, polygon
 door, yellow fill, rectangle
 light, windows, yellow fill, rectangle
 top, red fill, polygon

Okay so.

I am loosely understanding the process of how to draw the shapes. He wants us to trial and error the whole lighthouse.

Base:
g.setColor(black);
g.fillRect(100, 550, 200, 30);

I added this code and it won't fill in the base.

fillRect doesn't even seem to be a command.

I'm just hopelessly lost on this.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Sep 16 2016 09:49pm
i assume it looks something like this? where g is a Graphics object you get from the parameter?
Code

public void paint (Graphics g) {
g.fillRect(100, 550, 200, 30);
}


explain this:
Quote
fillRect doesn't even seem to be a command.

are you getting a compile error? if so, what is it?
Member
Posts: 4,448
Joined: Nov 23 2004
Gold: 1,644.50
Sep 16 2016 09:49pm
There is no compile error, it just doesn't actually fill in the rectangle.

Nor does it create a shape at all.

This post was edited by nikkilina on Sep 16 2016 09:50pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Sep 16 2016 09:53pm
Quote (nikkilina @ Sep 16 2016 11:49pm)
There is no compile error, it just doesn't actually fill in the rectangle.


a few things to check.

1) verify your function (eg: paint) is getting called. use a debugger or log statement. (System.out.println(...))
2) verify your screen is the proper size. if you're writing to coordinates off screen, you won't see it. try smaller coordinates
g.fillRect(10, 10, 10, 10);
3) is anything getting drawn by any of your code?
Member
Posts: 4,448
Joined: Nov 23 2004
Gold: 1,644.50
Sep 16 2016 10:12pm
Okay well, this is what I am working with. The following is an example of drawing polygon shapes:

/*
documentation section
program:DrawPolygon
name: msullivan
date: january.2011
*/

/* import statement section */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class DrawPolygon extends JFrame
{
/* declaration section */
Color white = new Color(255, 255, 255);
Color black = new Color(0, 0, 0);


public DrawPolygon()
{
createUserInterface();
}

public void createUserInterface()
{
Container contentPane = getContentPane();
contentPane.setBackground(white);
contentPane.setLayout(null);

/* initialize components section */


setTitle("DrawPolygon");
setSize(400, 550);
setVisible(true);
}

/* main method */
public static void main(String[] args)
{
DrawPolygon application = new DrawPolygon();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public void paint(Graphics g)
{
super.paint(g);

g.setColor(black);
Polygon diamond = new Polygon();
diamond.addPoint(200, 350);
diamond.addPoint(300, 400);
diamond.addPoint(200, 450);
diamond.addPoint(100, 400);
g.drawPolygon(diamond);

Polygon square = new Polygon();
square.addPoint(200, 100);
square.addPoint(300, 100);
square.addPoint(300, 200);
square.addPoint(100, 200);
g.drawPolygon(square);

Polygon triangle = new Polygon();
triangle.addPoint(200, 250);
triangle.addPoint(100, 300);
triangle.addPoint(300, 300);
g.drawPolygon(triangle);

Polygon rect = new Polygon();
rect.addPoint (100, 0);
rect.addPoint (550, 0);
rect.addPoint (200, 5);
rect.addpoint (30, 5);
g.drawPonygon(rect);
g.fillRect(100, 550, 200, 30);




}
}

My biggest issue is that the book isn't covering a lot of what the teacher wants me to do. For example, the first homework assignment incorporated code that was not even discussed in chapter 1 or 2.

What I don't understand right now is:

What defines the size of the document? i.e 300x300

What do I do to draw the shapes? These code examples above do indeed draw these shapes. However, do I absolutely need to trial and error coordinates or is there an easier way?

The code above shows me how to draw shapes. When I want to fill in these shapes, do I need to separate the drawing code from the painting code? Or can I just add a line to that shape to paint it?

This post was edited by nikkilina on Sep 16 2016 10:13pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Sep 16 2016 10:25pm
Quote
What defines the size of the document? i.e 300x300


the jframe represents your window. Notice your class `DrawPolygon` inherits from JFrame. in particular, this function is telling the jframe the size:
Code
setSize(400, 550);


Quote
However, do I absolutely need to trial and error coordinates or is there an easier way?


You could do some simple math. you can see the coordinates you're already using. if you're a visual learner, then draw it by hand and label the coordinates you already know. shouldn't be too difficult to determine the other coordinates.

Quote
When I want to fill in these shapes, do I need to separate the drawing code from the painting code?


it's probably as simple as replacing drawXXX with fillXXX

https://docs.oracle.com/javase/7/docs/api/java/awt/Graphics.html#fillPolygon(java.awt.Polygon)
Member
Posts: 29,432
Joined: Mar 24 2011
Gold: 489.70
Warn: 40%
Sep 17 2016 06:21pm
Quote (carteblanche @ 16 Sep 2016 23:25)
the jframe represents your window. Notice your class `DrawPolygon` inherits from JFrame. in particular, this function is telling the jframe the size:
Code
setSize(400, 550);




You could do some simple math. you can see the coordinates you're already using. if you're a visual learner, then draw it by hand and label the coordinates you already know. shouldn't be too difficult to determine the other coordinates.



it's probably as simple as replacing drawXXX with fillXXX

https://docs.oracle.com/javase/7/docs/api/java/awt/Graphics.html#fillPolygon(java.awt.Polygon)


Still helping people I see! I'm finally on my way! Thanks for telling me to keep my head up when it comes to learning raw coding and programming!
Member
Posts: 4,448
Joined: Nov 23 2004
Gold: 1,644.50
Sep 17 2016 08:48pm
Okay I have another question.

g.setColor(gray);
Polygon square = new Polygon();
square.addPoint(120, 250);
square.addPoint(110, 550);
square.addPoint(285, 550);
square.addPoint(280, 250);
g.drawPolygon(square);

I am trying to fill this square in with gray. The issue is, I can't do a g.fillRect because this shape is slanted.

How do I fill this in with gray paint?
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll