Jump to content


  • Posts

  • Joined

  • Last visited

anson920520's Achievements


Newbie (1/7)



  1. Thanks for help,but let's said this, i have my table which wrote in javascript document.write('<table>');//Creates Tabledocument.write('<table>');//for loop that creates the row, increments from 0 to 9 each time it reiteratesfor (var row=0; row<9; row++) { document.write('<tr>'); //a nested for loop that reiterates 9 times creating the <td>'s and then exits to the parent for loop. for (var col=0; col<9; col++) { document.write('<td id="r', row, 'c', col, '"oncontextmenu="handleRightClick(this); return false;">'); document.write('</td>'); } document.write('</tr>');}document.write('</table>'); and now i want to put an image when user right click on itso i try this code to test it: function handleRightClick(){ document.getElementById('r0c0').innerHTML = '<img src="minesweeper_tiles.png" width="28px" height="28px" alt="Automine" />';} it works , but i want to let the user click on any of themand the cell he select will have an image appeari had plan to loop throught the whole tablebut i realize if i do so, when the user click onceeverything cell will appear an image, not only the selected cell. Many thanks
  2. I want to change something in a table when i click on it,so i create a onclick function,but i notice if i have a fucntion like this: <html><head><script type="text/javascript">function handleRightClick(){document.getElementById("test").innerHTML="123123";}</script</head><body><table id="test" oncontextmenu="handleRightClick(this); return false;"><tr><td>A</td></tr><tr><td>B</td></tr><tr><td>C</td></tr></body></html> when i click on it, it will change everything,how can i change all my elements sperate ?like when i click on A, only a will change. I want to know how to make it in one function,not create a new functionbecause if i have 50 elements then i may need to have 50 same functions to do it lol Many thanks
  3. I am currently working on a minesweeper game function about calculate adjacent mines,i have done the fully code now, but i might some error inside itso when i open up the page, there's no number came out.My target is when i open up a browser and there will be ten mines withnumber around it can someone tell me what i should correct? this is the code to lay 10 random mines,it working good: Code: function addmines () { var mine = 9; var mineNumber = 0; var n = 10; //number of mines while (n>0) { var row = Math.floor(Math.random()*9); var col = Math.floor(Math.random()*9); var id = "r" + row + "c" + col; var td = document.getElementById(id); if (td.children.length > 0) continue; //there's already a mine here n--; var a = document.createElement("img"); a.src = "mine32.gif"; a.height = 30; a.width = 30; a.id = 'mine' + mineNumber; td.appendChild(a); mineNumber++; }} The code below is the main problemi had made a little function to test if the loop works or not,and i can see it is working but there is still no number appear. Code: function neighbours () { var dr = [-1, -1, 0, 1, 1, 1, 0, -1]; var dc = [ 0, 1, 1, 1, 0, -1, -1, -1]; var newRow = 9; var newCol = 9; for (var row=0; row<newRow; row++) { //a nested for loop that reiterates 9 times creating the <td>'s and then exits to the parent for loop. for (var col=0; col<newCol; col++) { //document.getElementById('r1c1').innerHTML = '<span class="label4">4</span>'; var id = 'r' + row + 'c' + col; //document.getElementById(id).innerHTML = '<span class="label4">4</span>'; if (addmines.toString().indexOf(id) == -1) { var counter = 0; //looping over for (var i=0; i<dr.length; i++) { /* //CODE GIVES MEGA ISSUES if(row+dr[i]>0 && col+dc[i]>0 && row+dr[i]<=newRow && col+dc[i]<=newCol) { //document.getElementById(id).innerHTML = '<span class="label4">4</span>'; DOES NOT WORK IN HERE var id = 'r' + (row+dr[i]) + 'c' + (col+dc[i]); if (mines.toString().indexOf(id) != -1) { counter++; } //END IF } //END IF //END OF PROBLEM CODE */ document.getElementById(id).innerHTML = '<span class="label4">4</span>'; //WORKS IF PROBLEM AREA IS DISABLED var id = 'r' + r + 'c' + c; var obj = document.getElementById(id); obj.firstChild.nodeValue = count; //value obj.className += ' m' + count; //style } //END FOR } //END IF } //END FOR }} function insertText () {
  4. I currently need to finish a task for my project which needs me to create a Create a display component to show Select pet shop name and total Pets in that shop. For this tasks i created a several components,I had a function call "getPetsInPetShop" to do what i mention public static ArrayList<Pet> getPetsInPetShop(Petshop petshopName) { // this should go through all pets and return only those whose // pet shop name is the same as the parameter... //new arrylist to store information about pets ArrayList<Pet> result = new ArrayList<Pet>(); //loop thought the csv file for (Pet p : pet) { //if the user input a petshop name that can be found in the csv file if (p.getShop().equals(petshopName)) { //return thouse pet p result.add(p); } } //Here is the output return result; } I assume this can allow me to loop thought how many pets i got inside the selected pet shop and return it. On the other hand this is my code to pass the function when the program run. This is mousevent function, when the user click on it , a new panel will appear for them to input information. public class SelectPetShopControl implements MouseListener{ @Override public void mouseClicked(MouseEvent e) { JLabel clicked = (JLabel)e.getSource(); String PetShopName, TotalPets; SelectPetShopPanel spsp = (SelectPetShopPanel)clicked.getParent(); PetShopName = JOptionPane.showInputDialog("Enter a pet shop name"); if(PetShopName != null){ TotalPets = ApplicationModel.getPetsInPetShop(TotalPets); spsp.setPetshop("Pet Shop Nmae: "+PetShopName); spsp.setTotalPets("Total: "+TotalPets); } } Everything in here is right expect "TotalPets = ApplicationModel.getPetsInPetShop(TotalPets);"The error message in here is incompatible types. I had look at some example and try to fix it by myself,but i realize i don't have enough technology to finish it. Many thanks
  • Create New...