Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Fixing display

MongloidooMongloidoo Member Posts: 52
I currently am working on a program that takes a large group of dots and sorts them into a diagonal line. There are 2 radio buttons on the application, one that sorts them as dots and one that sorts them as lines. I created the method to make the dots and the lines.


if (mode == "lines") {
// Draw new lines
g.setColor(Color.black);
y = 500;
for (int i = arrayLength; --i >= 0; y -= 2) {
g.drawLine(0, y, dataArray[i], y);
}
} else {
// Draw new dots
g.setColor(Color.black);
y = 500;
for (int i = arrayLength; --i >= 0; y -= 2) {
g.drawLine(dataArray[i] - 1, y, dataArray[i], y);
}
}

But i when i start up my application i only get the dots. I added actionlisteners to the dot and the line button but i have a feeling something is wrong with my if then statement.

if (src == linesRadioBtn)
drawingMode = "Lines";
else
if (src == dotsRadioBtn)
drawingMode = "Dots";


I am not quite sure what i am doing incorrectly, i would appreciate any help you could give me. Thank you so much.


Mongloid

Comments

  • IllcoIllco Member Posts: 382
    You cannot compare strings like this:
    [code]
    String s1 = ...;
    String s2 = ...;

    if ( s1 == s2 ) ...
    [/code]
    This only compares the adresses of both objects and since they were not created together, the adresses are not the same (altough their content may be!).
    Comparing strings is done with the equals() method:
    [code]
    if ( s1.equals( s2 ) ) ...
    [/code]
    Note that for this s1 must be non-null!!
  • MongloidooMongloidoo Member Posts: 52
    I changed my statement to..

    if ("Dots".equals(dotsRadioBtn))
    drawingMode = "Dots";
    else
    drawingMode = "Lines";

    and still nothing, i am interpreting your advice incorrectly?
    Thanks for all your help so far.

    Mongloid
  • CthulhuCthulhu Member Posts: 39
    : I changed my statement to..
    :
    : if ("Dots".equals(dotsRadioBtn))
    : drawingMode = "Dots";
    : else
    : drawingMode = "Lines";
    :
    : and still nothing, i am interpreting your advice incorrectly?
    : Thanks for all your help so far.
    :
    : Mongloid
    :
    try
    [code] if( modes.equals( "Lines" );[/code]
    instead of
    [code] if( modes == "Lines" ); [/code]


  • MongloidooMongloidoo Member Posts: 52
    I tried this...


    if (drawingMode.equals("lines"))
    drawingMode = "lines";
    else
    if (drawingMode.equals("dots"))
    drawingMode = "dots";

    am i missing a line? This is driving me nuts..lol, i appreciate your help though

    Mongloid
  • CthulhuCthulhu Member Posts: 39
    : if (drawingMode.equals("lines"))
    : drawingMode = "lines";
    : else
    : if (drawingMode.equals("dots"))
    : drawingMode = "dots";
    :
    uhm .. why do you test like that ? Maybe some more of your code would help. Here you set drawingMode to be lines, if it already is lines, and dots if it already is dots.
    Shouldn't the application test if the mode is lines, and if it is, then draw lines ?

  • MongloidooMongloidoo Member Posts: 52
    Yeah that is exactly what i want to do, i want it to test whether the radio is button is set to lines or dots and then draw dots, but i am a little lost...here is some more of my code...

    The lines and dot method

    if (mode == "lines") {
    // Draw new lines
    g.setColor(Color.black);
    y = 500;
    for (int i = arrayLength; --i >= 0; y -= 2) {
    g.drawLine(0, y, dataArray[i], y);
    }
    } else {
    // Draw new dots
    g.setColor(Color.black);
    y = 500;
    for (int i = arrayLength; --i >= 0; y -= 2) {
    g.drawLine(dataArray[i] - 1, y, dataArray[i], y);
    }
    }

    }


    and here is my main java file..

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;


    public class JSort extends JApplet implements ActionListener {


    final static int DATA_ARRAY_SIZE = 200;
    int dataArray[] = new int[DATA_ARRAY_SIZE];

    private JComboBox selectSortAlgorithm;

    private String options[] =
    {
    "Select Type of Sort",
    "Bubble Sort",
    "Bi-Directional Bubble Sort",
    "Quick Sort"};


    private JLabel label;

    private JLabel
    modeLabel,
    speedLabel,
    spacerLabel1,
    spacerLabel2,
    spacerLabel3,
    spacerLabel4;


    private JButton startBtn;
    private JButton stopBtn;
    private JRadioButton slowRadioBtn;
    private JRadioButton mediumRadioBtn;
    private JRadioButton fastRadioBtn;
    private JRadioButton linesRadioBtn;
    dotsRadioBtn;
    private ButtonGroup radioButtons;
    private ButtonGroup modeRadioButtons;
    private JPanel buttonPanel, modeButtonPanel;
    private SortPanel sortPanel;
    private final static int WINDOW_WIDTH = 550;
    private final static int WINDOW_HEIGHT = 550;
    private String drawingMode = "Dots";

    public void actionPerformed(ActionEvent e) {

    Object src = e.getSource();
    if (src == slowRadioBtn)
    sortPanel.setPausePeriod(100);
    if (src == mediumRadioBtn)
    sortPanel.setPausePeriod(10);
    if (src == fastRadioBtn)
    sortPanel.setPausePeriod(0);

    if (src == startBtn) {

    sortPanel.setStopRequest(false);

    if (slowRadioBtn.isSelected())
    sortPanel.setPausePeriod(100);
    else
    if (mediumRadioBtn.isSelected())
    sortPanel.setPausePeriod(10);
    else
    if (fastRadioBtn.isSelected())
    sortPanel.setPausePeriod(0);

    //check value of display type...This is where i am having trouble. i can't find out what i am missing.
    if (drawingMode.equals("lines"))
    drawingMode = "lines";
    else
    if (drawingMode.equals("dots"))
    drawingMode = "dots";

    SortAlgorithm sortAlgorithm;
    switch (selectSortAlgorithm.getSelectedIndex()) {
    case 1 : // remove magic numbers
    sortAlgorithm = (SortAlgorithm) new BubbleSortAlgorithm();
    break;
    case 2 :
    sortAlgorithm = (SortAlgorithm) new BidirBubbleSortAlgorithm();
    break;
    case 3 :
    sortAlgorithm = (SortAlgorithm) new QSortAlgorithm();

    break;
    // case 4 :
    // ShellSort(dataArray);
    // break;
    default :
    sortAlgorithm = (SortAlgorithm) new BidirBubbleSortAlgorithm();
    } // End of switch
    sortPanel.setSortAlgorithm(sortAlgorithm);
    initSort();
    sortPanel.start();
    } else
    if (src == stopBtn) {

    close();
    System.exit(0);
    }

    }

    public void close() {
    sortPanel.stop();
    setVisible(false);
    stop();
    destroy();

    }

    public void init(){


    //super("Animated Sorting Algorithms");
    sortPanel = new SortPanel(drawingMode, dataArray);
    sortPanel.setBackground(Color.white);
    sortPanel.setBorder(BorderFactory.createLoweredBevelBorder());
    sortPanel.setBorder(BorderFactory.createLineBorder(Color.green));





    //create Display Mode Radio Buttons
    modeLabel = new JLabel();
    modeLabel.setText("Display As: ");
    linesRadioBtn = new JRadioButton("Lines", true);
    linesRadioBtn.setBorder (BorderFactory.createRaisedBevelBorder());
    linesRadioBtn.setBorderPainted (true);

    dotsRadioBtn = new JRadioButton("Dots", false);
    dotsRadioBtn.setBorder (BorderFactory.createRaisedBevelBorder());
    dotsRadioBtn.setBorderPainted (true);


    modeRadioButtons = new ButtonGroup();
    modeRadioButtons.add(linesRadioBtn);
    linesRadioBtn.setMnemonic(KeyEvent.VK_L);
    modeRadioButtons.add(dotsRadioBtn);
    dotsRadioBtn.setMnemonic(KeyEvent.VK_D);
    buttonPanel = new JPanel();
    dotsRadioBtn.addActionListener(this);
    linesRadioBtn.addActionListener(this);

    stopBtn = new JButton("Close Applet");
    stopBtn.setFont (new Font (null, Font.BOLD, 14));
    stopBtn.setForeground (Color.red);
    stopBtn.addActionListener(this);
    stopBtn.setMnemonic(KeyEvent.VK_O);
    buttonPanel.add(stopBtn);



    //create Speed Radio Buttons
    spacerLabel1 = new JLabel();
    spacerLabel1.setText(" ");
    spacerLabel2 = new JLabel();
    spacerLabel2.setText(" ");
    spacerLabel3 = new JLabel();
    spacerLabel3.setText(" ");
    spacerLabel4 = new JLabel();
    spacerLabel4.setText(" ");
    speedLabel = new JLabel();
    speedLabel.setText(" Speed: ");

    slowRadioBtn = new JRadioButton("Slow", true);
    slowRadioBtn.setMnemonic(KeyEvent.VK_S);
    slowRadioBtn.addActionListener(this);
    slowRadioBtn.setBorder (BorderFactory.createRaisedBevelBorder());
    slowRadioBtn.setBorderPainted (true);

    mediumRadioBtn = new JRadioButton("Medium", false);
    mediumRadioBtn.setMnemonic(KeyEvent.VK_M);
    mediumRadioBtn.addActionListener(this);
    mediumRadioBtn.setBorder (BorderFactory.createRaisedBevelBorder());
    mediumRadioBtn.setBorderPainted (true);

    fastRadioBtn = new JRadioButton("Fast", false);
    fastRadioBtn.setMnemonic(KeyEvent.VK_F);
    fastRadioBtn.addActionListener(this);
    fastRadioBtn.setBorder (BorderFactory.createRaisedBevelBorder());
    fastRadioBtn.setBorderPainted (true);



    radioButtons = new ButtonGroup();
    radioButtons.add(slowRadioBtn);
    radioButtons.add(mediumRadioBtn);
    radioButtons.add(fastRadioBtn);


    buttonPanel.setLayout(new GridLayout(13,1,30,0));
    buttonPanel.add(speedLabel);
    buttonPanel.add(slowRadioBtn);
    buttonPanel.add(mediumRadioBtn);
    buttonPanel.add(fastRadioBtn);
    buttonPanel.add(spacerLabel1);

    buttonPanel.add(modeLabel);
    buttonPanel.add(linesRadioBtn);
    buttonPanel.add(dotsRadioBtn);
    buttonPanel.add(spacerLabel2);


    startBtn = new JButton( "Shuffle and Start");
    startBtn.setFont (new Font (null, Font.BOLD, 14));
    startBtn.setForeground (Color.blue);
    startBtn.addActionListener(this);
    startBtn.setMnemonic(KeyEvent.VK_T);
    buttonPanel.add(startBtn);



    buttonPanel.setBorder(BorderFactory.createEmptyBorder(
    30, //top
    30, //left
    10, //bottom
    30) //right
    );


    // Setup sort algorithm selection box
    int n = 2;
    selectSortAlgorithm = new JComboBox(options);
    selectSortAlgorithm.setMaximumRowCount(n);
    Container container = getContentPane();
    container.add(sortPanel,BorderLayout.CENTER);
    container.add(selectSortAlgorithm,BorderLayout.SOUTH);
    container.add(buttonPanel,BorderLayout.EAST);

    setSize(WINDOW_WIDTH,WINDOW_HEIGHT);
    setVisible(true);
    initSort();


    } // End init

    public void initSort() {
    scramble();
    sortPanel.paintImmediately(new Rectangle(sortPanel.getSize()));
    }

    public static void main( String args[] ){
    JFrame applicationFrame = new JFrame ("Animated Sorting Algorithms Applet");

    JSort appletObject = new JSort();
    appletObject.init();
    appletObject.start();

    applicationFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    applicationFrame.getContentPane().add(appletObject);
    applicationFrame.setSize(WINDOW_WIDTH,WINDOW_HEIGHT);
    applicationFrame.pack();
    applicationFrame.setVisible(true);
    } //end main

    void scramble() {

    double f = 300 / (double) dataArray.length; // replace magic number
    for (int i = dataArray.length; --i >= 0;) {
    dataArray[i] = (int)(i * f);
    }
    for (int i = dataArray.length; --i >= 0;) {
    int j = (int)(i * Math.random());
    int t = dataArray[i];
    dataArray[i] = dataArray[j];
    dataArray[j] = t;
    }

    }

    public void start() {



    }

    public void stop() {
    sortPanel.setStopRequest(true);

    }
    }
  • beancounterbeancounter Member Posts: 88
    : Yeah that is exactly what i want to do, i want it to test whether the radio is button is set to lines or dots and then draw dots, but i am a little lost...here is some more of my code...
    :
    : The lines and dot method
    :
    : if (mode == "lines") {
    : // Draw new lines
    : g.setColor(Color.black);
    : y = 500;
    : for (int i = arrayLength; --i >= 0; y -= 2) {
    : g.drawLine(0, y, dataArray[i], y);
    : }
    : } else {
    : // Draw new dots
    : g.setColor(Color.black);
    : y = 500;
    : for (int i = arrayLength; --i >= 0; y -= 2) {
    : g.drawLine(dataArray[i] - 1, y, dataArray[i], y);
    : }
    : }
    :
    : }
    :
    :
    :
    Hi,

    I not sure if this is what your asking, But....
    If you want to determine if a jRadioButton is selected you might try this

    if (jRadioButton1.isSelected()){
    "Insert your code here"

    }

    It appears, since you only seem to get the dots, that the line condition never fires.

    Hope this helps
  • MongloidooMongloidoo Member Posts: 52
    I wrote my statement like this...

    if (linesRadioBtn.isSelected()){
    sortPanel.draw("lines");}
    else
    if (dotsRadioBtn.isSelected());
    sortPanel.draw("dots");

    but i still can't get the line condition to fire. Am i missing something here? I added actionListeners, would things be any different if i added eventListeners or is that the same thing? If they aren't how do i add eventListeners. please help me!! :(


    Mongloid
  • Justin BibJustin Bib USAMember Posts: 0

    _____ [ http://forcoder.org ] free video tutorials and ebooks about || Visual Basic C C++ Perl Objective-C Visual Basic .NET Python MATLAB Delphi Assembly R Swift Java Scratch PHP Ruby Go C# PL/SQL JavaScript SAS LabVIEW Kotlin Dart Transact-SQL D FoxPro Fortran Awk F# Bash Crystal Erlang ML Prolog Lua Lisp Rust VBScript ABAP Hack COBOL Ada Alice Julia Apex Logo Scheme Scala Clojure || ___________

Sign In or Register to comment.