Using JAVA insert a background photo for this gameboard import java.awt.Color; i
ID: 3829680 • Letter: U
Question
Using JAVA insert a background photo for this gameboard
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionListener;
import java.util.Random;
/*
*
*/
public class Board extends JFrame implements ActionListener{
final int startingMoney = 0;
public final int WIDTH = 1000;
public final int HEIGHT = 1000;
private JTextArea theText;
public static int LINES = 5;
public static int CHAR_PER_LINE = 20;
double money = 1000;
Player a = new Player();
public String result;
Graphics canvas;
Random rand = new Random();
int n = rand.nextInt(5) + 1;
Player player= new Player();
public static void main(String[] args){
Board a = new Board();
//a.setVisible(true);
}
public Board(){
JFrame board = new JFrame();
board.setLayout(new GridLayout(15, 15, 10, 10));
////////////////////////////////////////////////////////////////////////////////////Buttons
JPanel p1 = new JPanel();
p1.setPreferredSize(new Dimension(50, 200));
Dimension d = new Dimension(110,50);
board.setBackground(Color.RED);
JButton blank1 = new JButton("");
p1.add(blank1);
blank1.setPreferredSize(d);
JButton blank2 = new JButton("");
p1.add(blank2);
blank2.setPreferredSize(d);
JButton blank3 = new JButton(" ");
p1.add(blank3);
blank3.setPreferredSize(d);
JButton blank4 = new JButton(" ");
p1.add(blank4);
blank4.setPreferredSize(d);
JButton blank5 = new JButton(" ");
p1.add(blank5);
blank5.setPreferredSize(d);
JButton blank6 = new JButton(" ");
p1.add(blank6);
blank6.setPreferredSize(d);
JButton party = new JButton ("");
p1.add(party);
party.addActionListener(this);
party.setPreferredSize(d);
JButton gown= new JButton ("");
p1.add(gown);
gown.addActionListener(this);
gown.setPreferredSize(d);
JButton applyGraduation = new JButton ("");
p1.add(applyGraduation);
applyGraduation.addActionListener(this);
applyGraduation.setPreferredSize(d);
JButton thesisPresent= new JButton ("");
p1.add(thesisPresent);
thesisPresent.addActionListener(this);
thesisPresent.setPreferredSize(d);
JButton thesisPrepare= new JButton ("");
p1.add(thesisPrepare);
thesisPrepare.addActionListener(this);
thesisPrepare.setPreferredSize(d);
JButton sickButton = new JButton ("");
p1.add(sickButton);
sickButton.addActionListener(this);
sickButton.setPreferredSize(d);
JButton stressButton = new JButton ("");
p1.add(stressButton);
stressButton.addActionListener(this);
stressButton.setPreferredSize(d);
JButton takeExam= new JButton ("");
p1.add(takeExam);
takeExam.addActionListener(this);
takeExam.setPreferredSize(d);
board.add(p1);
//p1.setVisible(true);
Explanation / Answer
Modified code:
Use your own image
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionListener;
import java.util.Random;
/*
*
*/
public class Board extends JFrame implements ActionListener{
final int startingMoney = 0;
public final int WIDTH = 1000;
public final int HEIGHT = 1000;
private JTextArea theText;
public static int LINES = 5;
public static int CHAR_PER_LINE = 20;
double money = 1000;
Player a = new Player();
public String result;
Graphics canvas;
Random rand = new Random();
int n = rand.nextInt(5) + 1;
Player player= new Player();
public BackgroundImageJFrame()
{
setTitle("Background Color for JFrame");
setSize(400,400);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
setLayout(new BorderLayout());
setContentPane(new JLabel(new ImageIcon("C:\Users\Computer\Downloads\color.png")));
setLayout(new FlowLayout());
setSize(399,399);
setSize(400,400);
}
public static void main(String[] args){
Board a = new Board();
//a.setVisible(true);
}
public Board(){
JFrame board = new JFrame();
board.setLayout(new GridLayout(15, 15, 10, 10));
////////////////////////////////////////////////////////////////////////////////////Buttons
JPanel p1 = new JPanel();
p1.setPreferredSize(new Dimension(50, 200));
Dimension d = new Dimension(110,50);
board.setBackground(Color.RED);
JButton blank1 = new JButton("");
p1.add(blank1);
blank1.setPreferredSize(d);
JButton blank2 = new JButton("");
p1.add(blank2);
blank2.setPreferredSize(d);
JButton blank3 = new JButton(" ");
p1.add(blank3);
blank3.setPreferredSize(d);
JButton blank4 = new JButton(" ");
p1.add(blank4);
blank4.setPreferredSize(d);
JButton blank5 = new JButton(" ");
p1.add(blank5);
blank5.setPreferredSize(d);
JButton blank6 = new JButton(" ");
p1.add(blank6);
blank6.setPreferredSize(d);
JButton party = new JButton ("");
p1.add(party);
party.addActionListener(this);
party.setPreferredSize(d);
JButton gown= new JButton ("");
p1.add(gown);
gown.addActionListener(this);
gown.setPreferredSize(d);
JButton applyGraduation = new JButton ("");
p1.add(applyGraduation);
applyGraduation.addActionListener(this);
applyGraduation.setPreferredSize(d);
JButton thesisPresent= new JButton ("");
p1.add(thesisPresent);
thesisPresent.addActionListener(this);
thesisPresent.setPreferredSize(d);
JButton thesisPrepare= new JButton ("");
p1.add(thesisPrepare);
thesisPrepare.addActionListener(this);
thesisPrepare.setPreferredSize(d);
JButton sickButton = new JButton ("");
p1.add(sickButton);
sickButton.addActionListener(this);
sickButton.setPreferredSize(d);
JButton stressButton = new JButton ("");
p1.add(stressButton);
stressButton.addActionListener(this);
stressButton.setPreferredSize(d);
JButton takeExam= new JButton ("");
p1.add(takeExam);
takeExam.addActionListener(this);
takeExam.setPreferredSize(d);
board.add(p1);
//p1.setVisible(true);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.