You are on page 1of 8

EX: 12(a)

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;

public class EX12 implements 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);

jf=new JFrame("COLOR CHANGER");

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

public void actionPerformed(ActionEvent e) {

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);

public static void main(String[] args) {

new EX12();

}
}

OUTPUT:

RESULT:

The Java program is executed and the expected output is obtained.


EX: 12(b)
GRAPHICS PROGRAMMING
DATE: /11/2021

AIM:

To Create a simple Quiz application with 3 questions and their options. Display the score of

the quiz in a dialog box.

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;

public class Quiz extends MouseAdapter {

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?",""};

String cho1[] = {"MUMBAI CITY", "PORTUGAL", "FRANCE",""};

String cho2[] = {"ATK MB", "ITALY", "GERMANY",""};

String cho3[] = {"CHENNAYIN FC", "SPAIN", "SPAIN",""};

int ans[] = {1, 2, 1};

int score = 0;

int quesno = 0;

void checker(int ch) {

if (ans[quesno] == ch) {

score++;

update();

else {

score--;

update();

Quiz() {

jf = new JFrame("FOOTY Quiz");

jf.setSize(300, 300);

JLabel background=new JLabel(new ImageIcon


("C:\\Users\\Rohan\\Pictures\\flutter project images\\google-quiz.jpg"));

jf.setLayout(null);

jl1 = new JLabel("FOOTBALL QUIZ");

jf.setSize(10, 20);

jl2 = new JLabel(questn[quesno]);

jb1 = new JButton(cho1[quesno]);

jb2 = new JButton(cho2[quesno]);

jb3 = new JButton(cho3[quesno]);

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() {

public void mouseClicked(MouseEvent e) {

checker(1);

});

jb2.addMouseListener(new MouseAdapter() {

public void mouseClicked(MouseEvent e) {


checker(2);

});

jb3.addMouseListener(new MouseAdapter() {

public void mouseClicked(MouseEvent e) {

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){

JFrame result=new JFrame("RESULT");

JLabel finalScore=new JLabel("Your Score:"+score);

result.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

result.setSize(300,300);

result.setLayout(new FlowLayout(FlowLayout.CENTER));

result.add(finalScore);

result.setVisible(true);

public static void main(String[] args) {


new Quiz();

OUTPUT:

RESULT:

The Java program is executed and the expected output is obtained.

You might also like