如何使图像可以点击其他问题 [英] how to make the images clickable with additional problems

查看:131
本文介绍了如何使图像可以点击其他问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是Sudoku.java
这是主类。 提醒保留时间, SudokuBoardDisplay 显示电路板并绘制图像。处理逻辑的人是 SudokuModel

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

如何使图像可点击并执行某些操作。
如何将剩余时间显示为JLabel ....?

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

我使用的是textfields,而不是textfields作为行col和val的容器,我想使用图像,,,当点击图像时,框架会弹出9个按钮,其值为1到9.当用户选择时,点击的图像将变为按钮所具有的值。

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);
        }
    }
}

}

这是我的'提醒'
如何在我的其他类中显示延长JFrame的剩余时间?

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);
    }
}
}


推荐答案

可点击图片

您自己并没有轻松完成此任务,但是......

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

您可能需要在电路板上添加 MouseListener 。当触发 mouseClicked 事件时,您需要确定事件发生在哪个单元格中。

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.

最简单的方法将有一个 List Rectangle 数组,每个代表一个单独的单元格。通过这种方式,您可以查看列表并调用 Rectangle#contains(Point)来确定鼠标是否落在该单元格的范围内。

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)

剩余计时器

你已经在使用 java.util.Timer 这很酷,但我会用 javax.swing.Timer 而不是。主要原因是它在Event Dispatching Thread中被触发。

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();

每次计时器滴答时,都会减少UI组件的时间......

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

我不会这样做...

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.

我会创建一个 Cell 类。


  • 每个单元格都将分配给特定的网格。

  • 每个人都会引用游戏模型

  • 每个单元格都是 paintable

  • 每个单元格都知道要绘制的位置。

  • 如果我正在编写它,每个单元格将从 JPanel扩展并使用 GridLayout GridBagLayout 进行布局(这样可以解决鼠标问题) click isssue)

  • 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)

这篇关于如何使图像可以点击其他问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆