So I am currently working on the graphics for a game, and I wanted to include a fade effect to add depth to the game. I am also using JButtons, but the problem is, my fade effect is fading everything, including my JButtons. I want to make it so that only the black rectangle I imported fades, and that the JButtons remain visible throughout. The background images are fine, it is just the JButtons. The JButtons fade, and then reappear when I hover over them. Any ideas on how to fix the issue?
My code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.IOException;
// playButton.setContentAreaFilled(false);
// playButton.setEnabled(false);
public class Frame extends JPanel implements ActionListener, KeyListener, MouseListener{
JButton playButton;
JButton instructionButton;
JButton lvlOneButton;
JButton lvlTwoButton;
JButton lvlThreeButton;
JButton backButton;
//Music mainTheme = new Music("Hyrule Field.wav");
BufferedImage instructions = null;
boolean onInstructions = false;
BufferedImage home = null;
boolean onHome = true;
BufferedImage levels = null;
boolean onLevels = false;
//Image fadeRectangle = new ImageIcon("black rectangle.png").getImage();
BufferedImage fadeRectangle = null;
Timer timer = new Timer(10, this);
float alpha = 1f;
Frame(){
loadImage();
timer.start();
playButton = new JButton();
playButton.setBounds(310, 320, 150, 70);
playButton.addActionListener(this);
playButton.addMouseListener(this);
playButton.setText("Play");
playButton.setFocusable(false);
playButton.setFont(new Font("Dialog", Font.BOLD, 40));
playButton.setForeground(Color.white);
playButton.setBackground(new Color(255, 115, 115));
playButton.setBorder(BorderFactory.createLineBorder(Color.white, 5));
playButton.setVisible(true);
instructionButton = new JButton();
instructionButton.setBounds(310, 400, 150, 70);
instructionButton.addActionListener(this);
instructionButton.addMouseListener(this);
instructionButton.setText("Instructions");
instructionButton.setFocusable(false);
instructionButton.setFont(new Font("Dialog", Font.BOLD, 20));
instructionButton.setForeground(Color.white);
instructionButton.setBackground(new Color(255, 115, 115));
instructionButton.setBorder(BorderFactory.createLineBorder(Color.white, 5));
instructionButton.setVisible(true);
lvlOneButton = new JButton();
lvlOneButton.setBounds(110, 250, 150, 150);
lvlOneButton.addActionListener(this);
lvlOneButton.addMouseListener(this);
lvlOneButton.setText("1");
lvlOneButton.setFocusable(false);
lvlOneButton.setFont(new Font("Dialog", Font.BOLD, 80));
lvlOneButton.setForeground(Color.white);
lvlOneButton.setBackground(new Color(255, 115, 115));
lvlOneButton.setBorder(BorderFactory.createLineBorder(Color.white, 5));
lvlOneButton.setVisible(false);
lvlTwoButton = new JButton();
lvlTwoButton.setBounds(310, 250, 150, 150);
lvlTwoButton.addActionListener(this);
lvlTwoButton.addMouseListener(this);
lvlTwoButton.setText("2");
lvlTwoButton.setFocusable(false);
lvlTwoButton.setFont(new Font("Dialog", Font.BOLD, 80));
lvlTwoButton.setForeground(Color.white);
lvlTwoButton.setBackground(new Color(255, 115, 115));
lvlTwoButton.setBorder(BorderFactory.createLineBorder(Color.white, 5));
lvlTwoButton.setVisible(false);
lvlThreeButton = new JButton();
lvlThreeButton.setBounds(510, 250, 150, 150);
lvlThreeButton.addActionListener(this);
lvlThreeButton.addMouseListener(this);
lvlThreeButton.setText("3");
lvlThreeButton.setFocusable(false);
lvlThreeButton.setFont(new Font("Dialog", Font.BOLD, 80));
lvlThreeButton.setForeground(Color.white);
lvlThreeButton.setBackground(new Color(255, 115, 115));
lvlThreeButton.setBorder(BorderFactory.createLineBorder(Color.white, 5));
lvlThreeButton.setVisible(false);
backButton = new JButton();
backButton.setBounds(10, 10, 150, 70);
backButton.addActionListener(this);
backButton.addMouseListener(this);
backButton.setText("Back");
backButton.setFocusable(false);
backButton.setFont(new Font("Dialog", Font.BOLD, 40));
backButton.setForeground(Color.white);
backButton.setBackground(new Color(255, 115, 115));
backButton.setBorder(BorderFactory.createLineBorder(Color.white, 5));
backButton.setVisible(false);
this.setLayout(null);
this.addMouseListener(this);
this.addKeyListener(this);
this.add(playButton);
this.add(instructionButton);
this.add(lvlOneButton);
this.add(lvlTwoButton);
this.add(lvlThreeButton);
this.add(backButton);
}
public void loadImage(){
try {
fadeRectangle = ImageIO.read(getClass().getClassLoader().getResource("black rectangle.png"));
instructions = ImageIO.read(getClass().getClassLoader().getResource("instructions.png"));
home = ImageIO.read(getClass().getClassLoader().getResource("home screen.png"));
levels = ImageIO.read(getClass().getClassLoader().getResource("level screen.png"));
} catch (IOException e) {
}
}
#Override
public void actionPerformed(ActionEvent event) {
alpha += -0.01f;
repaint();
if (alpha <= 0) {
alpha = 0;
timer.stop();
}
if (event.getSource() == playButton) {
fadeOut();
onHome = false;
onLevels = true;
playButton.setVisible(false);
instructionButton.setVisible(false);
lvlThreeButton.setVisible(true);
lvlTwoButton.setVisible(true);
lvlOneButton.setVisible(true);
backButton.setVisible(true);
}
if (event.getSource() == instructionButton) {
fadeOut();
onHome = false;
onInstructions = true;
playButton.setVisible(false);
instructionButton.setVisible(false);
backButton.setVisible(true);
}
if (event.getSource() == backButton) {
fadeOut();
onHome = true;
onInstructions = false;
onLevels = false;
playButton.setVisible(true);
instructionButton.setVisible(true);
lvlThreeButton.setVisible(false);
lvlTwoButton.setVisible(false);
lvlOneButton.setVisible(false);
backButton.setVisible(false);
}
if (event.getSource() == lvlOneButton) {
fadeOut();
lvlThreeButton.setVisible(false);
lvlTwoButton.setVisible(false);
lvlOneButton.setVisible(false);
backButton.setVisible(false);
//do level one stuff here fjkdsnfkjdsnn
}
}
public void fadeOut(){
loadImage();
alpha = 1;
timer.start();
alpha += -0.01f;
repaint();
if (alpha <= 0) {
alpha = 0;
timer.stop();
}
}
#Override
public void keyTyped(KeyEvent event) {
}
#Override
public void keyPressed(KeyEvent event) {
// TODO Auto-generated method stub
}
#Override
public void keyReleased(KeyEvent event) {
// TODO Auto-generated method stub
}
#Override
public void mouseClicked(MouseEvent e) {
}
#Override
public void mousePressed(MouseEvent e) {
}
#Override
public void mouseReleased(MouseEvent e) {
}
#Override
//makes button color lighter when hovering over
public void mouseEntered(MouseEvent e) {
if (e.getSource() == playButton){
playButton.setBackground(new Color(253, 214, 214));
}
if (e.getSource() == lvlOneButton){
lvlOneButton.setBackground(new Color(253, 214, 214));
}
if (e.getSource() == lvlTwoButton){
lvlTwoButton.setBackground(new Color(253, 214, 214));
}
if (e.getSource() == lvlThreeButton){
lvlThreeButton.setBackground(new Color(253, 214, 214));
}
if (e.getSource() == backButton){
backButton.setBackground(new Color(253, 214, 214));
}
if (e.getSource() == instructionButton){
instructionButton.setBackground(new Color(253, 214, 214));
}
}
#Override
//returns button color to original when not hovering
public void mouseExited(MouseEvent e) {
if (e.getSource() == playButton){
playButton.setBackground(new Color(255, 115, 115));
}
if (e.getSource() == lvlOneButton){
lvlOneButton.setBackground(new Color(255, 115, 115));
}
if (e.getSource() == lvlTwoButton){
lvlTwoButton.setBackground(new Color(255, 115, 115));
}
if (e.getSource() == lvlThreeButton){
lvlThreeButton.setBackground(new Color(255, 115, 115));
}
if (e.getSource() == backButton){
backButton.setBackground(new Color(255, 115, 115));
}
if (e.getSource() == instructionButton){
instructionButton.setBackground(new Color(255, 115, 115));
}
}
#Override
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
if (onInstructions){
g2d.setComposite(AlphaComposite.SrcOver);
g2d.drawImage(instructions, 0, 0, 800, 600, null);
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
g2d.drawImage(fadeRectangle, 0, 0, null);
}
if (onHome){
g2d.setComposite(AlphaComposite.SrcOver);
g2d.drawImage(home, 0, 0, 800, 600, null);
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
g2d.drawImage(fadeRectangle, 0, 0, null);
}
if (onLevels){
g2d.setComposite(AlphaComposite.SrcOver);
g2d.drawImage(levels, 0, 0, 800, 600, null);
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
g2d.drawImage(fadeRectangle, 0, 0, null);
}
super.paint(g);
}
public static void main(String args[]){
JFrame frame = new JFrame();
frame.add(new Frame());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 600);
frame.setVisible(true);
frame.setResizable(false);
}
}
I tried to put super.paint(g); at the end but that did not fix it. I want to find a way to edit JButton properties in the paintComponent.
Related
learning the basics here.
I just want to know how would I have to arrange the code such that the paint method runs at the beginning of the program and not when you click a button.
or am I forgetting something important?
i know that paint method is automatically called when you set the visibility to true but other than that, im not sure.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Triangle extends JFrame implements ActionListener
{
private JButton b1 = new JButton ("Blue");
private JButton b2 = new JButton ("Red");
private boolean color = true;
public Triangle()
{
setLayout(new FlowLayout());
add(b1);
add(b2);
b1.addActionListener(this);
b2.addActionListener(this);
setTitle("Triangle");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500,400);
setLocation(300,300);
setVisible(true);
}
public void paint(Graphics g)
{
if(color == true)
{
super.paint(g);
g.setColor(Color.white);
g.drawLine(100, 70, 100, 250);
g.drawLine(100, 250, 400, 250);
g.drawLine(100,70,400,250);
getContentPane().setBackground(Color.red);
}
else
{
super.paint(g);
g.setColor(Color.white);
g.drawLine(100, 70, 100, 250);
g.drawLine(100, 250, 400, 250);
g.drawLine(100,70,400,250);
getContentPane().setBackground(Color.BLUE);
}
}
public void actionPerformed(ActionEvent a)
{
if(a.getSource() == b2)
{
color = true;
repaint();
}
else
{
color = false;
repaint();
}
}
}
my paint method was originally like this
public void paint(Graphics g)
{
super.paint(g);
g.setColor(Color.white);
g.drawLine(100, 70, 100, 250);
g.drawLine(100, 250, 400, 250);
g.drawLine(100,70,400,250);
if(color == true)
{
getContentPane().setBackground(Color.red);
}
else
{
getContentPane().setBackground(Color.BLUE);
}
}
This question already has answers here:
Show an animated BG in Swing
(3 answers)
Closed 6 years ago.
im making a mathematical game and want to use a gif as a background. Its dimensions are 1100*800
I searched many posts how to add a GIF as background, but with no success. Any suggestions for a easy method (if using other components -JPanel,...; could you please show how?)
So far, this is my code of the JFrame:
public class Game extends JFrame implements ActionListener {
private JButton play, endG, tutorial, login, easy, medium, hard, next, checkAnswer;
private JTextArea answer;
int total, goodAnswer = 0;
public Game(String heading) {
super(heading);
this.setSize(1100, 800);
this.setLayout(null);
firstScreen();
setResizable(false);
}
public void firstScreen() {
getContentPane().removeAll();
play = new JButton();
play.setBounds(373, 350, 354, 80);
play.setIcon(new ImageIcon("entrancePlayButton.png"));
play.addActionListener(this);
play.setOpaque(false);
play.setContentAreaFilled(false);
add(play);
tutorial = new JButton("Tutorial");
tutorial.setBounds(345, 520, 150, 50);
tutorial.setFont(new Font("Arial", Font.PLAIN, 20));
tutorial.addActionListener(this);
tutorial.setOpaque(false);
tutorial.setContentAreaFilled(false);
add(tutorial);
endG = new JButton("End Game");
endG.setBounds(605, 520, 150, 50);
endG.setFont(new Font("Arial", Font.PLAIN, 20));
endG.addActionListener(this);
endG.setOpaque(false);
endG.setContentAreaFilled(false);
add(endG);
revalidate();
repaint();
}
public void difficultyScreen() {
getContentPane().removeAll();
easy = new JButton("Easy");
easy.setBounds(450, 310, 200, 80);
easy.setFont(new Font("Arial", Font.PLAIN, 30));
easy.addActionListener(this);
easy.setOpaque(false);
easy.setContentAreaFilled(false);
add(easy);
medium = new JButton("Medium");
medium.setBounds(450, 440, 200, 80);
medium.setFont(new Font("Arial", Font.PLAIN, 30));
medium.addActionListener(this);
medium.setOpaque(false);
medium.setContentAreaFilled(false);
add(medium);
hard = new JButton("Hard");
hard.setBounds(450, 570, 200, 80);
hard.setFont(new Font("Arial", Font.PLAIN, 30));
hard.addActionListener(this);
hard.setOpaque(false);
hard.setContentAreaFilled(false);
add(hard);
endG = new JButton("Exit");
endG.setBounds(1000, 700, 60, 30);
endG.setFont(new Font("Arial", Font.PLAIN, 15));
endG.addActionListener(this);
endG.setOpaque(false);
endG.setContentAreaFilled(false);
add(endG);
revalidate();
repaint();
}
public void playGameScreen() {
getContentPane().removeAll();
revalidate();
repaint();
}
public void tutorialScreen() {
getContentPane().removeAll();
revalidate();
repaint();
}
private static double stringToDouble(String number) {
double num = Double.parseDouble(number);
return num;
}
public static void main() {
Game areaGame = new Game("Area Game");
areaGame.setVisible(true);
}
public void actionPerformed(ActionEvent actionEvent) {
if (actionEvent.getSource() == play) {
difficultyScreen();
}
if (actionEvent.getSource() == tutorial) {
tutorialScreen();
}
if (actionEvent.getSource() == endG) {
int reply = JOptionPane.showConfirmDialog(null, "You are about to exit the game, are you sure?", "Exit game", JOptionPane.YES_NO_OPTION);
if (reply == JOptionPane.YES_OPTION) {
System.exit(0);
}
}
}
}
There are two ways to do this:
you can override the paintComponent() method of your JPanel.
like this:
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(yourImage, 0, 0, this);
}
or you can use a JLabel by loading the image as an ImageIcon and then displaying it in a JLabel.
I am trying to simulate a traffic light with radio buttons. No matter where I put the call to repaint(), it does not seem to call the paintComponent method. Any help would be appreciated. Here is my code. Sorry if this is kind of long.
package trafficlight;
import java.awt.BorderLayout;
import javax.swing.*;
import java.awt.event.*;
public class Frame extends JFrame{
Drawing ob = new Drawing();
private JPanel buttonPanel;
private JRadioButton red;
private JRadioButton yellow;
private JRadioButton green;
public Frame(String title, int width, int height) {
super(title);
setSize(width, height);
buttonPanel = new JPanel();
//creating JFrame components
red = new JRadioButton("Red");
yellow = new JRadioButton("Yellow");
green = new JRadioButton("Green");
buttonPanel.add(red);
buttonPanel.add(yellow);
buttonPanel.add(green);
//JRadioButton group allows only one button to be selected at a time
ButtonGroup group = new ButtonGroup();
group.add(red);
group.add(yellow);
group.add(green);
//adding components to frame
add(buttonPanel, BorderLayout.SOUTH);
//Adding action listeners
red.addActionListener(new Listener());
yellow.addActionListener(new Listener());
green.addActionListener(new Listener());
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
//Listener class to handle action events
private class Listener implements ActionListener{
#Override
public void actionPerformed(ActionEvent e){
if(red.isSelected()){
ob.setRed(true);
ob.setYellow(false);
ob.setGreen(false);
ob.repaint();
}
else if(yellow.isSelected()){
ob.setYellow(true);
ob.setGreen(false);
ob.setRed(false);
ob.repaint();
}
else if(green.isSelected()){
ob.setGreen(true);
ob.setYellow(false);
ob.setRed(false);
ob.repaint();
}
}
}
}
Here is my seconds class
package trafficlight;
import java.awt.*;
import javax.swing.*;
public class Drawing extends JPanel{
private boolean red = false;
private boolean yellow = false;
private boolean green = false;
public void setRed(boolean clicked){
this.red = clicked;
}
public void setYellow(boolean clicked){
this.yellow = clicked;
}
public void setGreen(boolean clicked){
this.green = clicked;
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.black);
g.drawRect(85, 20, 60, 110);
if(!red){
g.setColor(Color.black);
g.drawOval(100, 25, 30, 30);
}
else{
g.setColor(Color.red);
g.fillOval(100, 25, 30, 30);
}
if(!yellow){
g.setColor(Color.black);
g.drawOval(100, 60, 30, 30);
}
else{
g.setColor(Color.yellow);
g.fillOval(100, 60, 30, 30);
}
if(!green){
g.setColor(Color.black);
g.drawOval(100, 95, 30, 30);
}
else{
g.setColor(Color.green);
g.fillOval(100, 95, 30, 30);
}
}
}
And here's the main method
package trafficlight;
public class TrafficLight {
public static void main(String[] args) {
Frame test = new Frame("TrafficLight", 250, 250);
test.add(new Drawing());
}
}
Some improvements for your code
Don't inherit from JFrame. You don't add any value to this class (composition over inheritance)
Your ActionListener doesn't need to be a named class because it hasn't got any state
No need for a field in your case
Use the Color constants with the UPPERCASE i.e. Color.BLACK
Enqueue Swing application in the EDT
Use more descriptive names for classes and variables
Then your program may look like this:
public class TrafficLightUI {
public TrafficLightUI(String title, int width, int height) {
JFrame frame = new JFrame(title);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(width, height);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
JPanel mainPanel = new JPanel(new BorderLayout());
JPanel buttonPanel = new JPanel();
JRadioButton redRadioButton = new JRadioButton("Red");
JRadioButton yellowRadioButton = new JRadioButton("Yellow");
JRadioButton greenRadioButton = new JRadioButton("Green");
buttonPanel.add(redRadioButton);
buttonPanel.add(yellowRadioButton);
buttonPanel.add(greenRadioButton);
//JRadioButton group allows only one button to be selected at a time
ButtonGroup buttonGroup = new ButtonGroup();
buttonGroup.add(redRadioButton);
buttonGroup.add(yellowRadioButton);
buttonGroup.add(greenRadioButton);
TrafficLightPanel trafficLight = new TrafficLightPanel();
//adding components to frame
mainPanel.add(buttonPanel, BorderLayout.SOUTH);
mainPanel.add(trafficLight, BorderLayout.CENTER);
//Adding action listeners
redRadioButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
setTrafficLight(true, false, false, trafficLight);
}
});
yellowRadioButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
setTrafficLight(false, true, false, trafficLight);
}
});
greenRadioButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
setTrafficLight(false, false, true, trafficLight);
}
});
frame.add(mainPanel);
frame.setVisible(true);
}
private void setTrafficLight(boolean red, boolean yellow, boolean green, TrafficLightPanel trafficLight) {
trafficLight.setGreen(green);
trafficLight.setYellow(yellow);
trafficLight.setRed(red);
trafficLight.repaint();
}
}
TrafficLightPanel
public class TrafficLightPanel extends JPanel {
private static final long serialVersionUID = 1L;
private boolean red = false;
private boolean yellow = false;
private boolean green = false;
public void setRed(boolean isRed) {
this.red = isRed;
}
public void setYellow(boolean isYellow) {
this.yellow = isYellow;
}
public void setGreen(boolean isGreen) {
this.green = isGreen;
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLACK);
g.drawRect(85, 20, 60, 110);
if (!red) {
g.setColor(Color.BLACK);
g.drawOval(100, 25, 30, 30);
} else {
g.setColor(Color.RED);
g.fillOval(100, 25, 30, 30);
}
if (!yellow) {
g.setColor(Color.BLACK);
g.drawOval(100, 60, 30, 30);
} else {
g.setColor(Color.YELLOW);
g.fillOval(100, 60, 30, 30);
}
if (!green) {
g.setColor(Color.BLACK);
g.drawOval(100, 95, 30, 30);
} else {
g.setColor(Color.GREEN);
g.fillOval(100, 95, 30, 30);
}
}
}
Main
public class TrafficLight {
public static void main(String... args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new TrafficLightUI("TrafficLight", 250, 250);
}
});
}
}
I'm trying to make a launcher. It works fine, except that when I press the 'Options' button it makes a little window in the corner. How do I prevent this?
The Launcher picture:
(source: gyazo.com)
And now with the option frame running:
(source: gyazo.com)
You can see the little window in the corner. It does not disappear when the Option frame does.
The Main File:
package ca.sidez.Launcher;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import ca.sidez.Main.Game;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
public class Main {
private Game ga;
public static int game = 0;
public static int isUpdate = 0;
public static int BigVersion = 00;
public static String Version = "04";
public static String Potenic_Version = "1.6a";
public static String DirtLife_Version = "0.1";
public static int lang = 0;
private static void createWindow() {
Font font;
Font font2;
final JFrame frame = new JFrame("Nic Launcher " + Version);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(820, 640);
frame.setLocationRelativeTo(null);
frame.setLayout(null);
frame.setResizable(false);
JEditorPane jep = new JEditorPane();
jep.setEditable(false);
font = new Font("Times New Roman", Font.PLAIN, 28);
font2 = new Font("Arial", Font.BOLD, 14);
jep.setContentType("text/html");
jep.setText("<html> Loading... </html>");
jep.setBorder(new EmptyBorder(0, 0, 0, 0));
try {
jep.setPage("http://potenic.tumblr.com/");
} catch(IOException e) {
jep.setContentType("text/html");
jep.setText("<html>Could not load, Check Your Connection</html>");
}
JScrollPane scrollPane = new JScrollPane(jep);
scrollPane.setBackground(Color.BLACK);
scrollPane.setSize(640, 480);
scrollPane.setLocation(0, 0);
scrollPane.setBorder(new EmptyBorder(0, 0, 0, 0));
final JRadioButton potenic = new JRadioButton("Potenic: " + Potenic_Version);
final JRadioButton Launcher = new JRadioButton("Launcher");
potenic.setBounds(675, 150, 200, 55);
Launcher.setBounds(675, 100, 200, 55);
ButtonGroup bg = new ButtonGroup();
bg.add(potenic);
bg.add(Launcher);
Launcher.setVisible(false);
ActionListener al = new ActionListener() {
public void actionPerformed(ActionEvent ae) {
if(potenic.isSelected()) {
game = 1;
Launcher.setVisible(true);
} else {
game = 0;
Launcher.setVisible(false);
}
}
};
potenic.addActionListener(al);
Launcher.addActionListener(al);
//Enslish
if(lang == 0) {
a JButton launchButton = new JButton("Play");
launchButton.setBounds(50, 530, 150, 55);
launchButton.setFont(font);
launchButton.setVisible(true);
launchButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(game == 1) {
Game.main(null);
frame.dispose();
}
}
});
frame.add(launchButton);
if(isUpdate == 0) {
JButton updateButton = new JButton("Check Update");
updateButton.setBounds(250, 530, 250, 55);
updateButton.setFont(font);
updateButton.setVisible(true);
updateButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
CheckUpdate frame2 = new CheckUpdate();
frame2.setVisible(true);
frame.dispose();
}
});
frame.add(updateButton);
}
else if(isUpdate == 1) {
JButton updateButton = new JButton("Update");
updateButton.setBounds(250, 530, 150, 55);
updateButton.setFont(font);
updateButton.setVisible(true);
updateButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Downloader frame2 = new Downloader();
frame2.setVisible(true);
frame.dispose();
}
});
frame.add(updateButton);
}
JButton OptionsButton = new JButton("Options");
OptionsButton.setBounds(550, 530, 150, 55);
OptionsButton.setFont(font);
OptionsButton.setVisible(true);
OptionsButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(game == 1) {
PotenicOptions frame4 = new PotenicOptions();
frame4.setVisible(true);
frame.dispose();
}
if(game == 0) {
Working frame3 = new Working();
frame3.setVisible(true);
frame.dispose();
}
}
});
frame.add(OptionsButton);
}
//Svenska
if(lang == 1) {
JButton launchButton = new JButton("Spela");
launchButton.setBounds(30, 530, 150, 55);
launchButton.setFont(font);
launchButton.setVisible(true);
launchButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(game == 1) {
Game.main(null);
frame.dispose();
}
}
});
frame.add(launchButton);
if(isUpdate == 0) {
JButton updateButton = new JButton("Titta Efter Uppdateringar");
updateButton.setBounds(215, 530, 350, 55);
updateButton.setFont(font);
updateButton.setVisible(true);
updateButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
CheckUpdate frame2 = new CheckUpdate();
frame2.setVisible(true);
frame.dispose();
}
});
frame.add(updateButton);
}
else if(isUpdate == 1) {
JButton updateButton = new JButton("Uppdatera");
updateButton.setBounds(250, 530, 150, 55);
updateButton.setFont(font);
updateButton.setVisible(true);
updateButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Downloader frame2 = new Downloader();
frame2.setVisible(true);
frame.dispose();
}
});
frame.add(updateButton);
}
JButton OptionsButton = new JButton("Inställningar");
OptionsButton.setBounds(600, 530, 200, 55);
OptionsButton.setFont(font);
OptionsButton.setVisible(true);
OptionsButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(game == 1) {
PotenicOptions frame4 = new PotenicOptions();
frame4.setVisible(true);
frame.dispose();
}
if(game == 0) {
LauncherOptions frame3 = new LauncherOptions();
frame3.setVisible(true);
frame.dispose();
}
}
});
frame.add(OptionsButton);
}
//Suomi
if(lang == 2) {
JButton launchButton = new JButton("Pelaa");
launchButton.setBounds(50, 530, 150, 55);
launchButton.setFont(font);
launchButton.setVisible(true);
launchButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(game == 1) {
Game.main(null);
frame.dispose();
}
}
});
frame.add(launchButton);
if(isUpdate == 0) {
JButton updateButton = new JButton("Tarkista päivitys");
updateButton.setBounds(250, 530, 250, 55);
updateButton.setFont(font);
updateButton.setVisible(true);
updateButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
CheckUpdate frame2 = new CheckUpdate();
frame2.setVisible(true);
frame.dispose();
}
});
frame.add(updateButton);
}
else if(isUpdate == 1) {
JButton updateButton = new JButton("päivittää");
updateButton.setBounds(250, 530, 150, 55);
updateButton.setFont(font);
updateButton.setVisible(true);
updateButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Downloader frame2 = new Downloader();
frame2.setVisible(true);
frame.dispose();
}
});
frame.add(updateButton);
}
JButton OptionsButton = new JButton("Asetukset");
OptionsButton.setBounds(550, 530, 150, 55);
OptionsButton.setFont(font);
OptionsButton.setVisible(true);
OptionsButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(game == 1) {
PotenicOptions frame4 = new PotenicOptions();
frame4.setVisible(true);
frame.dispose();
}
if(game == 0) {
LauncherOptions frame3 = new LauncherOptions();
frame3.setVisible(true);
frame.dispose();
}
}
});
frame.add(OptionsButton);
}
frame.add(scrollPane);
frame.add(potenic);
frame.add(Launcher);
frame.setVisible(true);
}
public static void main(String[] args) {
new LoadLang();
javax.swing.SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
createWindow();
}
});
}
}
The Option Code:
package ca.sidez.Launcher;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import ca.sidez.Main.Game;
import ca.sidez.Main.GamePanel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
public class Working extends JFrame {
private Game ga;
public static int game;
Font font;
Font font2;
public Working() {
final JFrame frame = new JFrame("Working In Progress");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(640, 480);
frame.setLocationRelativeTo(null);
frame.setLayout(new FlowLayout());
frame.setResizable(false);
font = new Font("Times New Roman", Font.PLAIN, 28);
font2 = new Font("Arial", Font.BOLD, 14);
JLabel working = new JLabel("Working In Progress");
frame.add(working);
//English
if(Main.lang == 0) {
JButton launchButton = new JButton("Back");
launchButton.setBounds(50, 300, 150, 55);
launchButton.setFont(font);
launchButton.setVisible(true);
launchButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame.dispose();
}
});
frame.add(launchButton);
}
//Svenska
if(Main.lang == 1) {
JButton launchButton = new JButton("Gå Tillbaks");
launchButton.setBounds(50, 300, 150, 55);
launchButton.setFont(font);
launchButton.setVisible(true);
launchButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
frame.add(launchButton);
}
//Suomi
if(Main.lang == 2) {
JButton launchButton = new JButton("Takaisin");
launchButton.setBounds(50, 300, 150, 55);
launchButton.setFont(font);
launchButton.setVisible(true);
launchButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
frame.add(launchButton);
}
frame.setVisible(true);
}
}
In your OptionsButton.addActionListener (pretty bad name for a variable, should not start with a capital letter), you create a Working object that IS a JFrame, and it's showed up.
But, in your Working class (realy bad name), that extends JFrame you work and show a final JFrame frame attribute.
That's why there is two windows (JFrame) showing up.
Delete that final JFrame frame in Working, and just use this !
I want to change the input in the textfield which will instantly update to the display, instead of press ENTER button to update it.
Here is my code.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MyProgram01 extends JFrame
{
private JTextField text1;
private JCheckBox check1;
private JCheckBox check2;
private String message;
private JLabel label1;
private JLabel label2;
private Font font;
public MyProgram01(String title)
{
super(title);
check1 = new JCheckBox("Bold");
check2 = new JCheckBox("Italics");
label1 = new JLabel("Text : ");
label2 = new JLabel("Style : ");
message = "Good Morning...";
text1 = new JTextField(message, 100);
font = new Font("Times New Roman", Font.PLAIN, 36);
setBounds(0, 0, 600, 300);
JPanel panel = new JPanel();
panel.setLayout(null);
panel.setBounds(0, 0, 600, 120);
panel.setBackground(Color.ORANGE);
label1.setFont(new Font("Times New Roman", Font.PLAIN, 36));
label1.setBounds(15, 15, 100, 36);
panel.add(label1);
text1.setBounds(120, 15, 400, 36);
panel.add(text1);
label2.setFont(new Font("Times New Roman", Font.PLAIN, 36));
label2.setBounds(15, 65, 100, 36);
panel.add(label2);
check1.setBounds(120, 65, 100, 36);
check2.setBounds(220, 65, 100, 36);
panel.add(check1);
panel.add(check2);
check1.addActionListener(new CheckBoxListener());
check2.addActionListener(new CheckBoxListener());
text1.addActionListener(new TextFieldListener());
setLayout(null);
add(panel);
}
public void paint(Graphics g)
{
super.paint(g);
g.setFont(font);
g.drawString(message, 15, 255);
}
private class CheckBoxListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(check1.isSelected() && check2.isSelected())
{
font = new Font("Times New Roman", Font.BOLD + Font.ITALIC, 36);
repaint();
}
else if(check1.isSelected())
{
font = new Font("Times New Roman", Font.BOLD, 36);
repaint();
}
else if(check2.isSelected())
{
font = new Font("Times New Roman", Font.ITALIC, 36);
repaint();
}
else
{
font = new Font("Times New Roman", Font.PLAIN, 36);
repaint();
}
}
}
private class TextFieldListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
message = text1.getText();
repaint();
}
}
public static void main(String[] args)
{
JFrame frame = new MyProgram01("My Program 01");
frame.setVisible(true);
frame.setResizable(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
How can I change the code to instantly update to the display?
EDIT :
It's work with the keyListener, but my program will only start display after second key is pressed.
For example, I key in abc, the program will start show a when I press b, and when I pressed c, it displays ab and c is missing, unless I press ENTER.
Here the part of the code :
text1.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
message = text1.getText();
repaint();
}
});
Add a KeyListener to your textfield.
You can do that like this:
textField.addKeyListener(new KeyAdapter(){
#Override
public void keyPressed(KeyEvent e){
message = textField.getText();
repaint();
}
});
OR
Add a DocumentListener to your textfield's Document.
You can do that like this:
private JFrame getFrame(){
return this;
}
...
textField.getDocument().addDocumentListener(new DocumentListener(){
#Override
public void insertUpdate(DocumentEvent e) {
message = textField.getText();
getFrame().repaint();
}
#Override
public void removeUpdate(DocumentEvent e) {
message = textField.getText();
getFrame().repaint();
}
#Override
public void changedUpdate(DocumentEvent e) {
// on change
}
});
Instead of using ActionListener for the class TextFieldListener, use KeyListener interface and use the keyTyped(KeyEvent e) method. When ever the event arises you can use getText() of texfield and repaint it.