Professional Documents
Culture Documents
EX: 12 (A) Graphics Programming DATE: /11/2021
EX: 12 (A) Graphics Programming DATE: /11/2021
GRAPHICS PROGRAMMING
DATE: /11/2021
AIM:
To write a java program to change the color of background using a color palette.
ALGORITHM:
1. Start.
2. Create a JFrame.
3. Create components JButtons:3,Jpanel:1.
4. Add three colours to the buttons.
5. Add the panel and the three buttons to the JFrame.
6. Add action listener to the three buttons.
7. When you click the buttons the panel colour need to change according to the colour given in
the button.
8. Stop.
PROGRAM:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
JFrame jf;
JPanel jp;
JButton jb;
JButton jb2;
JButton jb3;
EX12(){
jb=new JButton();
jb.setBackground(Color.red);
jb2=new JButton();
jb2.setBackground(Color.blue);
jb3=new JButton();
jb3.setBackground(Color.green);
jp=new JPanel();
jf.setLayout(new FlowLayout());
jf.add(jp);
jp.add(jb);
jp.add(jb2);
jp.add(jb3);
jb.addActionListener(this);
jb2.addActionListener(this);
jb3.addActionListener(this);
jf.setVisible(true);
@Override
if(e.getSource()==jb){
jf.getContentPane().setBackground(Color.red);
}else if(e.getSource()==jb2){
jf.getContentPane().setBackground(Color.blue);
}else if(e.getSource()==jb3){
jf.getContentPane().setBackground(Color.green);
new EX12();
}
}
OUTPUT:
RESULT:
AIM:
To Create a simple Quiz application with 3 questions and their options. Display the score of
ALGORITHM:
1. Start.
2. Create two arrays having the questions and answers for quiz.
3. Create frame,label and radio buttons.
4. Set radio buttons for the answers.
5. Once first question is over,set the label with the next question.
6. And radio buttons with the answers for next question.
7. Once all questions are over reveal the score.
8. Stop.
PROGRAM:
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
JFrame jf;
JLabel jl1;
JLabel jl2;
JLabel jl3;
JButton jb1;
JButton jb2;
JButton jb3;
String questn[] = {"WHO WON ISL 20/21?", "WHO WON EURO 20/21?", "WHO WON WORLD CUP
2018?",""};
int score = 0;
int quesno = 0;
if (ans[quesno] == ch) {
score++;
update();
else {
score--;
update();
Quiz() {
jf.setSize(300, 300);
jf.setLayout(null);
jf.setSize(10, 20);
background.setBounds(400,10,598,396);
jl1.setBounds(600,450,100,40);
jl2.setBounds(600,500,500,40);
jb1.setBounds(300,600,200,40);
jb2.setBounds(600,600,200,40);
jb3.setBounds(900,600,300,40);
jf.setLayout(null);
jf.setBackground(Color.white);
jf.add(background);
jf.add(jl1);
jf.add(jl2);
jf.add(jb1);
jf.add(jb2);
jf.add(jb3);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jb1.addMouseListener(new MouseAdapter() {
checker(1);
});
jb2.addMouseListener(new MouseAdapter() {
});
jb3.addMouseListener(new MouseAdapter() {
checker(3);
});
jf.setVisible(true);
void update() {
if (quesno < 3) {
quesno++;
jl2.setText(questn[quesno]);
jb1.setText(cho1[quesno]);
jb2.setText(cho2[quesno]);
jb3.setText(cho3[quesno]);
if(quesno==3){
result.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
result.setSize(300,300);
result.setLayout(new FlowLayout(FlowLayout.CENTER));
result.add(finalScore);
result.setVisible(true);
OUTPUT:
RESULT: