Create a MessageFrame class the panel to the frame. The frame should be titled M
ID: 3753671 • Letter: C
Question
Create a MessageFrame class the panel to the frame. The frame should be titled Message in a Bottle and should end the program when the close box is clicked. In the panel, draw a bottle shape using some combination of Graphics object methods like drawLine, drawPolygon, drawPolyline, drawRondRect and drawArc (look up the Graphics object in the Java API for details). Make the bottle as attractive as you can although you won't be graded on artistic merit. In the center of the bottle shape, add a message string. Create a Message class with a main method that instantiates the MessageFrame the ball rolling extending JFrame and a MessagePanel class extending JPanel. Add and getsExplanation / Answer
import java.awt.*;
import javax.swing.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
class MyFrame extends JFrame
{
JPanel panel;
JLabel label;
// constructor 0f messageframe to JFrame
MyFrame( String"Message in a Bottle")
{
Frame( "message in a bottle" ); // invoking the JFrame constructor
setSize( 120, 80 );
setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); //to set default close operation of JFrame
setLayout( new FlowLayout() ); // set the layout manager
label = new JLabel("Message in a Bottle!"); // construct a JLabel
add( label ); // adding label to the JFrame
}
}
public class TestFrame2
{
public static void main ( String[] args )
{
MyFrame frame = new MyFrame("Message in a Bottle"); // construct a MyFrame object
frame.setVisible( true ); //to make it visible
public void paint ( Graphics g )
{
// draw a bottle shape at location x=50 y=100
g.drawRoundRect("myFrame object", 20, 60 );
frame.setSize( 50, 100 ); // set it to 150 wide by 100 high
frame.setVisible( true ); //to make it visible
}
}
//using graphic objects
public class TestMainGUI
{
private JFrame frame;
private JPanel workingDefaultPanel; //defining working panel
private TestJPanel notWorkingExtendingJPanel;
public static void main(String[] args)
{
public void run()
{
try
{
TestMainGUI window = new TestMainGUI();
window.frame.setVisible(true); //to set the frame visible
}
catch (Exception e)
{
e.printStackTrace();
}});
//to make the bottle more attractive
workingDefaultPanel = new JPanel();
bottleborder.addTab("Working Panel", null, workingDefaultPanel, null);//borders on bottle
drawRoundRectLayout_WorkingDefaultPanel = newRoundRectLayout();
drawRoundRectLayout_WorkingDefaultPanel.columnWidths = new int[]{0, 0};//initialising the bottle length
drawRoundRectLayout_WorkingDefaultPanel.rowHeights = new int[]{0, 0};
drawRoundRectLayout_WorkingDefaultPanel.columnWeights = new double[]{1.0, Double.MIN_VALUE};
drawRoundRectLayout_WorkingDefaultPanel.rowWeights = new double[]{1.0, Double.MIN_VALUE};
workingDefaultPanel.setLayout(gbl_WorkingDefaultPanel);
public class TestJPanel extends JPanel
{
private JScrollPane notWorkingJScrollPane;
private JTextArea notWorkingJTextArea; //declaring the textarea of the panel
public TestJPanel()
{
super("Message in a Bottle");
WorkingJScrollPane = new JScrollPane();
WorkingJScrollPane.setViewportView(notWorkingJTextArea);
}
protected JScrollPane getNotWorkingJScrollPane()
{
return notWorkingJScrollPane;
getClipBounds(Rectangle r)
getClipRect() //declaring the bottle size drawroundrect
}
protected JTextArea getNotWorkingJTextArea()
{
return notWorkingJTextArea;
//message class with main method create instantates the message frame and get the ball rolling
public class message
{
public static void main(string args[]){
system.out.println("message in a bottle");
//rolling the bottle
public drawRoundRect(Point p, int w, int h) {
origin = p;
width = w;
height = h;
// a method for computing the area of the bottle
public int getArea() {
return width * height;
}
public void draw(Graphics page) { //an image to draw
super.paintComponent (page);
image.paintIcon (new ReboundPanel(), page, x, y);
}
private class ReboundListener implements ActionListener {
public void actionPerformed (ActionEvent event) { //action defined here is rolling the bottle
x += moveX;
y += moveY; //to roll the bottle
if (x <= 0 || x >= WIDTH-IMAGE_SIZE)
moveX = moveX * -1;
if (y <= 0 || x >= WIDTH-IMAGE_SIZE)
moveY = moveY * -1;
get.bottlerolled();//action performed
close(); //end the program when the closebox is clicked
}
}}}
Hope you understand and this helps you.All the best