Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hello

 
Jose Carrillo
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi I'm having a problem and I do not find what is problem well. I creating an applet with 4 buttons draw lines, rect, ovals, and fill. when fill button is click it will draw the rect and ovals fill. well my program compile and everything but I does not load the applet in the web can somebody help please. here is my code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.util.EventObject;
public class mouseDraw extends JApplet implements MouseListener, MouseMotionListener, ActionListener
{
private int mouseX, mouseY, mouseY1, mouseX1;
private JButton[] buttons = new JButton[4];
private JPanel controlPanel, drawPanel;
private int shape;
private Button fillButton;
private Panel currentcolorPanel;
public static boolean FillShape;
public void init()
{
buttons[0] = new JButton("Line");
buttons[1] = new JButton("Rectangle");
buttons[2] = new JButton("Oval");
buttons[3] = new JButton("Fill");
buttons[0].setBackground(Color.red);
currentcolorPanel.add(fillButton);
fillButton.addActionListener(this);
controlPanel = new JPanel();
controlPanel.setLayout(new FlowLayout());
controlPanel.setBackground(Color.orange);
drawPanel = new JPanel();
drawPanel.setBackground(new Color(0xffffcc));
for (int i=0; i<4; i++)
controlPanel.add(buttons[i]);
Container pane = getContentPane();
pane.setLayout(new BorderLayout());
pane.add(controlPanel, BorderLayout.NORTH);
pane.add(drawPanel, BorderLayout.CENTER);
for (int i=0; i<4; i++)
buttons[i].addActionListener(this);
addMouseListener(this);
addMouseMotionListener(this);
}
public void paint(Graphics g)
{
super.paint(g);
int x = Math.min(mouseX1, mouseX);
int y = Math.min(mouseY1, mouseY);
g.setColor(Color.blue);
int w = (int)Math.abs(mouseX1 - mouseX);
int h = (int)Math.abs(mouseY1 - mouseY);
switch (shape)
{
case 0: g.drawLine(mouseX1, mouseY1, mouseX, mouseY);
int [] a = new int [x];
int [] z = new int [y];
break;
case 1: g.drawRect(x, y, w, h);
int [] b = new int [x];
break;
case 2: g.drawOval(x, y, w, h);
int [] c = new int [x];
break;
case 3: g.fillRect(x, y, w, h);
break;
}
}
public void mouseMoved(MouseEvent e)
{
mouseX = e.getX();
mouseY = e.getY();
showStatus("Mouse is moved to (" + mouseX + mouseY + ")");
}
public void mouseDragged(MouseEvent e)
{
mouseX = e.getX();
mouseY = e.getY();
showStatus("Mouse is moved to (" + mouseX + mouseY + ")");
repaint();
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}
public void mouseReleased(MouseEvent e)
{
}
public void mousePressed(MouseEvent e)
{
mouseX1 = mouseX;
mouseY1 = mouseY;
}
public void mouseClicked(MouseEvent e)
{
}
public void actionPerformed(ActionEvent actionevent)
{
if(actionevent.getSource() == fillButton)
{
if(FillShape)
{
FillShape = false;
fillButton.setLabel("Unfilled");
return;
}
FillShape = true;
fillButton.setLabel("Filled");
}
for (int i=0; i<4; i++)
{
buttons[i].setBackground(Color.lightGray);
if (actionevent.getSource() == buttons[i])
{
shape = i;
buttons[i].setBackground(Color.red);
}
}
}
}
thanks
 
Jasmine kaur
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello ,
Well you want to run applet then you should use appletviewer whivh is present in the bin folder but before that you have to put the applet code tag in your source code only then it would run sucessfully on the web.
so put this cod ein your source code
as **/<applet code="myapllet.class"Height=200width=200>
</applet> **/
you have to put the same synatx in your source code and then compile your code then you can run your applet like this
appletviewer myapplet.java so you can easily see your applet then you can put this in html file by putting the applet code tag and run the html file on browser .
So do this you code will run .If you face some confusion you can ask more.
Thanks
Jasmine kaur
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic