'How do I create a frame with images to act as a tool tip in Java?
I've been struggling with an issue for 2 days now and I keep running into walls. When I mouse over a task on my first window I want another frame to pop up and display the images of available people on a roster that I could then click on to assign to the task I had moused over. I'm using Java.awt. I feel like this shouldn't be that hard but I can't get it to work. Any advice or help would be greatly appreciated.
MAIN CLASS
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();
}
}
===============TASK CLASS====================
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;
}
}
Solution 1:[1]
I would HIGHLY recommend you moving to Swing or JavaFX. I don't know about raw AWT, but Swing can interpret HTML, so an easy way to get image tooltips is to embed 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);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Ryan |