Java Applet Help, Please.


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Java Applet Help, Please.

  1. #1
    Join Date
    Nov 2004
    Posts
    1

    Java Applet Help, Please.

    Ok, what i am trying to do is have 2 a Choice box's, when the two counties are selected the submit button is pressed and it draws a line over the image from the co-ordinates of the first county, to the co-ordinates of the second one. I am currently using a canvas, but i dont have to. This is what i have so far, but the image just wont load, i think it is because the canvas is being painted over it.

    Can anyone please help me out with this? with possible solutions or tips or code or anything!

    Thanks in advance.


    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.*;
    import java.net.*;
    import java.applet.*;
    import java.util.*;


    public class LineDraw extends Applet
    {
    SimpleDrawCanvas canvas;
    Choice CountyChooser;
    Choice CountyChooser2;
    Image objImage;

    public void init()
    {
    canvas = new SimpleDrawCanvas();
    Panel bottom = new Panel();
    Panel right = new Panel();
    Panel top = new Panel();

    CountyChooser = new Choice(); //First County Chooser
    CountyChooser2 = new Choice();//Second Country Chooser

    CountyChooser.add("Start County");
    CountyChooser.add("Antrim");
    CountyChooser.add("Armagh");
    CountyChooser.add("Carlow");
    CountyChooser.add("Cavan");
    CountyChooser.add("Clare");
    CountyChooser.add("Cork");
    CountyChooser.add("Derry");
    CountyChooser.add("Donegal");
    CountyChooser.add("Down");
    CountyChooser.add("Dublin");
    CountyChooser.add("Fermanagh");
    CountyChooser.add("Galway");
    CountyChooser.add("Kerry");
    CountyChooser.add("Kildare");
    CountyChooser.add("Kilkenny");
    CountyChooser.add("Laois"); // ^^^^^^^^^^^^^^^^^ALL 32 COUNTIES, FIRST LIST
    CountyChooser.add("Letrim");
    CountyChooser.add("Limerick");
    CountyChooser.add("Longford");
    CountyChooser.add("Louth");
    CountyChooser.add("Mayo");
    CountyChooser.add("Meath");
    CountyChooser.add("Monaghan");
    CountyChooser.add("Offaly");
    CountyChooser.add("Roscommon");
    CountyChooser.add("Sligo");
    CountyChooser.add("Tipperary");
    CountyChooser.add("Tyrone");
    CountyChooser.add("Waterford");
    CountyChooser.add("Westmeath");
    CountyChooser.add("Wexford");
    CountyChooser.add("Wicklow");

    //CountyChooser2 list...

    CountyChooser2.add("End county");
    CountyChooser2.add("Antrim");
    CountyChooser2.add("Armagh");
    CountyChooser2.add("Carlow");
    CountyChooser2.add("Cavan");
    CountyChooser2.add("Clare");
    CountyChooser2.add("Cork");
    CountyChooser2.add("Derry");
    CountyChooser2.add("Donegal");
    CountyChooser2.add("Down");
    CountyChooser2.add("Dublin");
    CountyChooser2.add("Fermanagh");
    CountyChooser2.add("Galway");
    CountyChooser2.add("Kerry");
    CountyChooser2.add("Kildare");
    CountyChooser2.add("Kilkenny");
    CountyChooser2.add("Laois"); // ^^^^^^^^^^^^^^^^^ALL 32 COUNTIES, SECOND LIST
    CountyChooser2.add("Letrim");
    CountyChooser2.add("Limerick");
    CountyChooser2.add("Longford");
    CountyChooser2.add("Louth");
    CountyChooser2.add("Mayo");
    CountyChooser2.add("Meath");
    CountyChooser2.add("Monaghan");
    CountyChooser2.add("Offaly");
    CountyChooser2.add("Roscommon");
    CountyChooser2.add("Sligo");
    CountyChooser2.add("Tipperary");
    CountyChooser2.add("Tyrone");
    CountyChooser2.add("Waterford");
    CountyChooser2.add("Westmeath");
    CountyChooser2.add("Wexford");
    CountyChooser2.add("Wicklow");

    Button b1 = new Button("Clear"); //Button clear is declared and created
    b1.addActionListener(canvas); // b1 is added to the actionlistener on the canvas
    bottom.add(b1); //b1 is then added to the bottom pane.

    Button b2 = new Button("Submit");
    b2.addActionListener(canvas);

    Label info = new Label("Please select a start and end county");
    top.add(info);
    right.add(CountyChooser); //adding County Choosers to the right pane
    right.add(CountyChooser2);
    right.add(b2);
    setBackground(Color.blue);

    setLayout(new BorderLayout(3,3)); // Various positions for componants
    add("Center",canvas);
    add("South",bottom);
    add("East",right);
    add("North", top);


    }

    public Insets getInsets()
    {

    return new Insets(10,10,10,10);
    }

    public void paint(Graphics g)
    {
    objImage = Toolkit.getDefaultToolkit().getImage(getClass().getResource("Image.gif"));
    g.drawImage(objImage, 0, 0, this);

    }

    public void conditions(Graphics2D g2d, Choice CountyChooser, Choice ChountyCooser2, ActionEvent e)
    {

    String command = e.getActionCommand();

    int county1 = CountyChooser.getSelectedIndex();
    int county2 = CountyChooser.getSelectedIndex();

    // ALL THE IF STATEMENTS GO IN HERE

    if (command.equals("Submit"))

    g2d.drawLine(100,100,200,300);

    }
    }


    class SimpleDrawCanvas extends Canvas implements ActionListener, MouseListener
    {

    Color currentColor = Color.red;

    SimpleDrawCanvas()
    {
    setBackground(Color.green);
    currentColor = Color.red;
    addMouseListener(this);

    }

    void doClear()
    {
    repaint();
    }

    public void actionPerformed(ActionEvent evt)
    {

    String command = evt.getActionCommand();
    if (command.equals("Clear"))
    doClear();
    }

    public void mousePressed(MouseEvent evt) { }
    public void mouseReleased(MouseEvent evt) { }
    public void mouseClicked(MouseEvent evt) { }
    public void mouseEntered(MouseEvent evt) { }
    public void mouseExited(MouseEvent evt) { }

    }

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    I'm not quite sure that you are doing it the kosher way...

    When I retrieve images for an applet i do it like this;

    String fileURLString="http://www.a.b/images/anImg.gif";

    URL imgURL=new URL(fileURLString);

    Image anImage=getImage(imgURL);
    Mediatracker mt=new Mediatracker(this);
    mt.addImage(imgURL,0);
    try {
    mt.waitForAll(); // cant draw images that haven't arrived
    } catch (InterruptedException ie) {
    System.err.println("damm");
    return;
    }

    Another thing, the Canvas class is lame, on some platforms it is deaf to museclicks, at least it was for Mac a few years ago, use a straight Panel instead, it does the same trick, Canvas is obsolete.

    Your SimpleDrawCanvas is very simple, it doesn't do anything but repaint its own empty self. You should get your image as I showed above, pass it to the SimpleDrawPanel (...) and paint it there. The panel will repaint when the applet repaints. And... it sould override the paint method.

    Good Luck !
    eschew obfuscation

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center