簡體   English   中英

如何創建帶有圖像的框架以充當 Java 中的工具提示?

[英]How do I create a frame with images to act as a tool tip in Java?

我已經為一個問題苦苦掙扎了 2 天,而且我一直在碰壁。 當我將鼠標懸停在第一個窗口上的任務上時,我希望彈出另一個框架並在名冊上顯示可用人員的圖像,然后我可以單擊以分配給我鼠標懸停的任務。 我正在使用 Java.awt。 我覺得這不應該那么難,但我無法讓它發揮作用。 任何建議或幫助將不勝感激。

主要課程

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;

public class Game extends Canvas implements Runnable {

    private static final long serialVersionUID = 1L;

    private boolean isRunning = false;
    private Thread thread;
    private Handler handler;

    public Game() {
        new Window(1000, 800, "blah blah", this);
        start();
        
        handler = new Handler();
        this.addMouseListener(new MouseInput(handler));
        
        handler.addObject(new InterfaceElement(10, 10, ID.InterfaceElement, "C:\\Users\\cpaqu\\eclipse-workspace\\Expediter Game\\res\\Face.png"));
        handler.addObject(new InterfaceElement(10, 500, ID.InterfaceElement, "C:\\Users\\cpaqu\\eclipse-workspace\\Expediter Game\\res\\MessageBox.png"));

        Maintainer carl = new Maintainer("Carl", 0.75, "CommonMx.png");
        Maintainer bill = new Maintainer("Bill", 1.0, "GoodMx.png");
        Maintainer Max = new Maintainer("Max", 1.1, "RareMx.png");
        Maintainer Sarah = new Maintainer("Sarah", 1.15, "LegendaryMx.png");
        Maintainer Darius = new Maintainer("Darius", 1.25, "ExoticMx.png");
        
        Task task = new Task(280, "OWS Warning Light");
        handler.addObject(task);
        task.setSlotOneMx(Max);
    }
    
    private void start() {
        isRunning = true;
        thread = new Thread(this);
        thread.start();
    }
    
    private void stop() {
        isRunning = false;
        try {
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    
    @Override
    public void run() {
        this.requestFocus();
        long lastTime = System.nanoTime();
        double amountOfTicks = 60.0;
        double ns = 1000000000 / amountOfTicks;
        double delta = 0;
        long timer = System.currentTimeMillis();
        int frames = 0;
        
        while(isRunning) {
            long now = System.nanoTime();
            delta += (now - lastTime) / ns;
            lastTime = now;
            while(delta >= 1) {
                tick();
                delta--;
            }
            render();
            frames++;
            
            if (System.currentTimeMillis() - timer > 1000) {
                timer += 1000;
                frames = 0;
            }
        }
        stop();
    }

    public void tick() {
        handler.tick();
    }
    
    public void render() {
        BufferStrategy bs = this.getBufferStrategy();
        if(bs == null) {
            this.createBufferStrategy(3);
            return;
        }
        
        Graphics g = bs.getDrawGraphics();
        /////////////////////////////////////
        
        g.setColor(Color.LIGHT_GRAY);
        g.fillRect(0, 0, 1000, 800);
        
        handler.render(g);
        
        g.setColor(Color.BLACK);
        g.setFont(new Font("TimesRoman", Font.BOLD, 24));
        g.drawString("Launches in 15 minutes,", 60, 600);
        g.drawString("get those B-men out to", 60, 630);
        g.drawString("their spots!", 60, 660);
        ////////////////////////////////////
        g.dispose();
        bs.show();
    }
    
    public static void main(String[] args) {
        new Game();
    }
}

===============任務類=====================

    import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.util.ArrayList;

public class Task extends GameObject{
    
    private final int MAX_TASKS = 6;
    private final double ticksPerSecond = 60.0;
    private final double progressBarLength = 250.0;
    
    private Maintainer slotOneMx = null;
    private Maintainer slotTwoMx = null;
    
    private static int numTasks = 0;
    
    private double task_len_in_sec;
    private double progress_per_second = 20;
    private double completed_progress = 0.0;
    
    private String name;
    private int mxSize;
    private int mxRows;
        
    private String interfacePath = "C:\\Users\\cpaqu\\eclipse-workspace\\Expediter Game\\res\\TaskBG.png";
    private String progressPath = "C:\\Users\\cpaqu\\eclipse-workspace\\Expediter Game\\res\\ProgressBar.png";
    private String NoMxPath = "C:\\Users\\cpaqu\\eclipse-workspace\\Expediter Game\\res\\NoMx.png";
    
    private BufferedImage interfaceImage = null;
    private BufferedImage progressImage = null;
    private BufferedImage NoMxImage = null;
    
//  private Frame frame;
    private ArrayList<Maintainer> workers;
    
    private BufferedImage[] icons;
    
    BufferedImageLoader loader;
    
    public Task(double taskLen, String name) {
        super(465, 15, ID.Task);
        
//      frame = new Frame();
//      frame.setVisible(true);
//      frame.setBounds(200, 200, (74 * 4) + (15 * 5), (mxRows * 80) + 30);
        
        loader = new BufferedImageLoader();
        
        mxSize = Maintainer.getMxQty();
        mxRows = mxSize / 4;
        
        icons = new BufferedImage[10];
        
        
        
        y = y + (Task.numTasks * 115);
        Task.numTasks++;
        
        this.task_len_in_sec = taskLen;
        this.name = name;
        
        interfaceImage = loader.loadImage(interfacePath);
        progressImage = loader.loadImage(progressPath);
        NoMxImage = loader.loadImage(NoMxPath);
    }

    public void tick() {
        if (completed_progress / task_len_in_sec <= 1.0) {  completed_progress += (progress_per_second / ticksPerSecond); }
        
//      workers = Maintainer.getMaintainers();
//      mxSize = Maintainer.getMxQty();
//      mxRows = (mxSize % 4 == 0) ? mxSize / 4 : ((mxSize / 4) + 1);
        

//      frame.setTitle(Integer.toString(mxSize) + " - " + Integer.toString(mxRows));
        
    }

    public void render(Graphics g) {
        g.drawImage(interfaceImage, x, y, null);
        
        g.setColor(Color.BLACK);
        g.setFont(new Font("TimesRoman", Font.BOLD, 24));
        g.drawString(name, x+20, y+30);
        
        if (slotOneMx == null) {
            g.drawImage(NoMxImage, x+302, y+14, null);
        } else {
            g.drawImage(loader.loadImage(slotOneMx.getImagePath()), x+302, y+14, null);
        }
        
        if (slotTwoMx == null) {
            g.drawImage(NoMxImage, x+397, y+15, null);
        } else {
            g.drawImage(loader.loadImage(slotTwoMx.getImagePath()), x+397, y+15, null);
        }
        
        g.drawImage(progressImage, x+29, y+53, (int) (progressBarLength * (completed_progress / task_len_in_sec)), 20, null);
        
    }

    public void setSlotOneMx(Maintainer mx) {
        slotOneMx = mx;
    }
    
    public void setSlotTwoMx(Maintainer mx) {
        slotTwoMx = mx;
    }
    
    public Rectangle getBounds() {
        return null;
    }
    
    public int getNumTasks() {
        int x = numTasks;
        return x;
    }

}

強烈建議您遷移到 Swing 或 JavaFX。 我不了解原始 AWT,但 Swing 可以解釋 HTML,因此獲取圖像工具提示的一種簡單方法是嵌入 HTML:

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    JLabel label = new JLabel("This is a task");
    label.setToolTipText("<html><img src=\"file:/path/to/image\"></html>");
    label.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
    
    frame.add(label);
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM