Java GUI: Developing a Dartboard Applet and Smiley Tennis Ball Design

Verified

Added on  2023/04/21

|3
|579
|419
Practical Assignment
AI Summary
This assignment showcases Java code for creating a dartboard applet and a smiley tennis ball graphic using Java Swing. The dartboard applet allows customization of the diameter and number of lines, drawing concentric circles and radial lines. The smiley tennis ball graphic demonstrates drawing basic shapes like circles, ovals, and lines to create a simple face on the ball. The code provides examples of using Graphics class methods such as drawOval, drawLine, fillOval, and fill3DRect for GUI design in Java. Desklib offers this assignment as a resource for students, alongside a wide range of past papers and solved assignments.
Document Page
Part 1
import java.awt.Graphics;
import javax.swing.JApplet;
public class Dartboard extends JApplet {
int Diameter;
int NumberPoints;
int inputlines;
double angle;
public void init() {
final int input = Integer.parseInt(getParameter("Width"));
final int Lines = Integer.parseInt(getParameter("Lines"));
Diameter = input;
inputlines = Lines;
}
// draw shapes on applet's background
public void paint(Graphics g) {
super.paint(g); // call paint method inherited from JApplet
g.drawOval(0, 0,
Diameter, Diameter);
g.drawOval(((Diameter / 2) / 3), ((Diameter / 2) / 3),
((Diameter / 3) * 2), ((Diameter / 3) * 2));
// done processing case
g.drawOval((((Diameter / 2) / 3) * 2), (((Diameter / 2) / 3) * 2),
(Diameter / 3), (Diameter / 3));
//check if the number of passed lines is within the bounds of the
requirements
if (inputlines < 2 || inputlines > 30) {
NumberPoints = 20;
} else {
NumberPoints = inputlines;
}
//find the angle that is to be used for the lines
angle = 360 / (double) NumberPoints;
int i; //for loop statement
int x; // X coordinate
int y; //y coordinate
for (i = 0; i < NumberPoints; i++) {
// find line angle in radians
double w = (angle * i) * Math.PI / 180;
x = (int) ((Diameter / 2) * Math.cos(w));
tabler-icon-diamond-filled.svg

Secure Best Marks with AI Grader

Need help grading? Try our AI Grader for instant feedback on your assignments.
Document Page
y = (int) ((Diameter / 2) * Math.sin(w));
g.drawLine(x + (Diameter / 2), y + (Diameter / 2), (Diameter /
2), (Diameter / 2));
}
}
}
Design
Smiley Tennis Ball at the Ireland v Georgia game.
Code:
import java.awt.*;
import java.awt.Event.*;
import javax.swing.*;
public class Flag extends JPanel {
public static void main(String[] args){
JFrame f = new JFrame("Flag");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Flag fl = new Flag();
fl.setBackground(Color.WHITE);
f.add(fl);
f.setSize(500, 270);
f.setVisible(true);
}
public void paintComponent(Graphics g){
super.paintComponent(g);
this.setBackground(Color.WHITE);
Document Page
g.setColor(Color.GREEN);
g.fill3DRect(10, 10, 100, 50, true);
g.setColor(Color.WHITE);
g.fill3DRect(110, 10, 100, 50, true);
g.setColor(Color.ORANGE);
g.fill3DRect(210, 10, 100, 50, true);
g.setColor(Color.YELLOW);
g.fillOval(100,100,100,100);
g.setColor(Color.BLACK);
g.fillOval(120,125,20,20);
g.fillOval(160,125,20,20);
g.drawLine(150,165,150,150);
g.drawArc(110,106,80,90,-30,-120);
}
}
Screenshot
chevron_up_icon
1 out of 3
circle_padding
hide_on_mobile
zoom_out_icon
logo.png

Your All-in-One AI-Powered Toolkit for Academic Success.

Available 24*7 on WhatsApp / Email

[object Object]