JLabel not loading on first load
I have an applet at: http COLON //pathhero DOT codemann8 DOT com/create DOT php
After you get the applet to load, you'll see an image. There's suppose to be a blank label on top of the image, which has a mouselistener. You're suppose to be able to click the image (actually the invisible label on top of it) and a green marker shows up and then you click in a second spot on the same line. This won't work until the user refreshes the page after the applet loads for the first time.
I know the problem is that that blank label isn't there, which is why clicking on it does nothing...I know it is doing this because I also added another label with that image as the icon, and got rid of the image display in the paint method...and it doesn't show, but after a refresh it does. But, I don't understand why.
Code:
import java.applet.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.net.*;
public class create1 extends Applet implements ActionListener
{
private JButton btnMask, btnUndo, btnRedo, btnSave;
private JLabel transChart, lblMask, lblChart;
private JSeparator sepTop;
private Image chart, activation, squeeze, nowhammy, index;
private ArrayList<int[]> masks = new ArrayList<int[]>();
private ArrayList<int[]> redo = new ArrayList<int[]>();
private int startX, startY, endX;
private int mouseDown = 0, maskColor = 1; //0=red 1=blue 2=yellow
public void init() {
chart = getImage(getCodeBase(), getParameter("imageName"));
ImageObserver io = null;
int height = chart.getHeight(io);
height -= 16;
this.setLayout(null);
this.resize(1014, height);
this.setBackground(Color.WHITE);
activation = getImage(getCodeBase(), "bluepicturefile");
squeeze = getImage(getCodeBase(), "redpicturefile");
nowhammy = getImage(getCodeBase(), "yellowpicturefile");
index = getImage(getCodeBase(), "indexpicturefile");
lblMask = new JLabel("Mask Color:", SwingConstants.RIGHT);
lblMask.setBounds(10,10,100,30);
this.add(lblMask);
btnMask = new JButton("Blue");
btnMask.setBounds(120,10,100,30);
btnMask.addActionListener(this);
this.add(btnMask);
btnUndo = new JButton("Undo");
btnUndo.setBounds(250,10,100,30);
btnUndo.setEnabled(false);
btnUndo.addActionListener(this);
this.add(btnUndo);
btnRedo = new JButton("Redo");
btnRedo.setBounds(360,10,100,30);
btnRedo.setEnabled(false);
btnRedo.addActionListener(this);
this.add(btnRedo);
btnSave = new JButton("Save");
btnSave.setBounds(490,10,100,30);
btnSave.addActionListener(this);
this.add(btnSave);
sepTop = new JSeparator();
sepTop.setBounds(10, 50, this.getWidth() - 20, 1);
this.add(sepTop);
//lblChart = new JLabel(new ImageIcon(chart));
//lblChart.setBounds(10, 60, 1004, height);
//this.add(lblChart);
transChart = new JLabel("Ready...");
transChart.setBounds(10, 60, 1004, height);
transChart.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent evt) {
imageClickEvent(evt);
}
});
this.add(transChart);
repaint();
}
public String getPath() {
int[] temp = masks.get(0);
String path = temp[0] + "-" + temp[1] + "-" + temp[2] + "-" + temp[3];
for (int i = 1; i < masks.size(); i++) {
temp = masks.get(i);
path = path + "/" + temp[0] + "-" + temp[1] + "-" + temp[2] + "-" + temp[3];
}
return path;
}
public void paint(Graphics g) {
super.paint(g);
g.drawImage(chart, transChart.getX(), transChart.getY(), this);
for (int i = 0; i < masks.size(); i++) {
int[] temp = masks.get(i);
if (temp[0] == 1) {
g.drawImage(activation, temp[1], temp[2], temp[3], 49, this);
}
else if (temp[0] == 0) {
g.drawImage(squeeze, temp[1], temp[2], temp[3], 49, this);
}
else {
g.drawImage(nowhammy, temp[1], temp[2], temp[3], 49, this);
}
}
if (mouseDown == 1) {
g.drawImage(index, startX, startY - 3, 1, 55, this);
}
}
public void actionPerformed(ActionEvent e) {
if (((JButton)e.getSource()).getText() == "Blue") {
maskColor = 0;
btnMask.setText("Red");
}
else if (((JButton)e.getSource()).getText() == "Red") {
maskColor = 2;
btnMask.setText("Yellow");
}
else if (((JButton)e.getSource()).getText() == "Yellow") {
maskColor = 1;
btnMask.setText("Blue");
}
else if (((JButton)e.getSource()).getText() == "Undo") {
mouseDown = 0;
redo.add(masks.get(masks.size() - 1));
masks.remove(masks.size() - 1);
if (masks.isEmpty()) {
btnUndo.setEnabled(false);
}
btnRedo.setEnabled(true);
repaint();
}
else if (((JButton)e.getSource()).getText() == "Redo") {
mouseDown = 0;
masks.add(redo.get(redo.size() - 1));
redo.remove(redo.size() - 1);
if (redo.isEmpty()) {
btnRedo.setEnabled(false);
}
btnUndo.setEnabled(true);
repaint();
}
else if (((JButton)e.getSource()).getText() == "Save") {
try {
AppletContext context = getAppletContext();
URL link = new URL("http COLON //pathhero DOT codemann8 DOT com/submit DOT php?chart=" + getParameter("chartId") + "&path=" + getPath());
context.showDocument(link, "_blank");
}
catch (MalformedURLException ex) {}
}
else {
}
}
public void imageClickEvent(MouseEvent e) {
mouseDown++;
mouseDown %= 2;
if (mouseDown == 1) {
//set start of activation
if (e.getY() < 114 || e.getY() % 114 > 49) {
//cancel click
mouseDown--;
}
else {
startX = e.getX() + transChart.getX();
startY = ((e.getY() / 114) * 114) + transChart.getY();
}
}
else {
endX = e.getX() + transChart.getX();
if (startX > endX) {
int t = endX;
endX = startX;
startX = t;
}
if (endX != startX) {
masks.add(new int[] {maskColor, startX, startY, endX - startX + 1});
btnUndo.setEnabled(true);
btnRedo.setEnabled(false);
redo.clear();
}
}
repaint();
}
}