JAVA. I cannot get my program to work. Add a JList of eight items so the user ca
ID: 3667901 • Letter: J
Question
JAVA. I cannot get my program to work.
Add a JList of eight items so the user can change the background color of the applet.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class DrawBG extends JApplet implements ListSelectionListener, ActionListener
{
//variable for JList
private JList ListBG;
//string array
private String[] ColorBG = { "Black", "Blue", "Cyan", "Gray", "Green",
"Orange", "Red", "Yellow"};
//color array
private Color[] SelColor = {Color.black, Color.blue, Color.cyan, Color.gray,
Color.green, Color.orange, Color.red, Color.yellow};
//declare JScrollPane variable
private JScrollPane SelectC;
//declare current color
private Color CurColor = Color.gray;
//declare NumTF
private JTextField NumTF;
//declare label variable
private JLabel Label;
//ok butten
private JButton OKBtn;
//declare num var
private int Num = 0;
//init method
public void init()
{
//container
Container C = getContentPane();
setSize(500, 500);
Label = new JLabel("Enter number");
Label.setLocation(200, 40);
Label.setSize(100, 30);
NumTF = new JTextField(1);
NumTF.setLocation(300, 40);
NumTF.setSize(100, 30);
OKBtn = new JButton("OK");
OKBtn.setLocation(400, 40);
OKBtn.setSize(50, 30);
OKBtn.addActionListener(this);
C.setLayout(null);
C.add(Label);
C.add(NumTF);
C.add(OKBtn);
ListBG = new JList(ColorBG);
ListBG.setVisibleRowCount(3);
ListBG.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
ListBG.addListSelectionListener(this);
SelectC = new JScrollPane(ListBG);
SelectC.setSize(100, 60);
SelectC.setLocation(200, 350);
C.add(SelectC);
}
//paint method
public void paint(Graphics g)
{
super.paint(g);
g.setColor(Color.orange);
g.setColor(CurColor);
switch(Num)
{
case 1: g.fillRect(20, 20, 20, 100);
break;
case 2: g.fillRect(20, 40, 80, 20);
g.fillRect(80, 40, 20, 40);
g.fillRect(20, 80, 80, 20);
g.fillRect(20, 80, 20, 50);
g.fillRect(20, 120, 80, 20);
break;
case 3: g.fillRect(20, 40, 80, 20);
g.fillRect(80, 40, 20, 40);
g.fillRect(20, 80, 80, 20);
g.fillRect(80, 80, 20, 50);
g.fillRect(20, 120, 80, 20);
break;
case 4: g.fillRect(20, 20, 20, 60);
g.fillRect(40, 60, 40, 20);
g.fillRect(80, 20, 20, 100);
break;
case 5: g.fillRect(20, 40, 80, 20);
g.fillRect(20, 40, 20, 40);
g.fillRect(20, 80, 80, 20);
g.fillRect(80, 80, 20, 50);
g.fillRect(20, 120, 80, 20);
break;
case 6: g.fillRect(20, 20, 20, 100);
g.fillRect(40, 20, 40, 20);
g.fillRect(40, 60, 20, 20);
g.fillRect(40, 100, 20, 20);
g.fillRect(60, 60, 20, 60);
break;
case 7: g.fillRect(40, 20, 30, 20);
g.fillRect(70, 20, 20, 80);
break;
case 8: g.fillRect(20, 20, 20, 100);
g.fillRect(40, 20, 40, 20);
g.fillRect(40, 60, 20, 20);
g.fillRect(40, 100, 20, 20);
g.fillRect(60, 40, 20, 80);
break;
case 9: g.fillRect(20, 20, 20, 60);
g.fillRect(40, 20, 40, 20);
g.fillRect(40, 60, 20, 20);
g.fillRect(40, 100, 20, 20);
g.fillRect(60, 40, 20, 80);
break;
case 0: g.fillRect(20, 20, 29, 100);
g.fillRect(40, 20, 60, 20);
g.fillRect(40, 100, 60, 20);
g.fillRect(100, 20, 20, 100);
break;
}
}
//action event method
public void ValChange(ListSelectionEvent e)
{
CurColor = SelColor[ListBG.getSelectedIndex()];
repaint();
}
public void ActPerform(ActionEvent e)
{
if(e.getActionCommand().equals("OK"))
Num = Integer.parseInt(NumTF.getText());
repaint();
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
@Override
public void valueChanged(ListSelectionEvent e) {
// TODO Auto-generated method stub
}
}
Explanation / Answer
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication19;
/**
*
* @author HARE KRISHNA
*/
import java.awt.*
import java.awt.event.*
import javax.swing.*
import javax.swing.event.*
public class DrawBG extends JApplet implements ListSelectionListener,ActionListener
{
//variable for JList
private JList ListBG
//string array
private String[] ColorBG = { "Black", "Blue", "Cyan", "Gray", "Green","Orange", "Red", "Yellow"}
//color array
private Color[] SelColor = {Color.black, Color.blue, Color.cyan, Color.gray,Color.green, Color.orange, Color.red, Color.yellow}
//declare JScrollPane variable
private JScrollPane SelectC
//declare current color
private Color CurColor = Color.gray
//declare NumTF
private JTextField NumTF
//declare label variable
private JLabel Label
//ok butten
private JButton OKBtn
//declare num var
private int Num = 0
//init method
//Added Colour List
private JList<String> colourList;
//create the model and add elements
DefaultListModel<String> listModel = new DefaultListModel<>();
listModel.addElement("Blue");
listModel.addElement("Black");
listModel.addElement("Cyan");
listModel.addElement("Gray");
listModel.addElement("Green");
listModel.addElement("Orange");
listModel.addElement("Red");
listModel.addElement("Yellow");
//create the list
colourList = new JList<>(listModel);
add(colourList);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("JList Example");
this.setSize(200,200);
this.setLocationRelativeTo(null);
this.setVisible(true);
public void init()
{
//container
Container C = getContentPane()
setSize(500, 500)
Label = new JLabel("Enter number")
Label.setLocation(200, 40)
Label.setSize(100, 30)
NumTF = new JTextField(1)
NumTF.setLocation(300, 40)
NumTF.setSize(100, 30)
OKBtn = new JButton("OK")
OKBtn.setLocation(400, 40)
OKBtn.setSize(50, 30)
OKBtn.addActionListener(this)
C.setLayout(null)
C.add(Label)
C.add(NumTF)
C.add(NumTF)
C.add(OKBtn)
ListBG = new JList(ColorBG)
ListBG.setVisibleRowCount(3)
ListBG.setSelectionMode(ListSelectionModel.SINGLE_SELECTION)
ListBG.addListSelectionListener(this)
SelectC = new JScrollPane(ListBG)
SelectC.setSize(100, 60)
SelectC.setLocation(200, 350)
C.add(SelectC)
}
//paint method
public void paint(Graphics g)
{
super.paint(g)
g.setColor(Color.orange)
g.setColor(CurColor)
switch(Num)
{
case 1: g.fillRect(20, 20, 20, 100)
break
case 2: g.fillRect(20, 40, 80, 20)
g.fillRect(80, 40, 20, 40)
g.fillRect(20, 80, 80, 20)
g.fillRect(20, 80, 20, 50)
g.fillRect(20, 120, 80, 20)
break
case 3: g.fillRect(20, 40, 80, 20)
g.fillRect(80, 40, 20, 40)
g.fillRect(20, 80, 80, 20)
g.fillRect(80, 80, 20, 50)
g.fillRect(20, 120, 80, 20)
break
case 4: g.fillRect(20, 20, 20, 60)
g.fillRect(40, 60, 40, 20)
g.fillRect(80, 20, 20, 100)
break
case 5: g.fillRect(20, 40, 80, 20)
g.fillRect(20, 40, 20, 40)
g.fillRect(20, 80, 80, 20)
g.fillRect(80, 80, 20, 50)
g.fillRect(20, 120, 80, 20)
break
case 6: g.fillRect(20, 20, 20, 100)
g.fillRect(40, 20, 40, 20)
g.fillRect(40, 60, 20, 20)
g.fillRect(40, 100, 20, 20)
g.fillRect(60, 60, 20, 60)
break
case 7: g.fillRect(40, 20, 30, 20)
g.fillRect(70, 20, 20, 80)
break
case 8: g.fillRect(20, 20, 20, 100)
g.fillRect(40, 20, 40, 20)
g.fillRect(40, 60, 20, 20)
g.fillRect(40, 100, 20, 20)
g.fillRect(60, 40, 20, 80)
break
case 9: g.fillRect(20, 20, 20, 60)
g.fillRect(40, 20, 40, 20)
g.fillRect(40, 60, 20, 20)
g.fillRect(40, 100, 20, 20)
g.fillRect(40, 100, 20, 20)
g.fillRect(60, 40, 20, 80)
break
case 0: g.fillRect(20, 20, 29, 100)
g.fillRect(40, 20, 60, 20)
g.fillRect(40, 100, 60, 20)
g.fillRect(100, 20, 20, 100)
break
}
}
//action event method
public void ValChange(ListSelectionEvent e)
{
CurColor = SelColor[ListBG.getSelectedIndex()]
repaint()
}
public void ActPerform(ActionEvent e)
{
if(e.getActionCommand().equals("OK"))
Num = Integer.parseInt(NumTF.getText())
repaint()
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Autogenerated method stub
}
@Override
public void valueChanged(ListSelectionEvent e) {
// TODO Autogenerated method stub
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.