Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Java Applet Help, Please. :D

  • 22-11-2004 3:42pm
    #1
    Registered Users, Registered Users 2 Posts: 234 ✭✭


    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. 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) { }

    }


Comments

  • Registered Users, Registered Users 2 Posts: 307 ✭✭Thordon


    I dont have time to pore over it atm, but one thing I noticed was that you load the image every time you call repaint, and as far as I know, that method of loading images will not wait until the image is loaded, so what could be happening, is its drawing a blank unloaded image onto the frame, never getting a chance to load. To fix this, have a class object Image, load it in init, and then just draw that image instead of loading it every time.

    Another thing is that the paint method refers to the Applet context, not the canvas context, so you could be drawing over buttons and stuff.


  • Registered Users, Registered Users 2 Posts: 234 ✭✭Yelnahs


    thanks for your time mate, i actually re-did the whole thing last night i got so frustrated at it.

    So, thankfully i only have a small problem. I have a number of if statements in the actionPerformed method, all of these use the Graphics method which i cant pass in because the actionPerformed method has to be specifically defined. Any ideas of a way around this?


  • Registered Users, Registered Users 2 Posts: 17,727 ✭✭✭✭Sherifu


    You could call a different method from actionPerformed to handle Graphics provided the Graphics object is in its scope.


  • Registered Users, Registered Users 2 Posts: 234 ✭✭Yelnahs


    right i actually got around this by using the getGraphics(); command. I am now fully up and running with a few minor glitches to iron out, thank you fro your input guys.


Advertisement