SlideShare a Scribd company logo
1 of 1
Download to read offline
I need help creating a java gui that draws lines, shapes, characters in response to mouseDragged,
mousePressed, keyTyped. The user should be able to control the size using a Jslider, and control
the color of all the shapes, lines, and letters using JcolorChooser and be able to control the Font
of the letters.
Solution
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class DPanel
extends JFrame { private DObjects panel = new DObjects(); private JPanel BPanel = new
JPanel(); private JFrame window = new JFrame(); DPanel(){ buildGUI(); }
void buildGUI(){ window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLayout(new GridLayout(2,2)); window.add(panel); window.add(BPanel);
BPanel.setBackground(Color.blue); //define buttons and add to panel JButton rect
= new JButton("Rect"); JButton oval = new JButton("Oval"); BPanel.add(rect);
BPanel.add(oval); rect.addActionListener(new ActionListener() { public void
actionPerformed(ActionEvent e) { panel.setType(1); } });
oval.addActionListener(new ActionListener() { public void
actionPerformed(ActionEvent e) { panel.setType(2); } });
window.setVisible(true); window.setSize(1024, 800); } public static void
main(String[] args) { new DPanel(); } } import java.awt.*; import
java.awt.event.*; import javax.swing.*; class DObjects extends JPanel { public int
x1,x2,y1,y2; public int type = 1; public DObjects() { init(); } public void init(){
setBackground(Color.WHITE); addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent m) { x1 = m.getX(); y1 =
m.getY(); repaint(); } public void mouseReleased(MouseEvent m)
{ x2 = m.getX(); y2 = m.getY(); repaint(); } });
addMouseMotionListener(new MouseMotionAdapter() { public void
mouseDragged(MouseEvent m) { x2 = m.getX(); y2 =
m.getY(); repaint(); } }); } public void setType(int arg){
if(arg == 1){ type = 1; }else if(arg == 2){ type = 2; } } public void
paintComponent(Graphics g) { super.paintComponent(g); if(type == 1) {
g.drawRect(x1,y1,x2,y2); } else if (type == 2) {
g.drawOval(x1,y1,x2,y2); } } }

More Related Content

Similar to I need help creating a java gui that draws lines, shapes, characters.pdf

Task Write a Java program to implement a simple graphics editor tha.pdf
Task Write a Java program to implement a simple graphics editor tha.pdfTask Write a Java program to implement a simple graphics editor tha.pdf
Task Write a Java program to implement a simple graphics editor tha.pdf
cronkwurphyb44502
 
Md10 building java gu is
Md10 building java gu isMd10 building java gu is
Md10 building java gu is
Rakesh Madugula
 
Whenever I run my application my Game appears with the pict.pdf
Whenever I run my application my Game appears with the pict.pdfWhenever I run my application my Game appears with the pict.pdf
Whenever I run my application my Game appears with the pict.pdf
aarthitimesgd
 
step by step to write a gnome-shell extension
step by step to write a gnome-shell extension step by step to write a gnome-shell extension
step by step to write a gnome-shell extension
Yuren Ju
 
Write a program whose source file is named UnitProgl.java that do.pdf
Write a program whose source file is named UnitProgl.java that do.pdfWrite a program whose source file is named UnitProgl.java that do.pdf
Write a program whose source file is named UnitProgl.java that do.pdf
arihantcomputersddn
 
Graphical User Interface (GUI) - 1
Graphical User Interface (GUI) - 1Graphical User Interface (GUI) - 1
Graphical User Interface (GUI) - 1
PRN USM
 
Throughout the semester, we have been working on command line applic.pdf
Throughout the semester, we have been working on command line applic.pdfThroughout the semester, we have been working on command line applic.pdf
Throughout the semester, we have been working on command line applic.pdf
birajdar2
 
package buttongui; import static com.sun.deploy.config.JREInf.pdf
package buttongui; import static com.sun.deploy.config.JREInf.pdfpackage buttongui; import static com.sun.deploy.config.JREInf.pdf
package buttongui; import static com.sun.deploy.config.JREInf.pdf
arjuntiwari586
 
Working with Layout Managers. Notes 1. In part 2, note that the Gam.pdf
Working with Layout Managers. Notes 1. In part 2, note that the Gam.pdfWorking with Layout Managers. Notes 1. In part 2, note that the Gam.pdf
Working with Layout Managers. Notes 1. In part 2, note that the Gam.pdf
udit652068
 
correct the error and add code same in the pic import jav.pdf
correct the error and add code same in the pic   import jav.pdfcorrect the error and add code same in the pic   import jav.pdf
correct the error and add code same in the pic import jav.pdf
devangmittal4
 
Z blue introduction to gui (39023299)
Z blue   introduction to gui (39023299)Z blue   introduction to gui (39023299)
Z blue introduction to gui (39023299)
Narayana Swamy
 

Similar to I need help creating a java gui that draws lines, shapes, characters.pdf (20)

Task Write a Java program to implement a simple graphics editor tha.pdf
Task Write a Java program to implement a simple graphics editor tha.pdfTask Write a Java program to implement a simple graphics editor tha.pdf
Task Write a Java program to implement a simple graphics editor tha.pdf
 
Graphical User Components Part 2
Graphical User Components Part 2Graphical User Components Part 2
Graphical User Components Part 2
 
Chap1 1 1
Chap1 1 1Chap1 1 1
Chap1 1 1
 
Chap1 1.1
Chap1 1.1Chap1 1.1
Chap1 1.1
 
Md10 building java gu is
Md10 building java gu isMd10 building java gu is
Md10 building java gu is
 
Whenever I run my application my Game appears with the pict.pdf
Whenever I run my application my Game appears with the pict.pdfWhenever I run my application my Game appears with the pict.pdf
Whenever I run my application my Game appears with the pict.pdf
 
step by step to write a gnome-shell extension
step by step to write a gnome-shell extension step by step to write a gnome-shell extension
step by step to write a gnome-shell extension
 
Write a program whose source file is named UnitProgl.java that do.pdf
Write a program whose source file is named UnitProgl.java that do.pdfWrite a program whose source file is named UnitProgl.java that do.pdf
Write a program whose source file is named UnitProgl.java that do.pdf
 
Java awt
Java awtJava awt
Java awt
 
Graphical User Interface (GUI) - 1
Graphical User Interface (GUI) - 1Graphical User Interface (GUI) - 1
Graphical User Interface (GUI) - 1
 
L11cs2110sp13
L11cs2110sp13L11cs2110sp13
L11cs2110sp13
 
Throughout the semester, we have been working on command line applic.pdf
Throughout the semester, we have been working on command line applic.pdfThroughout the semester, we have been working on command line applic.pdf
Throughout the semester, we have been working on command line applic.pdf
 
explain how 2D drawing is done in Java using Swing- Have you done this.docx
explain how 2D drawing is done in Java using Swing- Have you done this.docxexplain how 2D drawing is done in Java using Swing- Have you done this.docx
explain how 2D drawing is done in Java using Swing- Have you done this.docx
 
package buttongui; import static com.sun.deploy.config.JREInf.pdf
package buttongui; import static com.sun.deploy.config.JREInf.pdfpackage buttongui; import static com.sun.deploy.config.JREInf.pdf
package buttongui; import static com.sun.deploy.config.JREInf.pdf
 
java2 swing
java2 swingjava2 swing
java2 swing
 
Working with Layout Managers. Notes 1. In part 2, note that the Gam.pdf
Working with Layout Managers. Notes 1. In part 2, note that the Gam.pdfWorking with Layout Managers. Notes 1. In part 2, note that the Gam.pdf
Working with Layout Managers. Notes 1. In part 2, note that the Gam.pdf
 
14a-gui.ppt
14a-gui.ppt14a-gui.ppt
14a-gui.ppt
 
correct the error and add code same in the pic import jav.pdf
correct the error and add code same in the pic   import jav.pdfcorrect the error and add code same in the pic   import jav.pdf
correct the error and add code same in the pic import jav.pdf
 
Z blue introduction to gui (39023299)
Z blue   introduction to gui (39023299)Z blue   introduction to gui (39023299)
Z blue introduction to gui (39023299)
 
CORE JAVA-2
CORE JAVA-2CORE JAVA-2
CORE JAVA-2
 

More from ezhilvizhiyan

Hi please complete the following with detailed working out Find the .pdf
Hi please complete the following with detailed working out Find the .pdfHi please complete the following with detailed working out Find the .pdf
Hi please complete the following with detailed working out Find the .pdf
ezhilvizhiyan
 
Explain and discuss 4G wireless communications and their advantages..pdf
Explain and discuss 4G wireless communications and their advantages..pdfExplain and discuss 4G wireless communications and their advantages..pdf
Explain and discuss 4G wireless communications and their advantages..pdf
ezhilvizhiyan
 
Draw and describe a module of the cross-cultural communication pr.pdf
Draw and describe a module of the cross-cultural communication pr.pdfDraw and describe a module of the cross-cultural communication pr.pdf
Draw and describe a module of the cross-cultural communication pr.pdf
ezhilvizhiyan
 
Describe the original purpose of the Clean Air Act Policy of 1963. E.pdf
Describe the original purpose of the Clean Air Act Policy of 1963. E.pdfDescribe the original purpose of the Clean Air Act Policy of 1963. E.pdf
Describe the original purpose of the Clean Air Act Policy of 1963. E.pdf
ezhilvizhiyan
 
7. In many respects metal binding is similar to the binding of a prot.pdf
7. In many respects metal binding is similar to the binding of a prot.pdf7. In many respects metal binding is similar to the binding of a prot.pdf
7. In many respects metal binding is similar to the binding of a prot.pdf
ezhilvizhiyan
 
3. Photosynthetic organisms produce about 300 x 1015 g of oxygen per .pdf
3. Photosynthetic organisms produce about 300 x 1015 g of oxygen per .pdf3. Photosynthetic organisms produce about 300 x 1015 g of oxygen per .pdf
3. Photosynthetic organisms produce about 300 x 1015 g of oxygen per .pdf
ezhilvizhiyan
 
This project will implement a simple usernamepassword lookup system.pdf
This project will implement a simple usernamepassword lookup system.pdfThis project will implement a simple usernamepassword lookup system.pdf
This project will implement a simple usernamepassword lookup system.pdf
ezhilvizhiyan
 
The following code is based on the Josephus problem, the code does c.pdf
The following code is based on the Josephus problem, the code does c.pdfThe following code is based on the Josephus problem, the code does c.pdf
The following code is based on the Josephus problem, the code does c.pdf
ezhilvizhiyan
 
Tech transfers should the fed Gov’t keep subsidizing University .pdf
Tech transfers should the fed Gov’t keep subsidizing University .pdfTech transfers should the fed Gov’t keep subsidizing University .pdf
Tech transfers should the fed Gov’t keep subsidizing University .pdf
ezhilvizhiyan
 
Remaining Time 31 minutes, 22 seconds QUESTION 1 Question Completion.pdf
Remaining Time 31 minutes, 22 seconds QUESTION 1 Question Completion.pdfRemaining Time 31 minutes, 22 seconds QUESTION 1 Question Completion.pdf
Remaining Time 31 minutes, 22 seconds QUESTION 1 Question Completion.pdf
ezhilvizhiyan
 

More from ezhilvizhiyan (20)

Hi please complete the following with detailed working out Find the .pdf
Hi please complete the following with detailed working out Find the .pdfHi please complete the following with detailed working out Find the .pdf
Hi please complete the following with detailed working out Find the .pdf
 
Explain and discuss 4G wireless communications and their advantages..pdf
Explain and discuss 4G wireless communications and their advantages..pdfExplain and discuss 4G wireless communications and their advantages..pdf
Explain and discuss 4G wireless communications and their advantages..pdf
 
Draw and describe a module of the cross-cultural communication pr.pdf
Draw and describe a module of the cross-cultural communication pr.pdfDraw and describe a module of the cross-cultural communication pr.pdf
Draw and describe a module of the cross-cultural communication pr.pdf
 
Discuss the reasons why visions fail. What steps can be implemented .pdf
Discuss the reasons why visions fail. What steps can be implemented .pdfDiscuss the reasons why visions fail. What steps can be implemented .pdf
Discuss the reasons why visions fail. What steps can be implemented .pdf
 
Describe the original purpose of the Clean Air Act Policy of 1963. E.pdf
Describe the original purpose of the Clean Air Act Policy of 1963. E.pdfDescribe the original purpose of the Clean Air Act Policy of 1963. E.pdf
Describe the original purpose of the Clean Air Act Policy of 1963. E.pdf
 
Continuity 100 Let f be a continuous function on a metric space X. L.pdf
Continuity 100 Let f be a continuous function on a metric space X. L.pdfContinuity 100 Let f be a continuous function on a metric space X. L.pdf
Continuity 100 Let f be a continuous function on a metric space X. L.pdf
 
Admitting New Partner With Bonus Cody Jenkins and Lacey Tanner formed.pdf
Admitting New Partner With Bonus Cody Jenkins and Lacey Tanner formed.pdfAdmitting New Partner With Bonus Cody Jenkins and Lacey Tanner formed.pdf
Admitting New Partner With Bonus Cody Jenkins and Lacey Tanner formed.pdf
 
7. In many respects metal binding is similar to the binding of a prot.pdf
7. In many respects metal binding is similar to the binding of a prot.pdf7. In many respects metal binding is similar to the binding of a prot.pdf
7. In many respects metal binding is similar to the binding of a prot.pdf
 
3. Photosynthetic organisms produce about 300 x 1015 g of oxygen per .pdf
3. Photosynthetic organisms produce about 300 x 1015 g of oxygen per .pdf3. Photosynthetic organisms produce about 300 x 1015 g of oxygen per .pdf
3. Photosynthetic organisms produce about 300 x 1015 g of oxygen per .pdf
 
1. Which is less soluble in water, 1-pentanol or 1-heptanol Explain..pdf
1. Which is less soluble in water, 1-pentanol or 1-heptanol Explain..pdf1. Which is less soluble in water, 1-pentanol or 1-heptanol Explain..pdf
1. Which is less soluble in water, 1-pentanol or 1-heptanol Explain..pdf
 
X Company has the following budgeted cash flows for JanuaryIf the .pdf
X Company has the following budgeted cash flows for JanuaryIf the .pdfX Company has the following budgeted cash flows for JanuaryIf the .pdf
X Company has the following budgeted cash flows for JanuaryIf the .pdf
 
Why did animal phyla appear so suddenly during the Cambrian explosion.pdf
Why did animal phyla appear so suddenly during the Cambrian explosion.pdfWhy did animal phyla appear so suddenly during the Cambrian explosion.pdf
Why did animal phyla appear so suddenly during the Cambrian explosion.pdf
 
Which of the following information-management systems uses artificia.pdf
Which of the following information-management systems uses artificia.pdfWhich of the following information-management systems uses artificia.pdf
Which of the following information-management systems uses artificia.pdf
 
Which of the following was part of the reason for the European excha.pdf
Which of the following was part of the reason for the European excha.pdfWhich of the following was part of the reason for the European excha.pdf
Which of the following was part of the reason for the European excha.pdf
 
Ture or false or uncertain ( explain why) Thank you! e. Output per c.pdf
Ture or false or uncertain ( explain why) Thank you! e. Output per c.pdfTure or false or uncertain ( explain why) Thank you! e. Output per c.pdf
Ture or false or uncertain ( explain why) Thank you! e. Output per c.pdf
 
This is for a C programDene a Car structure type in your header le.pdf
This is for a C programDene a Car structure type in your header le.pdfThis is for a C programDene a Car structure type in your header le.pdf
This is for a C programDene a Car structure type in your header le.pdf
 
This project will implement a simple usernamepassword lookup system.pdf
This project will implement a simple usernamepassword lookup system.pdfThis project will implement a simple usernamepassword lookup system.pdf
This project will implement a simple usernamepassword lookup system.pdf
 
The following code is based on the Josephus problem, the code does c.pdf
The following code is based on the Josephus problem, the code does c.pdfThe following code is based on the Josephus problem, the code does c.pdf
The following code is based on the Josephus problem, the code does c.pdf
 
Tech transfers should the fed Gov’t keep subsidizing University .pdf
Tech transfers should the fed Gov’t keep subsidizing University .pdfTech transfers should the fed Gov’t keep subsidizing University .pdf
Tech transfers should the fed Gov’t keep subsidizing University .pdf
 
Remaining Time 31 minutes, 22 seconds QUESTION 1 Question Completion.pdf
Remaining Time 31 minutes, 22 seconds QUESTION 1 Question Completion.pdfRemaining Time 31 minutes, 22 seconds QUESTION 1 Question Completion.pdf
Remaining Time 31 minutes, 22 seconds QUESTION 1 Question Completion.pdf
 

Recently uploaded

Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 

Recently uploaded (20)

Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Role Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxRole Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptx
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 

I need help creating a java gui that draws lines, shapes, characters.pdf

  • 1. I need help creating a java gui that draws lines, shapes, characters in response to mouseDragged, mousePressed, keyTyped. The user should be able to control the size using a Jslider, and control the color of all the shapes, lines, and letters using JcolorChooser and be able to control the Font of the letters. Solution import java.awt.*; import java.awt.event.*; import javax.swing.*; public class DPanel extends JFrame { private DObjects panel = new DObjects(); private JPanel BPanel = new JPanel(); private JFrame window = new JFrame(); DPanel(){ buildGUI(); } void buildGUI(){ window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.setLayout(new GridLayout(2,2)); window.add(panel); window.add(BPanel); BPanel.setBackground(Color.blue); //define buttons and add to panel JButton rect = new JButton("Rect"); JButton oval = new JButton("Oval"); BPanel.add(rect); BPanel.add(oval); rect.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { panel.setType(1); } }); oval.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { panel.setType(2); } }); window.setVisible(true); window.setSize(1024, 800); } public static void main(String[] args) { new DPanel(); } } import java.awt.*; import java.awt.event.*; import javax.swing.*; class DObjects extends JPanel { public int x1,x2,y1,y2; public int type = 1; public DObjects() { init(); } public void init(){ setBackground(Color.WHITE); addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent m) { x1 = m.getX(); y1 = m.getY(); repaint(); } public void mouseReleased(MouseEvent m) { x2 = m.getX(); y2 = m.getY(); repaint(); } }); addMouseMotionListener(new MouseMotionAdapter() { public void mouseDragged(MouseEvent m) { x2 = m.getX(); y2 = m.getY(); repaint(); } }); } public void setType(int arg){ if(arg == 1){ type = 1; }else if(arg == 2){ type = 2; } } public void paintComponent(Graphics g) { super.paintComponent(g); if(type == 1) { g.drawRect(x1,y1,x2,y2); } else if (type == 2) { g.drawOval(x1,y1,x2,y2); } } }