Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1411246 – Object Oriented programming with Java Homework #1 eclipse 1411246- Obj

ID: 3674418 • Letter: 1

Question

1411246 – Object Oriented programming with Java Homework #1

eclipse

1411246- Object Oriented programming with Java Homework #1 Spring 2015/2016 Due Date: Mar. 6, 2016 Objectives: . Learn drawing techniques on an applet . Use Graphics2D API How to complete this HW 1. First read and analyze each problem; then implement both problems in a project called HW1. 2. For each exercise, insert comments on ID, Name, and HW# 3. Submit the zipped solution on BB link. The name of each file should be HW1_qt_myID_.java For example, HW1-q1_a000 111 11.java (a? stands for question #) Note on Honor Code: You must NOT look at previously published solutions of any of these problems in preparing your answers. You may discuss these problems with other students in the class (in fact, you are encouraged to do so) and/or look into other documents (books, web sites), with the exception of published solutions, without taking any written or electronic notes. If you have discussed any of the problems with other students, indicate their name(s) in the comments section at the top of your program. Any intentional transgression of these rules will be considered an honor code violation. 1. City Scape Applet You have been hired to design a city scape of buildings. This is a competition! Top city scapes in the class will receive extra credit on this assignment. Get creative! Below is an example of what you could do. Grading will be based on adherence to the following specifications. You must name your file CityScape.java exactly than 800 pixels in each direction date, ID, name, and HW number The dimensions of your applet must be at least 500 pixels in each direction and no more You must add a comment at the top of your code stating that you are the author, the You must print your first name on top of one of the buildings You must use at least three different colors (you can pick the colors) You must color in the background - do not use setBackground instead, use fillRect and fill the entire dimensions of your applet before you start drawing. .Create your own drawing; use the applet output as a guideline You must use each of the following methods at least once a. drawLine

Explanation / Answer

Inlcude the below Code

Keep all files in one folder just run CitySpace.java file.

Commands to run

To Compile:

javac CitySpace.java

To Run:

java CitySpace

It is wrking fine..

CitySpace.java


import java.applet.Applet;
import java.awt.*;
import java.util.*;
import javax.swing.JFrame;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;

public class CitySpace extends JFrame implements MouseMotionListener
{
private int mX, mY; //Mouse cooddinates
private Image mImage; //Image buffer

private int num = 0;

private Building bldg1 = new Building(305, 110, 30);
private Building bldg2 = new Building(380, 125, 170);
private Building bldg3 = new Building(245, 200, 325);
private Building bldg4 = new Building(470, 170, 555);
private Building bldg5 = new Building(395, 200, 755);
private Background bg = new Background();

public void init ()
{
}

public static void main(String []args)
{
CitySpace f = new CitySpace();

f.setSize(1017, 661); //Sets size of window
f.setTitle("CitySpace"); //Sets title of window
f.show();
}

public void paintOffscreen(Graphics page)
{
//Draws the background
bg.draw(page);

//Moving square
num++;
if (num > 1200)
num = 0;
page.setColor(Color.yellow);
page.fillRect(num,100,100,100);

//Draws the buildings
bldg1.draw(page);
bldg2.draw(page);
bldg3.draw(page);
bldg4.draw(page);
bldg5.draw(page);

//Mouse move square
int s = 100;

page.setColor(Color.yellow);
page.fillRect(mX - s / 2, mY - s / 2, s, s);

repaint();
}

//====================================BUFFER CODE========================================
public void paint(Graphics g)
{
//Clear the buffer
Dimension d = getSize();
checkOffscreenImage();
Graphics offG = mImage.getGraphics();
offG.setColor(getBackground());
offG.fillRect(0, 0, d.width, d.height);

//Save frame to buffer
paintOffscreen(mImage.getGraphics());

//Draw the buffer
g.drawImage(mImage, 0, 0, null);

}

private void checkOffscreenImage()
{
Dimension d = getSize();

if (mImage == null || mImage.getWidth(null) != d.width || mImage.getHeight(null) != d.height)
mImage = createImage(d.width, d.height);
}

public CitySpace()
{
addMouseMotionListener(this);
setVisible(true);
}

public void mouseMoved(MouseEvent me)
{
Graphics g = getGraphics();
mX = (int) me.getPoint().getX();
mY = (int) me.getPoint().getY();
update(g);
//repaint();
}

public void mouseDragged(MouseEvent me)
{
mouseMoved(me);
}

}

Windows.java:

import java.applet.Applet;
import java.awt.*;
import java.util.Random;
import javax.swing.JFrame;

/*
* This class driver for Cityspace.java class
*/
public class Windows extends JFrame
{
private Random gen = new Random();
private int height, width, locX;
private int>

public Windows()
{
height = 305;
width = 110;
locX = 30;
}

public Windows(int height, int width, int locX)
{
this.height = height;
this.width= width;
this.locX = locX;
}

public void draw(Graphics page)
{
page.setColor (Color.darkGray);

page.fillRect (locX, 550 - height, width, height);

for (int i = 550 - height + 5; i < 550; i += 15)
{
for (int x = locX + 5; x < locX + width; x += 15)
{
>

if(onOff == 0)
page.setColor(Color.black);
else
page.setColor(Color.yellow);

page.fillRect (x,i,10,10);
}
}
}
}

Building.java:


import java.applet.Applet;
import java.awt.*;
import javax.swing.JFrame;

public class Building extends JFrame
{
private int hgt, wth, locX;
private int onOff;
private Windows windows1;
//this is Windows class object..

//this i Building class object..
public Building()
{
hgt = 305;
wth = 110;
locX = 30;

windows1 = new Windows(hgt, wth, locX);
}

public Building(int hgt, int wth, int locX)
{
this.wth = wth;
this.hgt = hgt;
this.locX = locX;

windows1 = new Windows(hgt, wth, locX);
}

public void draw(Graphics page)
{
page.setColor (Color.darkGray);

page.fillRect (locX, 550 - hgt, wth, hgt);

windows1.draw(page);
}
}

Background.java


import java.applet.Applet;
import java.awt.*;

public class Background extends Applet
{
private int height, width;

public Background()
{
height = 400;
width = 2000;
}

public Background(int height, int width)
{
this.height = height;
this.width = width;
}

public void draw(Graphics page)
{
//Draws the sky
page.setColor(Color.cyan);
page.fillRect(0,0,2000,2000);
//Draws the grass
page.setColor (Color.green);
page.fillRect (0,500,width,height);
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote