how to make the images clickable with additional problems [closed]

你说的曾经没有我的故事 提交于 2019-12-13 22:31:35

问题


this is Sudoku.java this is the main class. Reminder does the time keeping, the SudokuBoardDisplay displays the board and draws images. The one who handles the logic is the SudokuModel

How do I make the images clickable and do something. how to display the remaining time as JLabel ....?

i used textfields but instead of the textfields being the container of the row col and val, i want to use the images ,,, when the images is clicked, a frame pops up with 9 buttons, that has a value of 1 to 9. and when the user chooses, the clicked image will turn to the value the button has.

i displayed the images in this manner how can i make it clickable and do something?

private void drawCellValues(Graphics g){
Image bufferedImage;
for(int i=0;i<9;i++) {
    int y=(i+1)*50-15;
    for(int j=0;j<9;j++){
        if(_model.getValue(i,j)!=10){
            int x=j*50+15;
            String path="/images/"+_model.getValue(i,j)+".png";
            //these lines of code should identify if the cell is editable or not, but unfortunately. _solve.getBoard().charAt(i+(j*10))=='0' produces an error
            //String patht="/images/"+_model.getVal(i,j)+"t.png";
            //if(_solve.getBoard().charAt(i+(j*10))=='0'){
            //    bufferedImage=new ImageIcon(this.getClass().getResource(patht)).getImage();
            //}
            //else{  
                bufferedImage=new ImageIcon(this.getClass().getResource(path)).getImage();  
            //}
            g.drawImage(bufferedImage,x-14,y-34,this);
        }
    }
}

}

this is my 'Reminder' how can i display the time left in my other class extending JFrame?

import java.util.*;
import javax.swing.JOptionPane;
public class Reminder{
Timer timer;
public Reminder(int sec){
timer=new Timer();
timer.schedule(new stop(),sec*1000);
}
class stop extends TimerTask{
    @Override
    public void run(){
        JOptionPane.showMessageDialog(null, "TIME IS UP!!! YOU FAIL!!");
        timer.cancel();
        System.exit(0);
    }
}
}

回答1:


Clickable Images

You've not made this task easy for yourself, however...

You will probably need to add a MouseListener to the board. When the mouseClicked event is triggered, you will need to determine in which cell the event occurred.

The easiest way would be to have a List or array of Rectangle, each representing an individual cell. This way you could look the list and call Rectangle#contains(Point) to determine if the mouse fell within the range of that cell.

Otherwise you're stuck with something like...

int row = -1;
int col = -1;

for (int i = 0; i < 9; i++) {
    int yPos = (i + 1) * 50 - 15;
    if (y >= yPos && y <= yPos + 50) {
        row = i;
        break;
    }
}
for (int j = 0; j < 9; j++) {
    int xPos = j * 50 + 15;
    if (x >= xPos && x <= xPos + 50) {
        col = j;
        break;
    }
}

(You're going to have to debug this to be sure, but I think it's right from what I can tell of your code)

Timer Remaining

You're already using a java.util.Timer which is cool, but I would use javax.swing.Timer instead. The main reason is that it is fired within the Event Dispatching Thread.

Timer countDown = new Timer(1000, new Watcher(gameTime));
countDown.setRepeats(true);
countDown.setCoalesce(true);
countDown.start();

Each time the timer ticks, you would reduce the UI components time...

public class Watcher implements ActionListener {
    private int length;
    private int ticks = 0;
    public Watcher(int time) {
        length = time;
    }

    public void actionPerformed(ActionEvent evt) {
        tick++;
        int timeRemaining = length - (tick * 1000);
        if (timeRemaining <= 0) {
            // Game over
        } else {
            labelShowingTimeRemaining.setText(Integer.toString((int)Math.round(timeRemaining / 1000)));
        }
    }
}

FeedBack

I wouldn't do this...

bufferedImage=new ImageIcon(this.getClass().getResource(path)).getImage();  

During every paint cycle. This can be time consuming and not to mention will consume a reasonable amount of memory.

I would create a Cell class.

  • Each cell would be assigned to a specific grid.
  • Each would have a reference to the game model
  • Each cell would be paintable
  • Each cell would know where it is to painted.
  • And if I was writing it, each cell would extend from JPanel and would be layout out using either a GridLayout or GridBagLayout (this would solve you mouse click isssue)


来源:https://stackoverflow.com/questions/12795309/how-to-make-the-images-clickable-with-additional-problems

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!