patternjavaMinor
Drawing simple shapes by mouse dragging
Viewed 0 times
simpleshapesdrawingdraggingmouse
Problem
I want to make figures of a rectangle and a circle. How can I make the code more elegant?
```
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class r extends JPanel
{
public int x1,x2,y1,y2;
public static double SWITCH;
public r()
{
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 paintComponent(Graphics g)
{
super.paintComponent(g);
if(SWITCH == 2)
{
g.drawRect(x1, y1, x2, y2);
}
else if (SWITCH == 3)
{
g.drawOval(x1,y1,x2,y2);
}
else
{
g.drawString("qwe", x1, y1);
}
}
}
public class q extends JFrame
{
public static void main(String[] args)
{
q window = new q();
window.setVisible(true);
window.setSize(1024, 800);
window.setDefaultCloseOperation(EXIT_ON_CLOSE);
Container cont = window.getContentPane();
cont.setLayout(new GridLayout(2,2));
r panel = new r();
JPanel BPanel = new JPanel();
cont.add(panel);
cont.add(BPanel);
BPanel.setBackground(Color.blue);
JButton button1,button2;
button1 = new JButton("Rect");
button2 = new JButton("Oval");
```
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class r extends JPanel
{
public int x1,x2,y1,y2;
public static double SWITCH;
public r()
{
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 paintComponent(Graphics g)
{
super.paintComponent(g);
if(SWITCH == 2)
{
g.drawRect(x1, y1, x2, y2);
}
else if (SWITCH == 3)
{
g.drawOval(x1,y1,x2,y2);
}
else
{
g.drawString("qwe", x1, y1);
}
}
}
public class q extends JFrame
{
public static void main(String[] args)
{
q window = new q();
window.setVisible(true);
window.setSize(1024, 800);
window.setDefaultCloseOperation(EXIT_ON_CLOSE);
Container cont = window.getContentPane();
cont.setLayout(new GridLayout(2,2));
r panel = new r();
JPanel BPanel = new JPanel();
cont.add(panel);
cont.add(BPanel);
BPanel.setBackground(Color.blue);
JButton button1,button2;
button1 = new JButton("Rect");
button2 = new JButton("Oval");
Solution
Just a few thoughts...
You are using x and y coordinates. Why not take advantage of the
The use of magic numbers (you storing 2 and 3 into
As per Java standards, a class name should begin with a capital letter. Your class is currently called
Rather than
You are using x and y coordinates. Why not take advantage of the
Point class? The MouseEvent class has methods that return Point objects, and it would probably make your code more readable. x1 and y1 don't mean anything to me, but you could probably give meaningful names to the points represented by (x1, y1) and (x2, y2).The use of magic numbers (you storing 2 and 3 into
SWITCH, for example) is a poor practice. Use constant ints or (as Landei suggested in the comments) an enum. It will allow you to improve the readable and understandability of the code - at a first read-through, I didn't get what you were doing.As per Java standards, a class name should begin with a capital letter. Your class is currently called
r. You should give it a meaningful name. You might also want to look into other Java standards for code formatting in terms of brace placement, indentation, and so forth. To me, your code is just plain hard to read because of formatting.Rather than
if and switch/case statements, consider leveraging polymorphism in order to draw different shapes. Move the logic to draw different shapes into concrete subclasses, while perhaps maintaining common logic in a superclass.Context
StackExchange Code Review Q#3609, answer score: 5
Revisions (0)
No revisions yet.