sturabumukiza
10-24-2009, 08:52 AM
can someone help! I am working with graphics primitives and I am having a problem in creating 5 drawings in 5 rectangular regions within one window.The following is what i have managed to come up with.
import java.applet.*;
import java.awt.*;
import javax.swing.*;
public class Circleline extends JApplet{
int x=300,y=100,r=50;
int xPoints[] = {50,80,80,50,50}; //Sets x coordinates to draw to/from
int yPoints[] = {50,50,80,80,50}; //Sets y coordinates to draw to/from
public void paint(Graphics Obj,Graphics g) //paint function (essential for graphics)
{
Obj.setColor(Color.black); //Sets the color to green
Obj.fillPolygon(xPoints,yPoints,xPoints.length); //Uses the x,y coordinates declared earlier to draw a filled square
for(int i = 0; i < xPoints.length;i++) //for loop to add to x values to move it across screen
xPoints[i] += 200;
Obj.drawPolyline(xPoints,yPoints,xPoints.length); //draws an empty square at the new x values and same y values
}
public void paint(Graphics g){
g.drawLine(3,300,200,10);
g.drawString("Line",100,100);
g.drawOval(x-r,y-r,100,100);
g.setColor(Color.blue);
g.fillOval(x-r,y-r,100,100);
g.setColor(Color.magenta);
g.drawString("Circle",275,100);
g.fillRect(400,50,200,100);
g.setColor(Color.pink);
g.drawRect(400,50,200,100);
g.drawString("Rectangle",450,100);
g.setColor(Color.yellow);
g.drawRect(250,250,250,250);
g.fillRect(250,250,250,250);
g.setColor(Color.gray);
g.drawString("Square",350,350);
}
public void init() //Initialize
{
setBackground(Color.cyan); //Make window background cyan
setSize(400,400); //Set window size
}
}
Please help me so that the code can output 5 drawings in 5 rectangular regions within one window. Thank you
import java.applet.*;
import java.awt.*;
import javax.swing.*;
public class Circleline extends JApplet{
int x=300,y=100,r=50;
int xPoints[] = {50,80,80,50,50}; //Sets x coordinates to draw to/from
int yPoints[] = {50,50,80,80,50}; //Sets y coordinates to draw to/from
public void paint(Graphics Obj,Graphics g) //paint function (essential for graphics)
{
Obj.setColor(Color.black); //Sets the color to green
Obj.fillPolygon(xPoints,yPoints,xPoints.length); //Uses the x,y coordinates declared earlier to draw a filled square
for(int i = 0; i < xPoints.length;i++) //for loop to add to x values to move it across screen
xPoints[i] += 200;
Obj.drawPolyline(xPoints,yPoints,xPoints.length); //draws an empty square at the new x values and same y values
}
public void paint(Graphics g){
g.drawLine(3,300,200,10);
g.drawString("Line",100,100);
g.drawOval(x-r,y-r,100,100);
g.setColor(Color.blue);
g.fillOval(x-r,y-r,100,100);
g.setColor(Color.magenta);
g.drawString("Circle",275,100);
g.fillRect(400,50,200,100);
g.setColor(Color.pink);
g.drawRect(400,50,200,100);
g.drawString("Rectangle",450,100);
g.setColor(Color.yellow);
g.drawRect(250,250,250,250);
g.fillRect(250,250,250,250);
g.setColor(Color.gray);
g.drawString("Square",350,350);
}
public void init() //Initialize
{
setBackground(Color.cyan); //Make window background cyan
setSize(400,400); //Set window size
}
}
Please help me so that the code can output 5 drawings in 5 rectangular regions within one window. Thank you