import java.awt.Color; import java.awt.Graphics; public class Box { private int
ID: 3644984 • Letter: I
Question
import java.awt.Color;import java.awt.Graphics;
public class Box {
private int upperLeftX;
private int upperLeftY;
private int height;
private int width;
private Color boxColor;
public Box(int upperX, int upperY, int h, int w, Color myColor){
this.upperLeftX = upperX;
this.upperLeftY = upperY;
this.height = h;
this.width = w;
this.boxColor = myColor;
}
public int getUpperLeftX() {
return upperLeftX;
}
public void setUpperLeftX(int upperLeftX) {
this.upperLeftX = upperLeftX;
}
public int getUpperLeftY() {
return upperLeftY;
}
public void setUpperLeftY(int upperLeftY) {
this.upperLeftY = upperLeftY;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public Color getBoxColor() {
return boxColor;
}
public void setBoxColor(Color boxColor) {
this.boxColor = boxColor;
}
public void display(Graphics g){
g.fillRect(upperLeftX,upperLeftY,width,height);
g.setColor(boxColor);
}
}
import java.awt.*;
import java.applet.Applet;
public class Grid extends Applet {
Box Box1 = new Box(0, 0, 10, 10, Color.black);
Box Box2 = new Box(10, 0, 10, 10, Color.blue);
Box Box3 = new Box(20, 0, 10, 10, Color.green);
Box Box4 = new Box(30, 0, 10, 10, Color.red);
Box Box5 = new Box(0, 10, 10, 10, Color.cyan);
Box Box6 = new Box(10, 10, 10, 10, Color.darkGray);
Box Box7 = new Box(20, 10, 10, 10, Color.magenta);
Box Box8 = new Box(30, 10, 10, 10, Color.orange);
Box Box9 = new Box(0, 20, 10, 10, Color.pink);
Box Box10 = new Box(10, 20, 10, 10, Color.yellow);
Box Box11 = new Box(20, 20, 10, 10, Color.darkGray);
Box Box12 = new Box(30, 20, 10, 10, Color.blue);
Box Box13 = new Box(0, 30, 10, 10, Color.magenta);
Box Box14 = new Box(10, 30, 10, 10, Color.green);
Box Box15 = new Box(20, 30 , 10, 10, Color.pink);
Box Box16 = new Box(30, 30, 10, 10, Color.yellow);
}
Just want to know to use the public void display function to display the boxes i.e. the code
Explanation / Answer
import java.applet.*; import java.awt.*; public class HelloApplet extends Applet { //The paint() method is called by the browser whenever the applet must be redrawn public void paint (Graphics g) { Dimension d = new Dimension(); d=getSize(); //the size of the applet that is.... int w = d.width; int h = d.height; String message = new String(); message = "Hello World!!!!!!"; g.drawString (message, w/2, h/2); //NOTE: Using the coordinates w/2 and h/2 allows // us to center the message. } } import java.applet.*; import java.awt.*; public class Play extends Applet { //The paint() method is called by the browser whenever the applet must be redrawn public void paint (Graphics g) { //In the following comments, I'll use the convention that //x1, y1, x2, y2 are the x and y coordinates of //points 1 and 2 respectively g.setColor (Color.blue); g.drawLine (30, 60, 60, 30); //(x1, y1, x2, y2) //In the following, all of the figures are based on a //rectangle defined by the x and y coordinates of the //top left corner and the width and height of the rectangle g.drawRect (90, 30, 40, 30); //(x, y, width, height) g.fillRect (150, 30, 50, 30); //(x, y, width, height) g.drawOval (220, 30, 70, 30); //(x, y, width, height) //NOTE: You can create your own colors as follows: Color matt = new Color (200, 100, 250); g.setColor (matt); //g.setColor (Color.red); g.fillOval (30, 80, 70, 30); //(x, y, width, height) g.fillArc (110, 80, 40, 30, 45, 315); //(x, y, width, height, starting angle, sweep angle) g.drawRoundRect(170, 80, 60, 30, 30, 10); //(x, y, width, height, xpixels, ypixels) //where xpixels and ypixels are the number //of pixels in from the corners where rounding //begins to occur g.fillRoundRect (250, 80, 50, 30, 20, 20); //(x, y, width, height, xpixels, ypixels) } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.