So I am doing a Hang man game in Java for my Final Project in my Computer Science class. I have done the template for the Hang man (i.e. buttons and added the words needed). But I don't know how to draw the Hangman itself and how to randomly add the words (meaning so they will start guessing the words) to the program. What I am asking is how do I draw the Hangman if a letter was guessed wrong(or right, to write in the textfield). For the letters I have done JButtons because I think it would be easier, and added action listeners. I know the code is very long, but don't pay attention to the template, method Start is where I have my words and hints. Thank you in advance. Don't mind that I do not have a main method, I have a driver class that extends this class. (It was one of the requirements)
Solved!
package hangman;
import javax.swing.*;
import java.util.Random;
import java.util.Scanner;
import java.awt.event.*;
import java.awt.*;
public class Hangman extends JFrame
{
JPanel panel = new JPanel();
JButton restart = new JButton("Restart");
JButton a = new JButton("A"), b = new JButton("B"), c = new JButton("C"), d = new JButton("D"), ee = new JButton("E"), f = new JButton("F"),
g = new JButton("G"), h = new JButton("H"), i = new JButton("I"), j = new JButton("J"), k = new JButton("K"), l = new JButton("L"),
m = new JButton("M"), n = new JButton("N"), o = new JButton("O"), p = new JButton("P"), q = new JButton("Q"), r = new JButton("R"),
s = new JButton("S"), t = new JButton("T"), u = new JButton("U"), v = new JButton("V"), w = new JButton("W"), x = new JButton("X"),
y = new JButton("Y"), z = new JButton("Z");
JButton exit = new JButton("Exit");
JButton hint = new JButton("Hint");
JLabel title = new JLabel("Welcome to Hang Man! ");
JLabel hintWord = new JLabel("Click the Button to show a hint!");
Font font = new Font("Comic Sans MS", Font.ITALIC, 24);
JTextField theWord = new JTextField();
Font font1 = new Font("Comic Sans MS", Font.BOLD, 34);
public Hangman()
{
//Panel adding
panel.add(restart);
panel.add(theWord);
panel.add(hint);
panel.add(exit);
panel.add(title);
panel.add(hintWord);
panel.add(a);
panel.add(b);
panel.add(c);
panel.add(d);
panel.add(ee);
panel.add(f);
panel.add(g);
panel.add(h);
panel.add(j);
panel.add(k);
panel.add(l);
panel.add(m);
panel.add(n);
panel.add(o);
panel.add(p);
panel.add(q);
panel.add(r);
panel.add(s);
panel.add(t);
panel.add(u);
panel.add(v);
panel.add(w);
panel.add(x);
panel.add(y);
panel.add(z);
add(panel);
title.setFont(font);
// JFrame properties
setSize(1024, 700);
setTitle("Hangman");
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
this.setLayout(null);
panel.setLayout(null);
//Adding to the panel
panel.add(restart);
panel.add(theWord);
panel.add(hint);
panel.add(exit);
panel.add(title);
panel.add(hintWord);
panel.add(a);
panel.add(b);
panel.add(c);
panel.add(d);
panel.add(ee);
panel.add(f);
panel.add(g);
panel.add(h);
panel.add(j);
panel.add(k);
panel.add(l);
panel.add(m);
panel.add(n);
panel.add(o);
panel.add(p);
panel.add(q);
panel.add(r);
panel.add(s);
panel.add(t);
panel.add(u);
panel.add(v);
panel.add(w);
panel.add(x);
panel.add(y);
panel.add(z);
add(panel);
// Positioning
restart.setLocation(25,25);
theWord.setLocation(100, 530);
theWord.setEnabled(false);
theWord.setSize(800, 100);
theWord.setHorizontalAlignment(theWord.CENTER);
theWord.setFont(font1);
theWord.setForeground(Color.RED);
hint.setLocation(600, 150);
exit.setLocation(900, 25);
title.setLocation(350, 25);
a.setLocation(600, 250);
b.setLocation(650, 250);
c.setLocation(700, 250);
d.setLocation(750, 250);
ee.setLocation(800, 250);
f.setLocation(850, 250);
g.setLocation(900, 250);
h.setLocation(950, 250);
j.setLocation(600, 280);
k.setLocation(650, 280);
l.setLocation(700, 280);
m.setLocation(750, 280);
n.setLocation(800, 280);
o.setLocation(850, 280);
p.setLocation(900, 280);
q.setLocation(950, 280);
r.setLocation(600, 310);
s.setLocation(650, 310);
t.setLocation(700, 310);
u.setLocation(750, 310);
v.setLocation(800, 310);
w.setLocation(850, 310);
x.setLocation(900, 310);
y.setLocation(950, 310);
z.setLocation(750, 340);
hintWord.setLocation(670, 150);
// Action Listeners
restart.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
restartActionPerformed(e);
}
});
hint.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
hintActionPerformed(e);
}
});
exit.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
exitActionPerformed(e);
}
});
a.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
aActionPerformed(e);
}
});
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
bActionPerformed(e);
}
});
c.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
cActionPerformed(e);
}
});
d.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
dActionPerformed(e);
}
});
ee.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
eeActionPerformed(e);
}
});
f.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
fActionPerformed(e);
}
});
g.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
gActionPerformed(e);
}
});
h.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
hActionPerformed(e);
}
});
j.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
jActionPerformed(e);
}
});
k.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
kActionPerformed(e);
}
});
l.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
lActionPerformed(e);
}
});
m.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
mActionPerformed(e);
}
});
n.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
nActionPerformed(e);
}
});
o.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
oActionPerformed(e);
}
});
p.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
pActionPerformed(e);
}
});
q.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
qActionPerformed(e);
}
});
r.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
rActionPerformed(e);
}
});
s.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
sActionPerformed(e);
}
});
t.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
tActionPerformed(e);
}
});
u.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
uActionPerformed(e);
}
});
v.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
vActionPerformed(e);
}
});
w.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
wActionPerformed(e);
}
});
x.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
xActionPerformed(e);
}
});
y.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
yActionPerformed(e);
}
});
z.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
zActionPerformed(e);
}
});
}
public void Start()
{
String words[] = new String[26];
String hints[] = new String[26];
// Words along with hints
words[0] = "president";
hints[0] = "Leader.";
words[1] = "exclamation";
hints[1] = "Shout out.";
words[2] = "statement";
hints[2] = "To say.";
words[3] = "television";
hints[3] = "You watch it.";
words[4] = "physics";
hints[4] = "Form of Science.";
words[5] = "algebra";
hints[5] = "Form of math.";
words[6] = "geometry";
hints[5] = "Form of math.";
words[7] = "difficult";
hints[7] = "Hard.";
words[8] = "extreme";
hints[8] = "Intense.";
words[9] = "procedure";
hints[9] = "Steps.";
words[10] = "ship";
hints[10] = "Big Boat.";
words[11] = "soldier";
hints[11] = "Army.";
words[12] = "lunch";
hints[12] = "Meal.";
words[13] = "hockey";
hints[13] = "Sports.";
words[14] = "tennis";
hints[14] = "Sports.";
words[15] = "soccer";
hints[15] = "Sports.";
words[16] = "football";
hints[16] = "Sports.";
words[17] = "basketball";
hints[17] = "Sports.";
words[18] = "bias";
hints[18] = "One sided.";
words[19] = "magazine";
hints[19] = "Form of book.";
words[20] = "computer";
hints[20] = "Microsoft.";
words[21] = "internet";
hints[21] = "World Wide Web.";
words[22] = "allegedly";
hints[22] = "Supposedly.";
words[23] = "system";
hints[23] = "Network.";
words[24] = "unison";
hints[24] = "As one.";
words[25] = "excited";
hints[25] = "Upbeat.";
}
// Action Listeners
public void restartActionPerformed(ActionEvent e)
{
a.setEnabled(true);
b.setEnabled(true);
c.setEnabled(true);
d.setEnabled(true);
ee.setEnabled(true);
f.setEnabled(true);
g.setEnabled(true);
h.setEnabled(true);
j.setEnabled(true);
k.setEnabled(true);
l.setEnabled(true);
m.setEnabled(true);
n.setEnabled(true);
o.setEnabled(true);
p.setEnabled(true);
q.setEnabled(true);
r.setEnabled(true);
s.setEnabled(true);
t.setEnabled(true);
u.setEnabled(true);
v.setEnabled(true);
w.setEnabled(true);
x.setEnabled(true);
y.setEnabled(true);
z.setEnabled(true);
hintWord.setText("Click the Button to show a hint!");
}
public void exitActionPerformed(ActionEvent e)
{
System.exit(0);
}
public void hintActionPerformed(ActionEvent e)
{
hintWord.setText("");
}
public void aActionPerformed(ActionEvent e)
{
a.setEnabled(false);
}
public void bActionPerformed(ActionEvent e)
{
b.setEnabled(false);
}
public void cActionPerformed(ActionEvent e)
{
c.setEnabled(false);
}
public void dActionPerformed(ActionEvent e)
{
d.setEnabled(false);
}
public void eeActionPerformed(ActionEvent e)
{
ee.setEnabled(false);
}
public void fActionPerformed(ActionEvent e)
{
f.setEnabled(false);
}
public void gActionPerformed(ActionEvent e)
{
g.setEnabled(false);
}
public void hActionPerformed(ActionEvent e)
{
h.setEnabled(false);
}
public void jActionPerformed(ActionEvent e)
{
j.setEnabled(false);
}
public void kActionPerformed(ActionEvent e)
{
k.setEnabled(false);
}
public void lActionPerformed(ActionEvent e)
{
l.setEnabled(false);
}
public void mActionPerformed(ActionEvent e)
{
m.setEnabled(false);
}
public void nActionPerformed(ActionEvent e)
{
n.setEnabled(false);
}
public void oActionPerformed(ActionEvent e)
{
o.setEnabled(false);
}
public void pActionPerformed(ActionEvent e)
{
p.setEnabled(false);
}
public void qActionPerformed(ActionEvent e)
{
q.setEnabled(false);
}
public void rActionPerformed(ActionEvent e)
{
r.setEnabled(false);
}
public void sActionPerformed(ActionEvent e)
{
s.setEnabled(false);
}
public void tActionPerformed(ActionEvent e)
{
t.setEnabled(false);
}
public void uActionPerformed(ActionEvent e)
{
u.setEnabled(false);
}
public void vActionPerformed(ActionEvent e)
{
v.setEnabled(false);
}
public void wActionPerformed(ActionEvent e)
{
w.setEnabled(false);
}
public void xActionPerformed(ActionEvent e)
{
x.setEnabled(false);
}
public void yActionPerformed(ActionEvent e)
{
y.setEnabled(false);
}
public void zActionPerformed(ActionEvent e)
{
z.setEnabled(false);
}
}
To draw the lines you could use drawline in the Graphics class.
g.drawLine(int x1, int y1, int x2, int y2);
Docs:
Draws a line, using the current color, between the points (x1, y1) and (x2, y2).
Write down the coordinates to a int array and loop thought that array for all cords.
int[] line1 = new int[]{x1,y1,x2,y2};
int[] line2 = new int[]{x3,y3,x4,y4};
If you wonder how to draw the head you can use drawPolygon
int[] head = new int[]{x,y};
final int headSize = 50; //width and height in px
g.drawPolygon(head[0], head[1], headSize, headSize);
Hope it helped.
Regards -Max
Related
I have been trying to create a flight arrivals map program using windowbuilder however I have been stuck on displaying in the incoming flights for each airport
For context this map[ should display incoming flights when you hover your mouse on the letters(i.e hovering on YYZ will display incoming flight arrivals for Toronto airport)
I have an set of ArrayLists from a java file. Lets call it Aeroports.java which scans a csv file and scans it adding a line into a certain arraylist depending on its airport code(e.g YYZFlights)
The CSV file is in the format
DateOfFlight[0],DepartureTime1,ArrivalTime2,FlightDuration[3]
I have posted the full code below. I had to change the format of the dat so it could be read
Below is the program which stores the setters(procedures) and getters(functions) for each object
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.Date;
public class Flights {
private String flightNo;
private String departureCity;
private String departureAirport;
private String ArrivalAirport;
private int delayedTime;
private LocalTime newArrivalTime;
private LocalTime ArrivalTime;
private LocalTime departureTime;
private LocalDate FlightDate;
public void setFlightNo(String flightNo)///sets the flightNo for each object
{
this.flightNo = flightNo;
}
public String getFlightNo()
{
return flightNo;
}
public void setdepartureCity(String departureCity)///sets the departure city for each object
{
this.departureCity = departureCity;
}
public String getdepartureCity()
{
return departureCity;
}
public void setdepartureAirport(String departureAirport)///sets the departure airport for each object
{
this.departureAirport = departureAirport;
}
public String getdepartureAirport()
{
return departureAirport;
}
public void setdelayedTime(int delayedTime)
{
this.delayedTime = delayedTime;
}
public int getdelayedTime()
{
return delayedTime;
}
public void setnewArrivalTime(LocalTime newArrivalTime)
{
this.newArrivalTime = newArrivalTime;
}
public LocalTime getnewArrivalTime()
{
return newArrivalTime;
}
public void setArrivalTime(LocalTime newArrivalTime)
{
this.ArrivalTime = newArrivalTime;
}
public LocalTime getArrivalTime()
{
return ArrivalTime;
}
public void setdepartureTime(LocalTime departureTime)
{
this.departureTime = departureTime;
}
public LocalTime getdepartureTime()
{
return departureTime;
}
public void setArrivalAirport(String ArrivalAirport)
{
this.ArrivalAirport = ArrivalAirport;
}
public String getArrivalAirport()
{
return ArrivalAirport;
}
public void setFlightDate( LocalDate FlightDate)
{
this.FlightDate = FlightDate;
}
public LocalDate getFlightDate( )
{
return FlightDate;
}
}
Listed below is the main program where delayed flights are calculated for each object and stores the ArrayLists that I intend to use in the future
import java.util.ArrayList;
import java.util.Date;
import java.time.format.DateTimeFormatter;
import java.io.*;
import java.util.Scanner;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Timer;
import java.util.TimerTask;
import java.time.*;
public class BCLAirport {
static ArrayList<Flights> allFlights = new ArrayList<Flights>();
static ArrayList<Flights> alldelayedFlights = new ArrayList<Flights>();
static ArrayList<Flights> BCLFlights = new ArrayList<Flights>();///Array list for Airport with code BCL the ones for the others are listed below
static ArrayList<Flights> SYDFlights = new ArrayList<Flights>();
static ArrayList<Flights> DXBFlights = new ArrayList<Flights>();
static ArrayList<Flights> DELFlights = new ArrayList<Flights>();
static ArrayList<Flights> BKKFlights = new ArrayList<Flights>();
static ArrayList<Flights> DUBFlights = new ArrayList<Flights>();
static ArrayList<Flights> CDGFlights = new ArrayList<Flights>();
static ArrayList<Flights> GRUFlights = new ArrayList<Flights>();
static ArrayList<Flights> YYZFlights = new ArrayList<Flights>();
static ArrayList<Flights> JFKFlights = new ArrayList<Flights>();
static ArrayList<Flights> MEXFlights = new ArrayList<Flights>();
static ArrayList<Flights> ARNFlights = new ArrayList<Flights>();
static ArrayList<Flights> CAIFlights = new ArrayList<Flights>();
static ArrayList<Flights> LOSFlights = new ArrayList<Flights>();
static ArrayList<Flights> AMSFlights = new ArrayList<Flights>();
static ArrayList<Flights> LVSFlights = new ArrayList<Flights>();
static ArrayList<Flights> DFWFlights = new ArrayList<Flights>();
static ArrayList<Flights> JNBFlights = new ArrayList<Flights>();
static ArrayList<Flights> HNDFlights = new ArrayList<Flights>();
static ArrayList<Flights> SVOFlights = new ArrayList<Flights>();
static ArrayList<Flights> LISFlights = new ArrayList<Flights>();
static ArrayList<Flights> MADFlights = new ArrayList<Flights>();
static ArrayList<Flights> RAKFlights = new ArrayList<Flights>();
static ArrayList<Flights> ATHFlights = new ArrayList<Flights>();
static ArrayList<Flights> FCOFlights = new ArrayList<Flights>();
public static void mainrun() throws ParseException ///
{
try
{
File myObj = new File("C:/Flights.csv"//this leads to flight csv file imagine this in a format); //Scans for the csv file
Scanner myReader = new Scanner(myObj);///gets input of csv file
while (myReader.hasNextLine())
{
String[] data = myReader.nextLine().split(",");///splits the line by the commas
Flights Flights = new Flights();//creayes new object
Flights.setdepartureAirport(data[6]);///sets the departure airport
Flights.setdepartureCity(data[7]);/// sets the departure city
Flights.setFlightNo(data[10]);//sets the flight number
Flights.setdelayedTime(Integer.parseInt(data[5]));// sets the delayed time
Flights.setdepartureTime(LocalTime.parse(data[1]));///sets the departure Time
Flights.setArrivalAirport(data[8]);
Flights.setArrivalTime(LocalTime.parse(data[2]));
String[] dating = data[0].split("/");
String newdating = dating[2]+"-"+dating[1]+"-"+dating[0];
data[0]=newdating;//writes the date in the new format
LocalDate todaysDate = LocalDate.now();
Flights.setFlightDate(LocalDate.parse(data[0]));
int delayTime = Integer.parseInt(data[5]);/// integer for the delayed time in minutes
if (LocalDate.parse(newdating).equals(todaysDate)) {
System.out.println("yes");
if (Flights.getArrivalAirport() == "BCL") {///Array lists for each airport
BCLFlights.add(Flights);
} else if (Flights.getArrivalAirport() == "SYD") {
SYDFlights.add(Flights);
} else if (Flights.getArrivalAirport() == "DXB") {
DXBFlights.add(Flights);
} else if (Flights.getArrivalAirport() == "DEL") {
DELFlights.add(Flights);
} else if (Flights.getArrivalAirport() == "BKK") {
BKKFlights.add(Flights);
} else if (Flights.getArrivalAirport() == "DUB") {
DUBFlights.add(Flights);
} else if (Flights.getArrivalAirport() == "CDG") {
CDGFlights.add(Flights);
} else if (Flights.getArrivalAirport() == "GRU") {
GRUFlights.add(Flights);
} else if (Flights.getArrivalAirport() == "YYZ") {
YYZFlights.add(Flights);
} else if (Flights.getArrivalAirport() == "JFK") {
JFKFlights.add(Flights);
} else if (Flights.getArrivalAirport() == "MEX") {
MEXFlights.add(Flights);
} else if (Flights.getArrivalAirport() == "ARN") {
ARNFlights.add(Flights);
} else if (Flights.getArrivalAirport() == "CAI") {
CAIFlights.add(Flights);
} else if (Flights.getArrivalAirport() == "LOS") {
LOSFlights.add(Flights);
} else if (Flights.getArrivalAirport() == "AMS") {
AMSFlights.add(Flights);
} else if (Flights.getArrivalAirport() == "LVS") {
LVSFlights.add(Flights);
} else if (Flights.getArrivalAirport() == "DFW") {
DFWFlights.add(Flights);
} else if (Flights.getArrivalAirport() == "JNB") {
JNBFlights.add(Flights);
} else if (Flights.getArrivalAirport() == "HND") {
HNDFlights.add(Flights);
} else if (Flights.getArrivalAirport() == "SVO") {
SVOFlights.add(Flights);
} else if (Flights.getArrivalAirport() == "LIS") {
LISFlights.add(Flights);
} else if (Flights.getArrivalAirport() == "MAD") {
MADFlights.add(Flights);
} else if (Flights.getArrivalAirport() == "RAK") {
RAKFlights.add(Flights);
} else if (Flights.getArrivalAirport() == "ATH") {
ATHFlights.add(Flights);
} else if (Flights.getArrivalAirport() == "FCO") {
FCOFlights.add(Flights);
}
if (delayTime >= 30) {
LocalTime oldTime = Flights.getArrivalTime();
// Flights.setArrivalTime(oldTime);formatter.format(today)
LocalTime newTime = oldTime.plusMinutes(delayTime);/// adds the delayed time
Flights.setnewArrivalTime(newTime);
alldelayedFlights.add(Flights);
}
}
//........
//if flightdate .equals date;
DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
Date today = new Date();
//System.out.println(formatter.format(today));
// "2022-03-03"*/
// Date todayWithZeroTime = formatter.parse(formatter.format(today));
}
myReader.close();// closes the reader
//for(int i = 0; i < allFlights.size();i++)
///{
///System.out.println(allFlights.get(i).getdepartureCity() + " " +allFlights.get(i).getdepartureAirport() + " " + allFlights.get(i).getFlightNo() + " delayed by " + allFlights.get(i).getdelayedTime() + " minutes") ;
///}
for(int i = 0; i < alldelayedFlights.size();i++)///for the length of the array list delayed flights
{
//System.out.println(alldelayedFlights.get(i).getdepartureTime() + " " +alldelayedFlights.get(i).getdepartureAirport() + " " + alldelayedFlights.get(i).getArrivalAirport() + " " + alldelayedFlights.get(i).getArrivalTime() + " " + alldelayedFlights.get(i).getdelayedTime() + " " + alldelayedFlights.get(i).getnewArrivalTime() + " " + alldelayedFlights.get(i).getFlightDate()) ;
}
}
catch (FileNotFoundException e)
{
System.out.println("File cannot be found");
e.printStackTrace();
}
}
}
In another GUI java program I have used the pop up method to show a window depending on which airport I click on.
lblNewLabel_11 = new JLabel("HND");
lblNewLabel_11.addMouseListener(new MouseAdapter() {
Popup frame = new Popup();
public void mouseEntered(MouseEvent e) {
frame.setVisible(true);
}
public void mouseExited(MouseEvent e) {
frame.setVisible(false);
}
});
lblNewLabel_11.setFont(new Font("Tahoma", Font.BOLD, 10));
lblNewLabel_11.setBounds(817, 277, 27, 13);
contentPane.add(lblNewLabel_11);
lblNewLabel_12 = new JLabel("SYD");
lblNewLabel_12.addMouseListener(new MouseAdapter() {
Popup frame = new Popup();
public void mouseEntered(MouseEvent e) {
frame.setVisible(true);
}
public void mouseExited(MouseEvent e) {
frame.setVisible(false);
}
});
lblNewLabel_12.setFont(new Font("Tahoma", Font.BOLD, 10));
lblNewLabel_12.setBounds(855, 503, 45, 13);
contentPane.add(lblNewLabel_12);
lblNewLabel_13 = new JLabel("JNB");
lblNewLabel_13.addMouseListener(new MouseAdapter() {
Popup frame = new Popup();
public void mouseEntered(MouseEvent e) {
frame.setVisible(true);
}
public void mouseExited(MouseEvent e) {
frame.setVisible(false);
}
});
Below is the UI file which uses window builder to build a UI
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextPane;
import javax.swing.JLabel;
import javax.swing.JScrollBar;
import javax.swing.JTextField;
import java.awt.Font;
import javax.swing.JPasswordField;
import javax.swing.ImageIcon;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import com.toedter.calendar.JCalendar;
import javax.swing.JTextArea;
import javax.swing.JButton;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class MainFrame extends JFrame {
private JPanel contentPane;
private JPasswordField passwordField;
private JLabel lblNewLabel;
private JLabel lblNewLabel_1;
private JLabel lblMad;
private JLabel lblNewLabel_2;
private JLabel lblNewLabel_3;
private JLabel lblNewLabel_4;
private JLabel lblNewLabel_5;
private JLabel lblNewLabel_6;
private JLabel lblNewLabel_7;
private JLabel lblNewLabel_8;
private JLabel lblNewLabel_9;
private JLabel lblNewLabel_10;
private JLabel lblNewLabel_11;
private JLabel lblNewLabel_12;
private JLabel lblNewLabel_13;
private JLabel lblNewLabel_14;
private JLabel lblNewLabel_15;
private JLabel lblNewLabel_16;
private JLabel lblNewLabel_17;
private JLabel lblNewLabel_18;
private JLabel lblNewLabel_19;
private JLabel lblNewLabel_20;
private JLabel lblNewLabel_21;
private JLabel lblNewLabel_22;
private JLabel lblNewLabel_23;
private JLabel lblNewLabel_24;
private JLabel lblNewLabel_25;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
BCLAirport.mainrun();
MainFrame frame = new MainFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public MainFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 1351, 770);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
lblNewLabel_18 = new JLabel("DFW");
lblNewLabel_18.addMouseListener(new MouseAdapter() {
Popup frame = new Popup();
public void mouseEntered(MouseEvent e) {
frame.setVisible(true);
}
public void mouseExited(MouseEvent e) {
frame.setVisible(false);
}
});
lblNewLabel_18.setFont(new Font("Tahoma", Font.BOLD, 10));
lblNewLabel_18.setBounds(105, 290, 27, 13);
contentPane.add(lblNewLabel_18);
lblNewLabel_16 = new JLabel("LVS");
lblNewLabel_16.addMouseListener(new MouseAdapter() {
Popup frame = new Popup();
public void mouseEntered(MouseEvent e) {
frame.setVisible(true);
}
public void mouseExited(MouseEvent e) {
frame.setVisible(false);
}
});
lblNewLabel_16.setFont(new Font("Tahoma", Font.BOLD, 10));
lblNewLabel_16.setBounds(52, 267, 55, 13);
contentPane.add(lblNewLabel_16);
lblNewLabel_4 = new JLabel("YYZ\r\n");
lblNewLabel_4.addMouseListener(new MouseAdapter() {
Popup frame = new Popup();
public void mouseEntered(MouseEvent e) {
frame.setVisible(true);
}
public void mouseExited(MouseEvent e) {
frame.setVisible(false);
}
});
lblNewLabel_4.setFont(new Font("Tahoma", Font.BOLD, 10));
lblNewLabel_4.setBounds(187, 231, 33, 13);
contentPane.add(lblNewLabel_4);
lblNewLabel_2 = new JLabel("DUB");
lblNewLabel_2.addMouseListener(new MouseAdapter() {
Popup frame = new Popup();
public void mouseEntered(MouseEvent e) {
frame.setVisible(true);
}
public void mouseExited(MouseEvent e) {
frame.setVisible(false);
}
});
lblNewLabel_2.setFont(new Font("Tahoma", Font.BOLD, 10));
lblNewLabel_2.setBounds(388, 216, 27, 13);
contentPane.add(lblNewLabel_2);
lblNewLabel_1 = new JLabel("BCL");
lblNewLabel_1.addMouseListener(new MouseAdapter() {
Popup frame = new Popup();
public void mouseEntered(MouseEvent e) {
frame.setVisible(true);
}
public void mouseExited(MouseEvent e) {
frame.setVisible(false);
}
});
lblNewLabel_1.setFont(new Font("Tahoma", Font.BOLD, 10));
lblNewLabel_1.setBounds(420, 210, 27, 24);
contentPane.add(lblNewLabel_1);
lblNewLabel_3 = new JLabel("JFK");
lblNewLabel_3.addMouseListener(new MouseAdapter() {
Popup frame = new Popup();
public void mouseEntered(MouseEvent e) {
frame.setVisible(true);
}
public void mouseExited(MouseEvent e) {
frame.setVisible(false);
}
});
lblNewLabel_3.setFont(new Font("Tahoma", Font.BOLD, 10));
lblNewLabel_3.setBounds(193, 267, 27, 13);
contentPane.add(lblNewLabel_3);
lblMad = new JLabel("MAD");
lblMad.addMouseListener(new MouseAdapter() {
Popup frame = new Popup();
public void mouseEntered(MouseEvent e) {
frame.setVisible(true);
}
public void mouseExited(MouseEvent e) {
frame.setVisible(false);
}
});
lblMad.setFont(new Font("Tahoma", Font.BOLD, 10));
lblMad.setBounds(414, 267, 33, 13);
contentPane.add(lblMad);
lblNewLabel_5 = new JLabel("LIS");
lblNewLabel_5.addMouseListener(new MouseAdapter() {
Popup frame = new Popup();
public void mouseEntered(MouseEvent e) {
frame.setVisible(true);
}
public void mouseExited(MouseEvent e) {
frame.setVisible(false);
}
});
lblNewLabel_5.setFont(new Font("Tahoma", Font.BOLD, 10));
lblNewLabel_5.setBounds(383, 267, 21, 13);
contentPane.add(lblNewLabel_5);
lblNewLabel_6 = new JLabel("CAI");
lblNewLabel_6.addMouseListener(new MouseAdapter() {
Popup frame = new Popup();
public void mouseEntered(MouseEvent e) {
frame.setVisible(true);
}
public void mouseExited(MouseEvent e) {
frame.setVisible(false);
}
});
lblNewLabel_6.setFont(new Font("Tahoma", Font.BOLD, 10));
lblNewLabel_6.setBounds(502, 309, 27, 13);
contentPane.add(lblNewLabel_6);
lblNewLabel_7 = new JLabel("DXB");
lblNewLabel_7.addMouseListener(new MouseAdapter() {
Popup frame = new Popup();
public void mouseEntered(MouseEvent e) {
frame.setVisible(true);
}
public void mouseExited(MouseEvent e) {
frame.setVisible(false);
}
});
lblNewLabel_7.setFont(new Font("Tahoma", Font.BOLD, 10));
lblNewLabel_7.setBounds(569, 321, 27, 13);
contentPane.add(lblNewLabel_7);
lblNewLabel_8 = new JLabel("DEL");
lblNewLabel_8.addMouseListener(new MouseAdapter() {
Popup frame = new Popup();
public void mouseEntered(MouseEvent e) {
frame.setVisible(true);
}
public void mouseExited(MouseEvent e) {
frame.setVisible(false);
}
});
lblNewLabel_8.setFont(new Font("Tahoma", Font.BOLD, 10));
lblNewLabel_8.setBounds(647, 309, 21, 13);
contentPane.add(lblNewLabel_8);
lblNewLabel_9 = new JLabel("BKK");
lblNewLabel_9.addMouseListener(new MouseAdapter() {
Popup frame = new Popup();
public void mouseEntered(MouseEvent e) {
frame.setVisible(true);
}
public void mouseExited(MouseEvent e) {
frame.setVisible(false);
}
});
lblNewLabel_9.setFont(new Font("Tahoma", Font.BOLD, 10));
lblNewLabel_9.setBounds(727, 343, 27, 13);
contentPane.add(lblNewLabel_9);
lblNewLabel_10 = new JLabel("HKG");
lblNewLabel_10.addMouseListener(new MouseAdapter() {
Popup frame = new Popup();
public void mouseEntered(MouseEvent e) {
frame.setVisible(true);
}
public void mouseExited(MouseEvent e) {
frame.setVisible(false);
}
});
lblNewLabel_10.setFont(new Font("Tahoma", Font.BOLD, 10));
lblNewLabel_10.setBounds(756, 309, 27, 13);
contentPane.add(lblNewLabel_10);
lblNewLabel_11 = new JLabel("HND");
lblNewLabel_11.addMouseListener(new MouseAdapter() {
Popup frame = new Popup();
public void mouseEntered(MouseEvent e) {
frame.setVisible(true);
}
public void mouseExited(MouseEvent e) {
frame.setVisible(false);
}
});
lblNewLabel_11.setFont(new Font("Tahoma", Font.BOLD, 10));
lblNewLabel_11.setBounds(817, 277, 27, 13);
contentPane.add(lblNewLabel_11);
lblNewLabel_12 = new JLabel("SYD");
lblNewLabel_12.addMouseListener(new MouseAdapter() {
Popup frame = new Popup();
public void mouseEntered(MouseEvent e) {
frame.setVisible(true);
}
public void mouseExited(MouseEvent e) {
frame.setVisible(false);
}
});
lblNewLabel_12.setFont(new Font("Tahoma", Font.BOLD, 10));
lblNewLabel_12.setBounds(855, 503, 45, 13);
contentPane.add(lblNewLabel_12);
lblNewLabel_13 = new JLabel("JNB");
lblNewLabel_13.addMouseListener(new MouseAdapter() {
Popup frame = new Popup();
public void mouseEntered(MouseEvent e) {
frame.setVisible(true);
}
public void mouseExited(MouseEvent e) {
frame.setVisible(false);
}
});
lblNewLabel_13.setFont(new Font("Tahoma", Font.BOLD, 10));
lblNewLabel_13.setBounds(484, 503, 21, 13);
contentPane.add(lblNewLabel_13);
lblNewLabel_14 = new JLabel("LOS");
lblNewLabel_14.addMouseListener(new MouseAdapter() {
Popup frame = new Popup();
public void mouseEntered(MouseEvent e) {
frame.setVisible(true);
}
public void mouseExited(MouseEvent e) {
frame.setVisible(false);
}
});
lblNewLabel_14.setFont(new Font("Tahoma", Font.BOLD, 10));
lblNewLabel_14.setBounds(433, 371, 21, 13);
contentPane.add(lblNewLabel_14);
lblNewLabel_15 = new JLabel("RAK");
lblNewLabel_15.addMouseListener(new MouseAdapter() {
Popup frame = new Popup();
public void mouseEntered(MouseEvent e) {
frame.setVisible(true);
}
public void mouseExited(MouseEvent e) {
frame.setVisible(false);
}
});
lblNewLabel_15.setFont(new Font("Tahoma", Font.BOLD, 10));
lblNewLabel_15.setBounds(381, 290, 34, 13);
contentPane.add(lblNewLabel_15);
lblNewLabel_17 = new JLabel("GRU");
lblNewLabel_17.addMouseListener(new MouseAdapter() {
Popup frame = new Popup();
public void mouseEntered(MouseEvent e) {
frame.setVisible(true);
}
public void mouseExited(MouseEvent e) {
frame.setVisible(false);
}
});
lblNewLabel_17.setFont(new Font("Tahoma", Font.BOLD, 10));
lblNewLabel_17.setBounds(264, 480, 27, 13);
contentPane.add(lblNewLabel_17);
lblNewLabel_19 = new JLabel("MEX");
lblNewLabel_19.addMouseListener(new MouseAdapter() {
Popup frame = new Popup();
public void mouseEntered(MouseEvent e) {
frame.setVisible(true);
}
public void mouseExited(MouseEvent e) {
frame.setVisible(false);
}
});
lblNewLabel_19.setFont(new Font("Tahoma", Font.BOLD, 10));
lblNewLabel_19.setBounds(93, 321, 27, 13);
contentPane.add(lblNewLabel_19);
lblNewLabel_20 = new JLabel("ATH");
lblNewLabel_20.addMouseListener(new MouseAdapter() {
Popup frame = new Popup();
public void mouseEntered(MouseEvent e) {
frame.setVisible(true);
}
public void mouseExited(MouseEvent e) {
frame.setVisible(false);
}
});
lblNewLabel_20.setFont(new Font("Tahoma", Font.BOLD, 10));
lblNewLabel_20.setBounds(478, 267, 27, 13);
contentPane.add(lblNewLabel_20);
lblNewLabel_21 = new JLabel("CDG");
lblNewLabel_21.addMouseListener(new MouseAdapter() {
Popup frame = new Popup();
public void mouseEntered(MouseEvent e) {
frame.setVisible(true);
}
public void mouseExited(MouseEvent e) {
frame.setVisible(false);
}
});
lblNewLabel_21.setFont(new Font("Tahoma", Font.BOLD, 10));
lblNewLabel_21.setBounds(420, 244, 27, 13);
contentPane.add(lblNewLabel_21);
lblNewLabel_22 = new JLabel("ARN");
lblNewLabel_22.addMouseListener(new MouseAdapter() {
Popup frame = new Popup();
public void mouseEntered(MouseEvent e) {
frame.setVisible(true);
}
public void mouseExited(MouseEvent e) {
frame.setVisible(false);
}
});
lblNewLabel_22.setFont(new Font("Tahoma", Font.BOLD, 10));
lblNewLabel_22.setBounds(453, 186, 27, 13);
contentPane.add(lblNewLabel_22);
lblNewLabel_23 = new JLabel("AMS");
lblNewLabel_23.addMouseListener(new MouseAdapter() {
Popup frame = new Popup();
public void mouseEntered(MouseEvent e) {
frame.setVisible(true);
}
public void mouseExited(MouseEvent e) {
frame.setVisible(false);
}
});
lblNewLabel_23.setFont(new Font("Tahoma", Font.BOLD, 10));
lblNewLabel_23.setBounds(453, 216, 27, 13);
contentPane.add(lblNewLabel_23);
lblNewLabel_24 = new JLabel("SVO");
lblNewLabel_24.addMouseListener(new MouseAdapter() {
Popup frame = new Popup();
public void mouseEntered(MouseEvent e) {
frame.setVisible(true);
}
public void mouseExited(MouseEvent e) {
frame.setVisible(false);
}
});
lblNewLabel_24.setFont(new Font("Tahoma", Font.BOLD, 10));
lblNewLabel_24.setBounds(520, 216, 45, 13);
contentPane.add(lblNewLabel_24);
lblNewLabel_25 = new JLabel("FCO");
lblNewLabel_25.addMouseListener(new MouseAdapter() {
Popup frame = new Popup();
public void mouseEntered(MouseEvent e) {
frame.setVisible(true);
}
public void mouseExited(MouseEvent e) {
frame.setVisible(false);
}
});
lblNewLabel_25.setFont(new Font("Tahoma", Font.BOLD, 10));
lblNewLabel_25.setBounds(453, 255, 27, 13);
contentPane.add(lblNewLabel_25);
lblNewLabel = new JLabel("Map(don't delete)");
lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 10));
lblNewLabel.setBounds(0, 130, 944, 469);
lblNewLabel.setIcon(new ImageIcon("C:\\Users\\Student\\Pictures\\GUI\\map.png"));
contentPane.add(lblNewLabel);
JTextArea textArea = new JTextArea();
textArea.setBounds(970, 133, 334, 571);
contentPane.add(textArea);
JScrollPane scroll = new JScrollPane(textArea);
scroll.setBounds(970, 133, 334, 571);
contentPane.add(scroll);
JButton btnNewButton = new JButton("LVS");
lblMad.addMouseListener(new MouseAdapter() {
Popup frame = new Popup();
public void mouseEntered(MouseEvent e) {
frame.setVisible(true);
}
public void mouseExited(MouseEvent e) {
frame.setVisible(false);
}
});
btnNewButton.setBounds(52, 263, 45, 21);
contentPane.add(btnNewButton);
JLabel lblNewLabel_26 = new JLabel("Delayed Flights");
lblNewLabel_26.setFont(new Font("Tahoma", Font.BOLD, 21));
lblNewLabel_26.setBounds(1034, 72, 189, 51);
contentPane.add(lblNewLabel_26);
for (Flights f:BCLAirport.alldelayedFlights )
{
textArea.append(f.getdepartureTime() + " " +f.getdepartureAirport() + " " + f.getArrivalAirport() + " " + f.getArrivalTime() + " " + f.getdelayedTime() + " " + f.getnewArrivalTime() + " " + f.getFlightDate() + "\n" );//prints the results and goes to a new line
}
}
}
The main issue I have having is working out how to implement a way to show the incoming flightArrivals for each airport depending on which aiport I click for a reference see the picture for how the airport UI looksHere's UI normally
When I hover on the name of an airport at the moment blank window pops up but I want it to show the incoming arrivals for the day. I have been able to show delayedFlights but have been unable to show the specific arraylists for each airport which I want to show in the format
ArrivalTime, Departure City, Departure Airport, FlightNo
It should look like this when you hover on a airport nameHow it should ideally look like
If someone could help that would be great becaus I'm quite stuck
What exactly is your issue? Is it that you only see delayed flights but not all flights? You have static ArrayList<Flights> allFlights = new ArrayList<Flights>(); but as far as I see you never add a flight to it. You only add flights to the other lists.
Unrelated to your main issue, but some general observations:
Consider renaming Flights to Flight This class is a single flight, not a list of flights right?
You have an ArrayList<Flight> for every airport. This seems really difficult to work with. Maybe there is a different data structure that better fits your needs. Maybe Map<Airport, List<Flight>> flightsByAirport
It looks like each airport is it's own class? BCLAirport Maybe there is a different way to handle that.
I am trying to write a program to take a quiz and then display the score at the end of 5 q. But my score always shows 0 regardless of what I do.
I have attached most part of the code so that it can be inserted into an IDE for checking.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class OnlineExam extends JFrame
{
CardLayout cardlayout;
JPanel jpanel1,jpanel2,jpanel3,jpanel4,cardpanel,buttonpanel;
JLabel label1,label2,label3,label4,label5,label6,qlabel1,qlabel2,qlabel3,qlabel4,qlabel5,alabel1,alabel2,alabel3,alabel4,alabel5,jscore;
JTextArea field1,field2;
JButton b1,b2,ab11,ab12,ab13,ab14,ab21,ab22,ab23,ab24,ab25,ab31,ab32,ab33,ab34,ab35,ab41,ab42,ab43,ab44,ab45,ab51,ab52,ab53,ab54,ab55,result;
int score;
OnlineExam()
{
JFrame frame=new JFrame("Exam On C Programming");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,500);
cardlayout=new CardLayout();
cardpanel=new JPanel();
cardpanel.setLayout(cardlayout);
//JPanel1::
jpanel1=new JPanel();
jpanel1.setBackground(Color.pink);
label1=new JLabel("Enter Username");
label1.setFont(new Font("Arial",Font.BOLD,16));
label2=new JLabel("Enter password");
label2.setFont(new Font("Arial",Font.BOLD,16));
field1=new JTextArea(1,40);
field2=new JTextArea(1,40);
jpanel1.add(label1);
jpanel1.add(field1);
jpanel1.add(label2);
jpanel1.add(field2);
b1=new JButton("Next");
b1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
cardlayout.show(cardpanel,"2");
}
});
jpanel1.add(b1);
//jpanel 2::
jpanel2=new JPanel();
jpanel2.setBackground(Color.pink);
jpanel2.setLayout(new BoxLayout(jpanel2,BoxLayout.Y_AXIS));
label3=new JLabel(" EXAM INSTRUCTIONS ");
label3.setFont(new Font("Arial",Font.BOLD,20));
label4=new JLabel("1. The exam consists of 5 questions. Each question is of 1 mark.");
label4.setFont(new Font("Arial",Font.PLAIN,16));
label5=new JLabel("2. The total time alloted is 5 min.");
label5.setFont(new Font("Arial",Font.PLAIN,16));
label6=new JLabel("3. There will be no negative marking.");
label6.setFont(new Font("Arial",Font.PLAIN,16));
jpanel2.add(label3);
jpanel2.add(label4);
jpanel2.add(label5);
jpanel2.add(label6);
b2=new JButton("Ready for Exam? Click here.");
b2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
cardlayout.show(cardpanel,"3");
}
});
jpanel2.add(b2);
jpanel3=new JPanel();
jpanel3.setBackground(Color.yellow);
jpanel3.setLayout(new BoxLayout(jpanel3,BoxLayout.Y_AXIS));
qlabel1=new JLabel("1. Command Line Arguments have how many parameters?");
qlabel2=new JLabel("2. Graph data structure is used in____");
qlabel3=new JLabel("3. C language is a ________ language");
qlabel4=new JLabel("4. what is sequence of extensions of file created after we hit the run button? ");
qlabel5=new JLabel("5. Tower of Hanoi problem can be solved using____");
alabel1=new JLabel("Result");
alabel2=new JLabel("Result");
alabel3=new JLabel("Result");
alabel4=new JLabel("Result");
alabel5=new JLabel("Result");
ab11=new JButton("1");
ab11.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
alabel1.setText("Wrong");
}
});
ab12=new JButton("2");
ab12.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
alabel1.setText("Correct");
score++;
}
});
ab13=new JButton("3");
ab13.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
alabel1.setText("Wrong");
}
});
ab14=new JButton("None");
ab14.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
alabel1.setText("Wrong");
}
});
ab21=new JButton("running appliances");
ab21.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
alabel2.setText("Wrong");
}
});
ab22=new JButton("sharing media files");
ab22.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
alabel2.setText("Wrong");
}
});
ab23=new JButton("finding places");
ab23.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
alabel2.setText("Correct");
score++;
}
});
ab24=new JButton("playing video games");
ab24.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
alabel2.setText("Wrong");
}
});
ab31=new JButton("structural");
ab31.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
alabel3.setText("Wrong");
}
});
ab32=new JButton("procedural");
ab32.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
alabel3.setText("Correct");
score++;
}
});
ab33=new JButton("object oriented");
ab33.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
alabel3.setText("Wrong");
}
});
ab34=new JButton("None");
ab34.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
alabel3.setText("Wrong");
}
});
ab41=new JButton(".c->.obj->.bak");
ab41.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
alabel4.setText("Correct");
score++;
}
});
ab42=new JButton(".c->.bak->.obj");
ab42.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
alabel4.setText("Wrong");
}
});
ab43=new JButton(".obj->.bak->.c");
ab43.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
alabel4.setText("Wrong");
}
});
ab44=new JButton("None");
ab44.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{alabel4.setText("Wrong");
}
});
ab51=new JButton("binary tree");
ab51.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
alabel5.setText("Wrong");
}
});
ab52=new JButton("queue");
ab52.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
alabel5.setText("Wrong");
}
});
ab53=new JButton("recursion");
ab53.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
alabel5.setText("Correct");
score++;
}
});
ab54=new JButton("None");
ab54.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
alabel4.setText("Wrong");
}
});
jpanel3.add(qlabel1);
jpanel3.add(ab11);
jpanel3.add(ab12);
jpanel3.add(ab13);
jpanel3.add(ab14);
jpanel3.add(alabel1);
jpanel3.add(qlabel2);
jpanel3.add(ab21);
jpanel3.add(ab22);
jpanel3.add(ab23);
jpanel3.add(ab24);
jpanel3.add(alabel2);
jpanel3.add(qlabel3);
jpanel3.add(ab31);
jpanel3.add(ab32);
jpanel3.add(ab33);
jpanel3.add(ab34);
jpanel3.add(alabel3);
jpanel3.add(qlabel4);
jpanel3.add(ab41);
jpanel3.add(ab42);
jpanel3.add(ab43);
jpanel3.add(ab44);
jpanel3.add(alabel4);
jpanel3.add(qlabel5);
jpanel3.add(ab51);
jpanel3.add(ab52);
jpanel3.add(ab53);
jpanel3.add(ab54);
jpanel3.add(alabel5);
result=new JButton("Click to see your reportcard");
result.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
cardlayout.show(cardpanel,"4");
}
});
jpanel3.add(result);
jpanel4=new JPanel();
jscore=new JLabel();
if(score==5)
{
jscore.setText("******Congratulations!!! A perfect score indeed!!!!!!******");
}
else if(score>=3)
{
jscore.setText("You have passed. Your score is : "+ score);
}
else
{
jscore.setText("You have failed. Your score is :"+ score);
}
jpanel4.add(jscore);
cardpanel.add(jpanel1,"1");
cardpanel.add(jpanel2,"2");
cardpanel.add(jpanel3,"3");
cardpanel.add(jpanel4,"4");
add(cardpanel,BorderLayout.CENTER);
//add(buttonpanel,BorderLayout.SOUTH);
frame.setVisible(true);
frame.setResizable(true);
}
public static void main(String[] args)
{
OnlineExam frame = new OnlineExam();
frame.setTitle("EXAM ON C PROGRAMMING");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,500);
frame.setVisible(true);
frame.pack();
}
}
the problem is with location of your code:
if (score == 5) {
jscore.setText("******Congratulations!!! A perfect score indeed!!!!!!******");
} else if (score >= 3) {
jscore.setText("You have passed. Your score is : " + score);
} else {
jscore.setText("You have failed. Your score is :" + score);
}
according to your code this section runs only one time and the jscore text does not change after changing the correct answers so I moved this section of your code inside result button to check it after each click of this as:
result = new JButton("Click to see your reportcard");
result.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cardlayout.show(cardpanel, "4");
if (score == 5) {
jscore.setText("******Congratulations!!! A perfect score indeed!!!!!!******");
} else if (score >= 3) {
jscore.setText("You have passed. Your score is : " + score);
} else {
jscore.setText("You have failed. Your score is :" + score);
}
}
});
the complete code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class OnlineExam extends JFrame {
CardLayout cardlayout;
JPanel jpanel1, jpanel2, jpanel3, jpanel4, cardpanel, buttonpanel;
JLabel label1, label2, label3, label4, label5, label6, qlabel1, qlabel2, qlabel3, qlabel4, qlabel5, alabel1,
alabel2, alabel3, alabel4, alabel5, jscore;
JTextArea field1, field2;
JButton b1, b2, ab11, ab12, ab13, ab14, ab21, ab22, ab23, ab24, ab25, ab31, ab32, ab33, ab34, ab35, ab41, ab42,
ab43, ab44, ab45, ab51, ab52, ab53, ab54, ab55, result;
int score = 0;
OnlineExam() {
JFrame frame = new JFrame("Exam On C Programming");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
cardlayout = new CardLayout();
cardpanel = new JPanel();
cardpanel.setLayout(cardlayout);
//JPanel1::
jpanel1 = new JPanel();
jpanel1.setBackground(Color.pink);
label1 = new JLabel("Enter Username");
label1.setFont(new Font("Arial", Font.BOLD, 16));
label2 = new JLabel("Enter password");
label2.setFont(new Font("Arial", Font.BOLD, 16));
field1 = new JTextArea(1, 40);
field2 = new JTextArea(1, 40);
jpanel1.add(label1);
jpanel1.add(field1);
jpanel1.add(label2);
jpanel1.add(field2);
b1 = new JButton("Next");
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cardlayout.show(cardpanel, "2");
}
});
jpanel1.add(b1);
//jpanel 2::
jpanel2 = new JPanel();
jpanel2.setBackground(Color.pink);
jpanel2.setLayout(new BoxLayout(jpanel2, BoxLayout.Y_AXIS));
label3 = new JLabel(" EXAM INSTRUCTIONS ");
label3.setFont(new Font("Arial", Font.BOLD, 20));
label4 = new JLabel("1. The exam consists of 5 questions. Each question is of 1 mark.");
label4.setFont(new Font("Arial", Font.PLAIN, 16));
label5 = new JLabel("2. The total time alloted is 5 min.");
label5.setFont(new Font("Arial", Font.PLAIN, 16));
label6 = new JLabel("3. There will be no negative marking.");
label6.setFont(new Font("Arial", Font.PLAIN, 16));
jpanel2.add(label3);
jpanel2.add(label4);
jpanel2.add(label5);
jpanel2.add(label6);
b2 = new JButton("Ready for Exam? Click here.");
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cardlayout.show(cardpanel, "3");
}
});
jpanel2.add(b2);
jpanel3 = new JPanel();
jpanel3.setBackground(Color.yellow);
jpanel3.setLayout(new BoxLayout(jpanel3, BoxLayout.Y_AXIS));
qlabel1 = new JLabel("1. Command Line Arguments have how many parameters?");
qlabel2 = new JLabel("2. Graph data structure is used in____");
qlabel3 = new JLabel("3. C language is a ________ language");
qlabel4 = new JLabel("4. what is sequence of extensions of file created after we hit the run button? ");
qlabel5 = new JLabel("5. Tower of Hanoi problem can be solved using____");
alabel1 = new JLabel("Result");
alabel2 = new JLabel("Result");
alabel3 = new JLabel("Result");
alabel4 = new JLabel("Result");
alabel5 = new JLabel("Result");
ab11 = new JButton("1");
ab11.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
alabel1.setText("Wrong");
}
});
ab12 = new JButton("2");
ab12.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
alabel1.setText("Correct");
score++;
}
});
ab13 = new JButton("3");
ab13.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
alabel1.setText("Wrong");
}
});
ab14 = new JButton("None");
ab14.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
alabel1.setText("Wrong");
}
});
ab21 = new JButton("running appliances");
ab21.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
alabel2.setText("Wrong");
}
});
ab22 = new JButton("sharing media files");
ab22.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
alabel2.setText("Wrong");
}
});
ab23 = new JButton("finding places");
ab23.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
alabel2.setText("Correct");
score++;
}
});
ab24 = new JButton("playing video games");
ab24.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
alabel2.setText("Wrong");
}
});
ab31 = new JButton("structural");
ab31.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
alabel3.setText("Wrong");
}
});
ab32 = new JButton("procedural");
ab32.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
alabel3.setText("Correct");
score++;
}
});
ab33 = new JButton("object oriented");
ab33.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
alabel3.setText("Wrong");
}
});
ab34 = new JButton("None");
ab34.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
alabel3.setText("Wrong");
}
});
ab41 = new JButton(".c->.obj->.bak");
ab41.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
alabel4.setText("Correct");
score++;
}
});
ab42 = new JButton(".c->.bak->.obj");
ab42.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
alabel4.setText("Wrong");
}
});
ab43 = new JButton(".obj->.bak->.c");
ab43.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
alabel4.setText("Wrong");
}
});
ab44 = new JButton("None");
ab44.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
alabel4.setText("Wrong");
}
});
ab51 = new JButton("binary tree");
ab51.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
alabel5.setText("Wrong");
}
});
ab52 = new JButton("queue");
ab52.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
alabel5.setText("Wrong");
}
});
ab53 = new JButton("recursion");
ab53.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
alabel5.setText("Correct");
score++;
}
});
ab54 = new JButton("None");
ab54.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
alabel4.setText("Wrong");
}
});
jpanel3.add(qlabel1);
jpanel3.add(ab11);
jpanel3.add(ab12);
jpanel3.add(ab13);
jpanel3.add(ab14);
jpanel3.add(alabel1);
jpanel3.add(qlabel2);
jpanel3.add(ab21);
jpanel3.add(ab22);
jpanel3.add(ab23);
jpanel3.add(ab24);
jpanel3.add(alabel2);
jpanel3.add(qlabel3);
jpanel3.add(ab31);
jpanel3.add(ab32);
jpanel3.add(ab33);
jpanel3.add(ab34);
jpanel3.add(alabel3);
jpanel3.add(qlabel4);
jpanel3.add(ab41);
jpanel3.add(ab42);
jpanel3.add(ab43);
jpanel3.add(ab44);
jpanel3.add(alabel4);
jpanel3.add(qlabel5);
jpanel3.add(ab51);
jpanel3.add(ab52);
jpanel3.add(ab53);
jpanel3.add(ab54);
jpanel3.add(alabel5);
result = new JButton("Click to see your reportcard");
result.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cardlayout.show(cardpanel, "4");
if (score == 5) {
jscore.setText("******Congratulations!!! A perfect score indeed!!!!!!******");
} else if (score >= 3) {
jscore.setText("You have passed. Your score is : " + score);
} else {
jscore.setText("You have failed. Your score is :" + score);
}
}
});
jpanel3.add(result);
jpanel4 = new JPanel();
jscore = new JLabel();
jpanel4.add(jscore);
cardpanel.add(jpanel1, "1");
cardpanel.add(jpanel2, "2");
cardpanel.add(jpanel3, "3");
cardpanel.add(jpanel4, "4");
add(cardpanel, BorderLayout.CENTER);
// add(buttonpanel,BorderLayout.SOUTH);
frame.setVisible(true);
frame.setResizable(true);
}
public static void main(String[] args) {
OnlineExam frame = new OnlineExam();
frame.setTitle("EXAM ON C PROGRAMMING");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
frame.setVisible(true);
frame.pack();
}
}
I want to make a paint program.
When the user clicks the text button, then the user click the canvas the user can input the text they want in the clicked location.
What can I use to make that happen?
When i use drawstring, I can put the pre-defined text and user click location
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
Font font = new Font("Serif", Font.PLAIN, 40);
g2.setFont(font);
g2.drawString("text", x, y);
How can I change "text" to what the user inputs from keyboard?
My MainPanel Code (For View Mostly)
public class MainPanel extends JPanel implements ActionListener, ChangeListener {
private JButton button1, button2, button3, button4, button5, button6, button7, button8, button9, button10, button11, button12, activeButton;
private ButtonGroup buttonGroup;
private DrawPanel drawPanel;
private boolean insertedDrawPanel;
private JSlider slider;
private JTextField valueField;
private int jSliderMin, jSliderMax, jSliderInit;
private Toolkit toolkit;
private Image cursorImage;
private Point cursorHotSpot;
private Cursor fillCursor, penCursor, eraserCursor, lineCursor, rectCursor, ovalCursor, triangleCursor, starCursor;
private Color color1, color2;
public MainPanel()
{
try {
javax.swing.UIManager.setLookAndFeel( javax.swing.UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
Logger.getLogger(MainPanel.class.getName()).log(Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
Logger.getLogger(MainPanel.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
Logger.getLogger(MainPanel.class.getName()).log(Level.SEVERE, null, ex);
} catch (UnsupportedLookAndFeelException ex) {
Logger.getLogger(MainPanel.class.getName()).log(Level.SEVERE, null, ex);
}
setPreferredSize(new Dimension(1280, 720));
initiateComponent();
buildLayout();
registerListener();
checkDrawPanel();
}
public JPanel getDrawPanel() {
return drawPanel;
}
public void initiateComponent()
{
int width=54;
int height=48;
jSliderMin=1;
jSliderMax=100;
jSliderInit=1;
insertedDrawPanel=false;
BufferedImage buttonIcon;
activeButton= new JButton();
button1 = new JButton();
button1.setPreferredSize(new Dimension(width, height));
button1.setName("");
button2 = new JButton();
button2.setPreferredSize(new Dimension(width, height));
button2.setName("");
button3 = new JButton();
button3.setPreferredSize(new Dimension(width, height));
button4 = new JButton("");
button4.setPreferredSize(new Dimension(width, height));
button5 = new JButton("");
button5.setPreferredSize(new Dimension(width, height));
button6 = new JButton("");
button6.setPreferredSize(new Dimension(width, height));
button7 = new JButton("1");
button7.setPreferredSize(new Dimension(width, height));
button8 = new JButton("2");
button8.setPreferredSize(new Dimension(width, height));
button9 = new JButton("");
button9.setPreferredSize(new Dimension(width, height));
button10 = new JButton("");
button10.setPreferredSize(new Dimension(width, height));
button11 = new JButton("");
button11.setPreferredSize(new Dimension(width, height));
button12 = new JButton("");
button12.setPreferredSize(new Dimension(width, height));
public void buildLayout()
{
this.setLayout(new BorderLayout());
JPanel buttonPanel = new JPanel();
String colSizes = "20px, 54px, 10px, 54px, 20px";
String rowSizes = "20px, 48px, 20px, 48px, 20px, 48px, 20px, 48px, 20px, 200px, 48px, 48px, 20px, 48px";
FormLayout layout = new FormLayout(colSizes, rowSizes);
buttonPanel.setLayout(layout);
CellConstraints cc = new CellConstraints();
buttonPanel.add(button1, cc.xy(2, 2));
buttonPanel.add(button2, cc.xy(4, 2));
buttonPanel.add(button3, cc.xy(2, 4));
buttonPanel.add(button4, cc.xy(4, 4));
buttonPanel.add(button5, cc.xy(2, 6));
buttonPanel.add(button6, cc.xy(4, 6));
buttonPanel.add(button7, cc.xy(2, 8));
buttonPanel.add(button8, cc.xy(4, 8));
buttonPanel.add(slider, cc.xywh(2, 10, 3, 1));
buttonPanel.add(valueField, cc.xyw(2, 11, 3));
buttonPanel.add(button9, cc.xy(2, 12));
buttonPanel.add(button10, cc.xy(4, 12));
buttonPanel.add(button11, cc.xy(2, 14));
buttonPanel.add(button12, cc.xy(4, 14));
this.add(buttonPanel, BorderLayout.WEST);
}
public void registerListener()
{
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
button4.addActionListener(this);
button5.addActionListener(this);
button6.addActionListener(this);
button7.addActionListener(this);
button8.addActionListener(this);
button9.addActionListener(this);
button10.addActionListener(this);
button11.addActionListener(this);
button12.addActionListener(this);
slider.addChangeListener(this);
}
public void addDrawPanel(DrawPanel panel)
{
drawPanel=panel;
JPanel centerPanel = new JPanel();
String colSizes = "20px, pref,20px";
String rowSizes = "20px, pref,20px";
FormLayout layout = new FormLayout(colSizes, rowSizes);
centerPanel.setLayout(layout);
CellConstraints cc = new CellConstraints();
centerPanel.add(drawPanel, cc.xy(2, 2));
this.add(centerPanel, BorderLayout.CENTER);
insertedDrawPanel=true;
checkDrawPanel();
}
public void addOpenedImage(Image img)
{
drawPanel = new DrawPanel();
drawPanel.setPreferredSize(new Dimension(img.getWidth(null),img.getHeight(null)));
drawPanel.setBgImg(img);
JPanel centerPanel = new JPanel();
String colSizes = "20px, pref,20px";
String rowSizes = "20px, pref,20px";
FormLayout layout = new FormLayout(colSizes, rowSizes);
centerPanel.setLayout(layout);
CellConstraints cc = new CellConstraints();
centerPanel.add(drawPanel, cc.xy(2, 2));
this.add(centerPanel, BorderLayout.CENTER);
insertedDrawPanel=true;
checkDrawPanel();
}
#Override
public void actionPerformed(ActionEvent ae) {
if(ae.getSource().equals(button1))
{
activeButton=button1;
this.setCursor(penCursor);
resetButtonChanges();
drawPanel.setPenActive(true);
}
if(ae.getSource().equals(button2))
{
activeButton=button2;
this.setCursor(lineCursor);
resetButtonChanges();
drawPanel.setLineActive(true);
}
if(ae.getSource().equals(button3))
{
activeButton=button3;
this.setCursor(eraserCursor);
resetButtonChanges();
drawPanel.setEraserActive(true);
}
if(ae.getSource().equals(button4))
{
activeButton=button4;
resetButtonChanges();
drawPanel.setTextActive(true);
}
if(ae.getSource().equals(button7))
{
activeButton= button7;
resetButtonChanges();
color1 = JColorChooser.showDialog(null, "Pick your Background Color", color1);
if(color1!=null)
{
drawPanel.setBackground(color1);
}
}
if(ae.getSource().equals(button8))
{
activeButton= button8;
resetButtonChanges();
color2 = JColorChooser.showDialog(null, "Pick your Stroke / Shape Color", color2);
if(color2!=null)
{
drawPanel.setStrokecolor(color2);
}
}
if(ae.getSource().equals(button9))
{
activeButton=button9;
this.setCursor(rectCursor);
resetButtonChanges();
drawPanel.setRectActive(true);
}
if(ae.getSource().equals(button10))
{
activeButton=button10;
this.setCursor(ovalCursor);
resetButtonChanges();
drawPanel.setOvalActive(true);
}
if(ae.getSource().equals(button11))
{
activeButton=button11;
this.setCursor(triangleCursor);
resetButtonChanges();
drawPanel.setTriangleActive(true);
}
if(ae.getSource().equals(button12))
{
activeButton=button12;
this.setCursor(starCursor);
resetButtonChanges();
drawPanel.setStarActive(true);
}
}
public void resetButtonChanges()
{
if(activeButton!=button1)
{
drawPanel.setPenActive(false);
}
if(activeButton!=button2)
{
drawPanel.setLineActive(false);
}
if(activeButton!=button3)
{
drawPanel.setEraserActive(false);
}
if(activeButton!=button4)
{
drawPanel.setTextActive(false);
}
if(activeButton!=button9)
{
drawPanel.setRectActive(false);
}
if(activeButton!=button10)
{
drawPanel.setOvalActive(false);
}
if(activeButton!=button11)
{
drawPanel.setTriangleActive(false);
}
if(activeButton!=button12)
{
drawPanel.setStarActive(false);
}
}
public void checkDrawPanel()
{
if(insertedDrawPanel==false)
{
button1.setEnabled(false);
button2.setEnabled(false);
button3.setEnabled(false);
button4.setEnabled(false);
button5.setEnabled(false);
button6.setEnabled(false);
button7.setEnabled(false);
button8.setEnabled(false);
button9.setEnabled(false);
button10.setEnabled(false);
button11.setEnabled(false);
button12.setEnabled(false);
slider.setEnabled(false);
}
else
{
button1.setEnabled(true);
button2.setEnabled(true);
button3.setEnabled(true);
button4.setEnabled(true);
button5.setEnabled(true);
button6.setEnabled(true);
button7.setEnabled(true);
button8.setEnabled(true);
button9.setEnabled(true);
button10.setEnabled(true);
button11.setEnabled(true);
button12.setEnabled(true);
slider.setEnabled(true);
}
}
#Override
public void stateChanged(ChangeEvent ce) {
if(ce.getSource().equals(slider))
{
drawPanel.setStrokevalue(slider.getValue());
valueField.setText(String.valueOf(slider.getValue()));
}
if(ce.getSource().equals(button7))
{
button7.setBackground(color1);
button7.setForeground(color2);
}
if(ce.getSource().equals(button8))
{
button8.setBackground(color2);
button8.setForeground(color1);
}
}
}
My DrawPanel Code
public class DrawPanel extends JPanel implements ActionListener, FocusListener, DocumentListener, MouseListener, MouseMotionListener{
private boolean penActive ,eraserActive, lineActive, rectActive, ovalActive, triangleActive, starActive, textActive, isDrag, isReleased;
private int x, y , xDrag, yDrag, xRelease, yRelease, strokevalue;
private Color strokecolor ,dragColor;
private Vector<int[]> allShapes;
private int[] dragShape;
private Vector<Color> shapeColor;
private Vector<String> type;
private String dragType;
private JTextField text;
private Image bgImg;
public boolean isPenActive() {
return penActive;
}
public void setPenActive(boolean penActive) {
this.penActive = penActive;
}
public boolean isEraserActive() {
return eraserActive;
}
public void setEraserActive(boolean eraserActive) {
this.eraserActive = eraserActive;
}
public boolean isTriangleActive() {
return triangleActive;
}
public void setTriangleActive(boolean triangleActive) {
this.triangleActive = triangleActive;
}
public boolean isLineActive() {
return lineActive;
}
public void setLineActive(boolean lineActive) {
this.lineActive = lineActive;
}
public int getStrokevalue() {
return strokevalue;
}
public void setStrokevalue(int strokevalue) {
this.strokevalue = strokevalue;
}
public Color getStrokecolor() {
return strokecolor;
}
public void setStrokecolor(Color strokecolor) {
this.strokecolor = strokecolor;
}
public boolean isRectActive() {
return rectActive;
}
public void setRectActive(boolean rectActive) {
this.rectActive = rectActive;
}
public boolean isOvalActive() {
return ovalActive;
}
public void setOvalActive(boolean ovalActive) {
this.ovalActive = ovalActive;
}
public boolean isStarActive() {
return starActive;
}
public void setStarActive(boolean starActive) {
this.starActive = starActive;
}
public boolean isTextActive() {
return textActive;
}
public void setTextActive(boolean textActive) {
this.textActive = textActive;
}
public Image getBgImg() {
return bgImg;
}
public void setBgImg(Image bgImg) {
this.bgImg = bgImg;
}
public DrawPanel()
{
setFocusable(true);
setBackground(Color.white);
initiateComponents();
addMouseMotionListener(this);
addMouseListener(this);
}
public void initiateComponents()
{
allShapes=new Vector<int[]>();
shapeColor=new Vector<Color>();
type=new Vector<String>();
eraserActive = false;
penActive = false;
lineActive = false;
rectActive = false;
ovalActive = false;
triangleActive = false;
starActive = false;
strokevalue=1;
strokecolor=Color.BLACK;
}
#Override
public void mouseClicked(MouseEvent me) {
if (me.getClickCount() == 2)
text.setEditable( true );
}
#Override
public void mousePressed(MouseEvent me) {
x = me.getX();
y = me.getY();
if(me.getClickCount() == 1)
{
this.requestFocusInWindow();
}
if(me.getClickCount() == 2)
{
System.out.println("asd");
text.setLocation(me.getPoint());
this.add(text);
text.requestFocusInWindow();
text.selectAll();
repaint();
}
}
#Override
public void mouseReleased(MouseEvent me) {
isDrag=false;
isReleased=true;
xRelease=me.getX();
yRelease=me.getY();
repaint();
}
#Override
public void mouseEntered(MouseEvent me) {
}
#Override
public void mouseExited(MouseEvent me) {
}
#Override
public void mouseDragged(MouseEvent me) {
isDrag=true;
isReleased=false;
xDrag=me.getX();
yDrag=me.getY();
repaint();
}
#Override
public void mouseMoved(MouseEvent me) {
}
#Override
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
if(penActive==true)
{
int[]coordinates=new int[5];
if(isDrag)
{
coordinates[0]=x;
coordinates[1]=y;
coordinates[2]=xDrag;
coordinates[3]=yDrag;
coordinates[4]=strokevalue;
allShapes.add(coordinates);
shapeColor.add(strokecolor);
type.add("Pen");
x=xDrag;
y=yDrag;
}
}
else if(eraserActive==true)
{
int[]coordinates=new int[5];
if(isDrag)
{
coordinates[0]=x;
coordinates[1]=y;
coordinates[2]=xDrag;
coordinates[3]=yDrag;
coordinates[4]=strokevalue;
allShapes.add(coordinates);
shapeColor.add(this.getBackground());
type.add("Eraser");
x=xDrag;
y=yDrag;
}
}
else if(lineActive==true)
{
int[]coordinates=new int[5];
if(isDrag)
{
dragShape=new int[5];
dragShape[0]=x;
dragShape[1]=y;
dragShape[2]=xDrag;
dragShape[3]=yDrag;
dragShape[4]=strokevalue;
dragColor=strokecolor;
dragType="Line";
}
if(isReleased)
{
dragShape=null;
coordinates[0]=x;
coordinates[1]=y;
coordinates[2]=xDrag;
coordinates[3]=yDrag;
coordinates[4]=strokevalue;
allShapes.add(coordinates);
shapeColor.add(strokecolor);
type.add("Line");
}
}
else if(textActive==true)
{
int[]coordinates=new int[3];
if(isReleased)
{
dragShape=null;
coordinates[0]=x;
coordinates[1]=y;
coordinates[2]=strokevalue;
allShapes.add(coordinates);
shapeColor.add(strokecolor);
type.add("Text");
}
}
else if(triangleActive==true)
{
int[]coordinates=new int[4];
if(isDrag)
{
dragShape=new int[4];
dragShape[0]=x;
dragShape[1]=y;
dragShape[2]=-y+yDrag;
dragShape[3]=strokevalue;
dragColor=strokecolor;
dragType="Triangle";
}
if(isReleased)
{
dragShape=null;
coordinates[0]=x;
coordinates[1]=y;
coordinates[2]=-y+yDrag;
coordinates[3]=strokevalue;
allShapes.add(coordinates);
shapeColor.add(strokecolor);
type.add("Triangle");
}
}
else if(starActive==true)
{
int[]coordinates=new int[4];
if(isDrag)
{
dragShape=new int[4];
dragShape[0]=x;
dragShape[1]=y;
dragShape[2]=-y+yDrag;
dragShape[3]=strokevalue;
dragColor=strokecolor;
dragType="Star";
}
if(isReleased)
{
dragShape=null;
coordinates[0]=x;
coordinates[1]=y;
coordinates[2]=-y+yDrag;
coordinates[3]=strokevalue;
allShapes.add(coordinates);
shapeColor.add(strokecolor);
type.add("Star");
}
}
else if(rectActive==true)
{
int[]coordinates=new int[5];
if(isDrag)
{
dragShape=new int[5];
dragShape[0]=x;
dragShape[1]=y;
dragShape[2]=xDrag;
dragShape[3]=yDrag;
dragShape[4]= strokevalue;
dragColor=strokecolor;
dragType="Rectangle";
}
if(isReleased)
{
dragShape=null;
coordinates[0]=x;
coordinates[1]=y;
coordinates[2]=xDrag;
coordinates[3]=yDrag;
coordinates[4]=strokevalue;
allShapes.add(coordinates);
shapeColor.add(strokecolor);
type.add("Rectangle");
}
}
else if(ovalActive==true)
{
int[]coordinates=new int[4];
if(isDrag)
{
dragShape=new int[4];
dragShape[0]=x;
dragShape[1]=y;
dragShape[2]=-y+yDrag;
dragShape[3]=strokevalue;
dragColor=strokecolor;
dragType="Oval";
}
if(isReleased)
{
dragShape=null;
coordinates[0]=x;
coordinates[1]=y;
coordinates[2]=-y+yDrag;
coordinates[3]=strokevalue;
allShapes.add(coordinates);
shapeColor.add(strokecolor);
type.add("Oval");
}
}
if(bgImg!=null)
{
g2.drawImage(bgImg, 0, 0, null);
}
for(int a=0;a<allShapes.size();a++)
{
if(type.get(a).equals("Pen")||type.get(a).equals("Eraser")||type.get(a).equals("Line"))
{
int[]coordinates=allShapes.get(a);
Color color=shapeColor.get(a);
g2.setStroke(new BasicStroke(coordinates[4]));
g2.setPaint(color);
g2.drawLine(coordinates[0], coordinates[1], coordinates[2], coordinates[3]);
}
else if(type.get(a).equals("Text"))
{
text = new JTextField("asd");
Font font = new Font("Serif", Font.PLAIN, 40);
g2.setFont(font);
}
else if(type.get(a).equals("Triangle"))
{
int[]coordinates=allShapes.get(a);
Point2D b= new Point2D.Double(coordinates[0], coordinates[1]);
Triangle triangle = new Triangle(b,coordinates[2]);
g2.setStroke(new BasicStroke(coordinates[3]));
g2.setPaint(dragColor);
g2.draw(triangle);
}
else if(type.get(a).equals("Star"))
{
int[]coordinates=allShapes.get(a);
Point2D b= new Point2D.Double(coordinates[0], coordinates[1]);
Star star = new Star(b,coordinates[2], coordinates[2]);
g2.setStroke(new BasicStroke(coordinates[3]));
g2.setPaint(dragColor);
g2.draw(star);
}
else if(type.get(a).equals("Rectangle"))
{
int[]coordinates=allShapes.get(a);
Point2D b=new Point2D.Double(coordinates[0], coordinates[1]);
Point2D c=new Point2D.Double(coordinates[2], coordinates[3]);
Rectangle rectangle = new Rectangle(b,c);
g2.setStroke(new BasicStroke(coordinates[4]));
g2.setPaint(dragColor);
g2.draw(rectangle);
}
else if(type.get(a).equals("Oval"))
{
int[]coordinates=allShapes.get(a);
Point2D b= new Point2D.Double(coordinates[0], coordinates[1]);
Circle oval = new Circle(b,coordinates[0], coordinates[1], coordinates[2]);
g2.setStroke(new BasicStroke(coordinates[3]));
g2.setPaint(dragColor);
g2.draw(oval);
}
}
if(dragShape!=null)
{
if(dragType.equals("Line"))
{
g2.setStroke(new BasicStroke(dragShape[4]));
g2.setPaint(dragColor);
g2.drawLine(dragShape[0], dragShape[1], dragShape[2], dragShape[3]);
}
else if(dragType.equals("Triangle"))
{
Point2D a= new Point2D.Double(dragShape[0], dragShape[1]);
Triangle triangle = new Triangle(a,dragShape[2]);
g2.setStroke(new BasicStroke(dragShape[3]));
g2.setPaint(dragColor);
g2.draw(triangle);
}
else if(dragType.equals("Star"))
{
Point2D a= new Point2D.Double(dragShape[0], dragShape[1]);
Star star = new Star(a,dragShape[2],dragShape[2]);
g2.setStroke(new BasicStroke(dragShape[3]));
g2.setPaint(dragColor);
g2.draw(star);
}
else if(dragType.equals("Rectangle")){
Point2D b=new Point2D.Double(dragShape[0], dragShape[1]);
Point2D c=new Point2D.Double(dragShape[2], dragShape[3]);
Rectangle rectangle = new Rectangle(b,c);
g2.setStroke(new BasicStroke(dragShape[4]));
g2.setPaint(dragColor);
g2.draw(rectangle);
}
else if(dragType.equals("Oval"))
{
Point2D a= new Point2D.Double(dragShape[0], dragShape[1]);
Circle oval = new Circle(a,dragShape[0],dragShape[1], dragShape[2]);
g2.setStroke(new BasicStroke(dragShape[3]));
g2.setPaint(dragColor);
g2.draw(oval);
}
}
}
#Override
public void focusGained(FocusEvent e) {
}
#Override
public void focusLost(FocusEvent e) {
text.setEditable(false);
}
#Override
public void insertUpdate(DocumentEvent e) {
text.setSize(getPreferredSize());
}
#Override
public void removeUpdate(DocumentEvent e) {
text.setSize(getPreferredSize());
}
#Override
public void changedUpdate(DocumentEvent e) {
}
#Override
public void actionPerformed(ActionEvent e) {
text.setEditable(false);
}
}
Here is an example that uses a JTextField to enter text at the location.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
public class InvisibleTextField extends JTextField
implements ActionListener, FocusListener, MouseListener, DocumentListener
{
public InvisibleTextField()
{
setOpaque( false );
setColumns( 1 );
// setBorder( null );
setSize( getPreferredSize() );
setColumns( 0 );
addActionListener( this );
addFocusListener( this );
addMouseListener( this );
getDocument().addDocumentListener( this );
}
// Implement ActionListener
public void actionPerformed(ActionEvent e)
{
setEditable( false );
}
// Implement FocusListener
public void focusLost(FocusEvent e)
{
setEditable( false );
}
public void focusGained(FocusEvent e) {}
// Implement MouseListener
public void mouseClicked( MouseEvent e )
{
if (e.getClickCount() == 2)
setEditable( true );
}
public void mouseEntered( MouseEvent e ) {}
public void mouseExited( MouseEvent e ) {}
public void mousePressed( MouseEvent e ) {}
public void mouseReleased( MouseEvent e ) {}
// Implement DocumentListener
public void insertUpdate(DocumentEvent e)
{
updateSize();
}
public void removeUpdate(DocumentEvent e)
{
updateSize();
}
public void changedUpdate(DocumentEvent e) {}
private void updateSize()
{
setSize( getPreferredSize() );
}
public static void main(String[] args)
{
JPanel panel = new JPanel();
panel.setFocusable( true );
panel.setLayout( null );
panel.addMouseListener( new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
JPanel panel = (JPanel)e.getSource();
if (e.getClickCount() == 1)
{
panel.requestFocusInWindow();
}
if (e.getClickCount() == 2)
{
InvisibleTextField tf = new InvisibleTextField();
tf.setLocation(e.getPoint());
panel.add( tf );
tf.requestFocusInWindow();
}
}
});
JFrame frame = new JFrame();
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.add(new JLabel("Double Click to Add Text"), BorderLayout.NORTH);
frame.add(panel);
frame.setSize(650, 300);
frame.setLocationRelativeTo( null );
frame.setVisible(true);
}
}
You can edit the text at any time by double clicking on the text.
When I run my program the JButtons don't appear until i change the size of the screen(setResizable = true;).
here is my code:
public class MainMenu extends JPanel {
Kingdomcraft kd;
Screen screen;
JButton playSP;
JButton playMP;
JButton settings;
JButton fullscreen;
JButton quit;
JButton createWorld;
JButton addServer;
JSlider sound;
JSlider light;
JList worldList;
JList serverList;
JTextField worldName;
JTextField serverName;
JTextField serverIP;
JButton addNewWorld;
JButton addNewServer;
private Preferences prefs;
private int soundLevel;
private int lightLevel;
public static boolean isFullscreen = false;
public static boolean serverNameFilled = false;
public static boolean serverIPFilled = false;
public void run() {
kd = new Kingdomcraft();
screen = new Screen();
playSP = new JButton("Singleplayer");
playMP = new JButton("Multiplayer");
settings = new JButton("Settings");
fullscreen = new JButton("Fullscreen");
quit = new JButton("Quit");
createWorld = new JButton("Create World");
addServer = new JButton("Add Server");
sound = new JSlider();
light = new JSlider();
prefs = Preferences.userNodeForPackage(MainMenu.class);
soundLevel = prefs.getInt("SOUND_LEVEL", 50);
lightLevel = prefs.getInt("LIGHT_LEVEL", 100);
worldList = new JList();
serverList = new JList();
worldName = new JTextField();
serverName = new JTextField();
serverIP = new JTextField();
addNewWorld = new JButton("Add");
addNewServer = new JButton("Add");
this.setBackground(Color.BLACK);
if (kd.inMainMenu) {
add(playSP);
playSP.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
remove(sound);
remove(light);
remove(fullscreen);
remove(addServer);
remove(serverName);
remove(serverIP);
remove(addNewServer);
repaint();
add(createWorld);
createWorld.setSize(110, 25);
createWorld.setLocation(playSP.getX() - (playSP.getWidth() / 2) - 5, playSP.getY() + 35);
createWorld.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
add(worldName);
worldName.setSize(110, 25);
worldName.setLocation(createWorld.getX(), createWorld.getY() + 35);
worldName.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
add(addNewWorld);
addNewWorld.setSize(110, 25);
addNewWorld.setLocation(worldName.getX(), worldName.getY() + 35);
}
});
}
});
}
});
add(playMP);
playMP.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
remove(sound);
remove(light);
remove(fullscreen);
remove(createWorld);
remove(worldName);
remove(addNewWorld);
repaint();
add(addServer);
addServer.setSize(100, 25);
addServer.setLocation(playMP.getX() - (playMP.getWidth() / 2) - 5, playMP.getY() + 35);
addServer.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
add(serverName);
serverName.setSize(100, 25);
serverName.setLocation(addServer.getX(), addServer.getY() + 35);
serverName.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
MainMenu.serverNameFilled = true;
}
});
add(serverIP);
serverIP.setSize(100, 25);
serverIP.setLocation(serverName.getX(), serverName.getY() + 35);
serverIP.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
MainMenu.serverIPFilled = true;
}
});
if (serverNameFilled && serverIPFilled) {
add(addNewServer);
addNewServer.setSize(100, 25);
addNewServer.setLocation(serverIP.getX(), serverIP.getY() + 35);
addNewServer.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
}
}
});
}
});
add(settings);
settings.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
remove(createWorld);
remove(addServer);
remove(worldName);
remove(serverName);
remove(serverIP);
remove(addNewWorld);
remove(addNewServer);
repaint();
add(sound);
sound.setSize(settings.getWidth(), settings.getHeight());
sound.setLocation(settings.getX() + (settings.getWidth() / 2) + 5, settings.getY() + 35);
sound.setOpaque(false);
sound.setMinimum(0);
sound.setMaximum(100);
sound.setValueIsAdjusting(true);
sound.setValue(soundLevel);
sound.setToolTipText("Audio: " + soundLevel + "%");
sound.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
soundLevel = sound.getValue();
sound.setToolTipText("Audio: " + soundLevel + "%");
prefs.putInt("SOUND_LEVEL", soundLevel);
}
});
add(light);
light.setSize(settings.getWidth(), settings.getHeight());
light.setLocation(sound.getX(), sound.getY() + 35);
light.setOpaque(false);
light.setMinimum(50);
light.setMaximum(150);
light.setValueIsAdjusting(true);
light.setValue(lightLevel);
light.setToolTipText("Brightness: " + lightLevel + "%");
light.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
lightLevel = light.getValue();
light.setToolTipText("Brightness: " + lightLevel + "%");
prefs.putInt("LIGHT_LEVEL", lightLevel);
}
});
add(fullscreen);
fullscreen.setSize(100, settings.getHeight());
fullscreen.setLocation(settings.getX() + (settings.getWidth() / 2) - 50, light.getY() + 35);
fullscreen.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
MainMenu.isFullscreen = true;
}
});
}
});
add(quit);
quit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(ABORT);
}
});
}
}
}
I understand that this is a lot of code to skim through but I'm just plain stumped.
When you add a UI component in swing, you need to call the component's revalidate method for the changes to take effect.
Can't tell exactly what you are doing but I'm not sure you should be continually removing/adding individual components.
Maybe you should be use a Card Layout which will allow to you remove/add panels based on the specific function the user requests.
This gui should draw moving images on frame panel called "system". But first of all i need to make those objects. Here i'm trying to add them to an array and use that array in other classes. But array keeps being empty!
I guess the problem is in declaring(bottom of the code). There is no exception thrown because I let other classes to execute only when this array(Planetarium) is not empty(it should work like that, because other moves depends on planets created(those objects)). But if it is empty that means nothing is declared...
What should i do if I want to fill an array in the thread executed in event listener?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class GUI extends Frame implements WindowListener,ActionListener {
cpWindow window1 = new cpWindow();
cmWindow window2 = new cmWindow();
Delete window3 = new Delete();
SolarSystem system = new SolarSystem(300,300);
Planet[] Planetarium = null; // using this to make an array
Moon[] Moonarium = null;
/**
* Frame for general window.
*/
public void createFrame0(){
JFrame f0 = new JFrame("Choose what you want to do");
f0.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f0.addWindowListener(this);
f0.setLayout(new GridLayout(3,1));
JButton cP = new JButton("Create a planet");
JButton cM = new JButton("Create a moon");
JButton Delete = new JButton("Annihilate a planet or moon");
f0.add(cP);
f0.add(cM);
f0.add(Delete);
cP.addActionListener(this);
cM.addActionListener(this);
Delete.addActionListener(this);
cP.setActionCommand("1");
cM.setActionCommand("2");
Delete.setActionCommand("3");
f0.pack();
f0.setVisible(true);
}
/**
* Frame for planet adding window.
*/
class cpWindow implements ActionListener,WindowListener{
JLabel name1 = new JLabel("Name");
JLabel color1 = new JLabel("Color");
JLabel diam1 = new JLabel("Diameter");
JLabel dist1 = new JLabel("Distance");
JLabel speed1 = new JLabel("Speed");
JTextField name2 = new JTextField();
JTextField color2 = new JTextField();
JTextField diam2 = new JTextField();
JTextField dist2 = new JTextField();
JTextField speed2 = new JTextField();
double distance;
int Speed;
double diameter;
public void createFrame1() {
JFrame f1 = new JFrame("Add planet");
f1.addWindowListener(this);
f1.setLayout(new GridLayout(6,2,5,5));
JButton mygt = new JButton("Create planet");
mygt.addActionListener(this);
name2.setText("belekoks");color2.setText("RED");diam2.setText("30");dist2.setText("60");spe ed2.setText("2");
f1.add(name1);f1.add(name2);f1.add(color1);f1.add(color2);f1.add(diam1);
f1.add(diam2);f1.add(dist1);f1.add(dist2);f1.add(speed1);f1.add(speed2);
f1.add(mygt);
f1.pack();
f1.setVisible(true);
}
public void createVariables(){
try {
distance = Double.parseDouble(dist2.getText());
Speed = Integer.parseInt(speed2.getText());
diameter = Double.parseDouble(diam2.getText());
}
catch(NumberFormatException i) {
}
Main.diametras = diameter;
Main.distancija = distance;
Main.greitis = Speed;
Main.vardas = name2.getText();
Main.spalva = color2.getText();
}
public void actionPerformed(ActionEvent e) {
createVariables();
new NewThread().start();
list.display();
}
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
public void windowClosed(WindowEvent e) {}
public void windowActivated(WindowEvent arg0) {}
public void windowDeactivated(WindowEvent arg0) {}
public void windowDeiconified(WindowEvent arg0) {}
public void windowIconified(WindowEvent arg0) {}
public void windowOpened(WindowEvent arg0) {}
}
/**
* Frame for moon adding window
*/
CheckboxGroup planets = new CheckboxGroup();
String which;
class cmWindow implements ActionListener,WindowListener, ItemListener{
JLabel name1 = new JLabel("Name");
JLabel color1 = new JLabel("Color");
JLabel diam1 = new JLabel("Diameter");
JLabel speed1 = new JLabel("Speed");
JTextField name2 = new JTextField();
JTextField color2 = new JTextField();
JTextField diam2 = new JTextField();
JTextField speed2 = new JTextField();
JLabel info = new JLabel("Which planet's moon it will be?");
JLabel corDist1 = new JLabel("Distance from centre of rotation");
JLabel corSpeed1 = new JLabel("Speed which moon centres");
JTextField corDist2 = new JTextField();
JTextField corSpeed2 = new JTextField();
int cordistance;
int corspeed;
public void createFrame1() {
JFrame f1 = new JFrame("Add moon");
f1.addWindowListener(this);
f1.setLayout(new GridLayout(8,2,5,5));
JButton mygt = new JButton("Create moon");
mygt.addActionListener(this);
for(int i=0;i<Planetarium.length;i++){
add(new Checkbox(Planetarium[i].nam,planets,false));
}
corDist2.setText("15");corSpeed2.setText("3");
f1.add(name1);f1.add(name2);f1.add(color1);f1.add(color2);
f1.add(diam1);f1.add(diam2);f1.add(speed1);f1.add(speed2);
f1.add(corDist1);f1.add(corDist2);f1.add(corSpeed1);f1.add(corSpeed2);
f1.add(mygt);
f1.pack();
f1.setVisible(true);
}
int Speed;
double diameter;
public void createVariables(){
try {
Speed = Integer.parseInt(speed2.getText());
diameter = Double.parseDouble(diam2.getText());
cordistance = Integer.parseInt(corDist2.getText());
corspeed = Integer.parseInt(corSpeed2.getText());
}
catch(NumberFormatException i) {}
Main.diametras = diameter;
Main.greitis = Speed;
Main.vardas = name2.getText();
Main.spalva = color2.getText();
Main.centGrt = corspeed;
Main.centAts = cordistance;
}
public void actionPerformed(ActionEvent e) {
createVariables();
new NewThread().start();
}
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
public void windowClosed(WindowEvent e) {}
public void windowActivated(WindowEvent arg0) {}
public void windowDeactivated(WindowEvent arg0) {}
public void windowDeiconified(WindowEvent arg0) {}
public void windowIconified(WindowEvent arg0) {}
public void windowOpened(WindowEvent arg0) {}
#Override
public void itemStateChanged(ItemEvent e) {
which = planets.getSelectedCheckbox().getLabel();
}
}
/**
* Deleting window
*/
class Delete implements ActionListener,WindowListener{
Checkbox[] checkB = new Checkbox[100];
public void createFrame2(){
JFrame f2 = new JFrame("Death Start");
f2.addWindowListener(this);
f2.setLayout(new GridLayout(6,2,5,5));
JButton Del = new JButton("Shoot it!");
Del.addActionListener(this);
JLabel[] planetName = new JLabel[100];
for(int i=0;i<Planetarium.length;i++){
planetName[i].setText(Planetarium[i].nam);
checkB[i].setLabel("This");
checkB[i].setState(false);
f2.add(planetName[i]);f2.add(checkB[i]);
}
}
public void actionPerformed(ActionEvent e) {
for(int i=0;i<Planetarium.length;i++){
if(checkB[i].getState()){
Planetarium[i] = null;
}
}
}
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
public void windowClosed(WindowEvent e) {}
public void windowActivated(WindowEvent arg0) {}
public void windowDeactivated(WindowEvent arg0) {}
public void windowDeiconified(WindowEvent arg0) {}
public void windowIconified(WindowEvent arg0) {}
public void windowOpened(WindowEvent arg0) {}
}
////////////////////////////////////////////////////////
public GUI() {
createFrame0();
}
public void actionPerformed(ActionEvent e) {
if ("1".equals(e.getActionCommand())) {this.window1.createFrame1();}
else if ("2".equals(e.getActionCommand()) & Planetarium != null) {this.window2.createFrame1();}
else if ("3".equals(e.getActionCommand()) & Planetarium != null) {this.window3.createFrame2();}
}
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
public void windowClosed(WindowEvent e) {}
public void windowActivated(WindowEvent arg0) {}
public void windowDeactivated(WindowEvent arg0) {}
public void windowDeiconified(WindowEvent arg0) {}
public void windowIconified(WindowEvent arg0) {}
public void windowOpened(WindowEvent arg0) {}
LinkedList list = new LinkedList();
class NewThread extends Thread {
Thread t;
NewThread() {
t = new Thread(this);
t.start();
}
public void run() {
Moon moon = null;
Planet planet = new Planet(Main.vardas,Main.distancija,Main.diametras,Main.spalva,Main.greitis);
if(Planetarium != null){
for(int i=0;i<Planetarium.length;i++){
if (which == Planetarium[i].nam){
moon = new Moon(Main.vardas,Planetarium[i].dist,Main.diametras,Main.spalva,Main.greitis,Main.centGrt,Main.centAts);
}
}}
int a=0,b=0;
int i = 0;
if (Main.centAts == 0){
Planetarium[i] = planet; //i guess problem is here
a++;
list.add(planet);
for(i=0; i <= i+1 0; i++) {
planet.move();
planet.drawOn(system);
system.finishedDrawing();
if (i==360){i=0;}
}
}
else{
Moonarium[i] = moon;
b++;
if(i==Main.greitis){
for(int l = 0; l <= l+1; l++) {
moon.move();
moon.drawOn(system);
system.finishedDrawing();
}}
}
}
}
}
EDIT: add linkedlist(still nothing after display) and moved declaration before infinite loop
You don't appear to have assigned these arrays anything so they should be null rather than empty.
The way you are using them a List might be better.
final List<Planet> planetarium = new ArrayList<Planet>();
planetarium.add(new Planet( .... ));
Planet p = planetarium.get(i);
for(Planet p: planetarium){
// something for each planet.
}
Note: you have to create an array before using it and you have know its length which is fixed. A list can have any length, but you still need to create it first.
Look at the loop before:
Planetarium[i] = planet;
It looks like it will loop infinitely
for(i=0; i >= 0; i++)
i will alway be >= 0.