PDA

View Full Version : NEW Assignment CSC105



massiah
04-11-2004, 11:24 PM
Hello my friends. I've finish java assignment on last tuesday.
You can look at my applet from link below.
Here (http://202.44.9.126/~massiah/Applet/GUI_Applet.html)
Certainly, I've post the java code below.
This code is simple shape. You can write another method to draw other shape.
If you have a question you can ask me by post message in this webboard or ask me during daytime.
Good luck to U.

massiah
04-11-2004, 11:27 PM
import java.awt.*;
public class Pict extends Canvas
{
private Color col = new Color(5);
private int pic = 0;
public void setColor(Color c)
{
col = c;
}
public void setPict(String p)
{
if (p=="Square")
pic = 1;
else if (p=="Circle")
pic = 2;

}
public void paint(Graphics g)
{
g.setColor(col);
switch (pic)
{
case 1 : drawRect(g);break;
case 2 : drawOval(g);break;
}
}
public void drawRect(Graphics g)
{
g.drawRect(100,25,100,100);
g.fillRect(100,150,100,100);
}
public void drawOval(Graphics g)
{
g.drawOval(100,25,100,100);
g.fillOval(100,150,100,100);
}
}

massiah
04-11-2004, 11:29 PM
import CSLib.*;
import java.awt.*;
import java.awt.event.*;

public class DrawGUI extends ClosableFrame
implements ActionListener,ItemListener
{
Pict c = new Pict();
Choice choice1 = new Choice();
Button
bRed = new Button("Red"),
bGreen = new Button("Green"),
bBlue = new Button("Blue"),
bBlack = new Button("Black"),
bQuit = new Button("Quit");
Panel butts = new Panel();

public DrawGUI()
{
choice1.addItem("");
choice1.addItem("Square");
choice1.addItem("Circle");
butts.setLayout(new GridLayout(1,5));
butts.add(bRed);
butts.add(bGreen);
butts.add(bBlue);
butts.add(bBlack);
butts.add(bQuit);
setLayout(new BorderLayout());
add("North",choice1);
add("Center",c);
add("South",butts);
bRed.addActionListener(this);
bGreen.addActionListener(this);
bBlue.addActionListener(this);
bBlack.addActionListener(this);
bQuit.addActionListener(this);
choice1.addItemListener(this);
c.setSize(300,300);
c.setBackground(Color.lightGray);
pack();
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource()==bRed)
c.setColor(Color.red);
else if (e.getSource()==bGreen)
c.setColor(Color.green);
else if (e.getSource()==bBlue)
c.setColor(Color.blue);
else if (e.getSource()==bBlack)
c.setColor(Color.black);
else if (e.getSource()==bQuit)
{
getGraphics().dispose();
System.exit(1);
}
c.repaint();

}
public void itemStateChanged(ItemEvent e)
{
if (e.getSource()==choice1)
{
if (choice1.getSelectedItem()=="Square")
c.setPict("Square");
else if (choice1.getSelectedItem()=="Circle")
c.setPict("Circle");
}
c.repaint();
}
}

massiah
04-11-2004, 11:32 PM
public class DrawClient
{
public static void main(String args[])
{
new DrawGUI();
}
}

massiah
04-11-2004, 11:37 PM
The given code has 3 class.
You don't forget to copy package CSLib to folder because CSLib contains class name ClosableFrame.
I hope that code that I've posted can help you to understand the assignment very well.
You shouldn't copy all of my code.
The best way is you should implement this code and understand it.