Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

An assignment to show your unlimited creativity: create an application that cont

ID: 3628859 • Letter: A

Question

An assignment to show your unlimited creativity: create an application that contains at least one JPanel and at least one control element (button, slider, etc.). Create a picture on the JPanel, which at least contains a circle, a rectangle, and a line. The control element must allow some kind of interaction with the user.

Examples:

The picture contains a sunset scene - the position and color of the sun is determined by a slider, which represents the time from 5 to 9pm.
The picture contains the minimum: circle, rectangle and line. The sizes/colors/positions change randomly when a button is clicked.



Thank You

Explanation / Answer

import java.awt.*; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.*; class Drawing extends JFrame { JCheckBox jchkFilled; JRadioButton jrbLINE,jrbRECTANGLE; protected panel panel=new panel(); public static void main(String[] args) { Drawing frame=new Drawing(); frame.setTitle("JAVA PICTURE"); frame.setLocationRelativeTo(null); frame.setSize(500,300); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } public Drawing() { add(panel,BorderLayout.CENTER); JPanel jp=new JPanel(); jp.add(jrbLINE=new JRadioButton("Line")); jp.add(jrbRECTANGLE=new JRadioButton("Rectangle")); jp.add(jchkFilled=new JCheckBox("Filled with green")); jp.setLayout(new FlowLayout()); add(jp,BorderLayout.SOUTH); ButtonGroup group=new ButtonGroup(); group.add(jrbLINE); jrbLINE.setMnemonic('L'); group.add(jrbRECTANGLE); jrbRECTANGLE.setMnemonic('R'); jrbLINE.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { panel.setType(1); panel.setFilled(false); jchkFilled.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if(jchkFilled.isSelected()) panel.setFilled(true); else panel.setFilled(false); } }); } }); jrbRECTANGLE.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { panel.setType(2); panel.setFilled(false); jchkFilled.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if(jchkFilled.isSelected()) panel.setFilled(true); else panel.setFilled(false); } }); } }); } } class panel extends JPanel { public static final int LINE=1; public static final int RECTANGLE=2; private int type; private boolean filled =false; public panel() { } public panel(int type) { this.type=type; } public panel(int type,boolean filled) { this.type=type; this.filled=filled; } protected void paintComponent(Graphics g) { super.paintComponent(g); int width=getWidth(); int height=getHeight(); switch(type) { case LINE: g.setColor(Color.GREEN); if(filled) g.drawLine(10,10,width-10,height-10); else g.drawLine(10,10,width-10,height-10); break; case RECTANGLE: g.setColor(Color.GREEN); if(filled) g.fillRect((int)(0.1*width), (int)(0.1*height),(int)(0.8*width), (int)(0.8*height)); else g.drawRect((int)(0.1*width), (int)(0.1*height),(int)(0.8*width), (int)(0.8*height)); break; } } public void setType(int type) { this.type=type; repaint(); } public int getType() { return type; } public void setFilled(boolean filled) { this.filled=filled; repaint(); } public boolean isFilled() { return filled; } public Dimension getPreferredSize() { return new Dimension(80,80); } }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote