patternjavaMinor
Extremely Simple Paint Program in Java
Viewed 0 times
simpleextremelyprogramjavapaint
Problem
I have written a very basic paint program in java using AWT and Swing. I have separated the program into two different class files; one of them holds the
In my first try, I tried using
Then I switched to using a different approach (stored previous x and y locations and drew a line between the current position and the previous location). Here are the two class files:
The custom
```
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Panel extends JPanel
{
// PROPERTIES
private final int DEFAULT_WIDTH = 800;
private final int DEFAULT_HEIGHT = 800;
private final Color BACK_COLOR = Color.WHITE;
private int x1, y1, x2, y2;
private MyMouseHandler handler;
private Graphics g;
// CONSTRUCTOR
public Panel()
{
setBackground( BACK_COLOR );
setPreferredSize( new Dimension( DEFAULT_WIDTH, DEFAULT_HEIGHT ) );
handler = new MyMouseHandler();
this.addMouseListener( handler );
this.addMouseMotionListener( handler );
}
// METHOD
public void paintComponent(Graphics g)
{
super.paintComponent(g);
}
private void setUpDrawingGraphics()
{
g = getGraphics();
}
// INNER CLASS
private class MyMouseHandler extends MouseAdapter
{
public void mousePressed( MouseEvent e )
{
x1 = e.getX();
y1 = e.getY();
System.out.println("Mouse is being pressed at X: " + x1 + " Y: " + y1);
setUpDrawingGraphics();
x2=x1;
y2=y1;
}
public void mouseDragged( MouseEvent e )
{
x1 = e.getX();
y1 = e.getY();
System.out.println("Mouse is being dragged at X: " + x1 + " Y: " + y1);
g.drawLine(x1,y1,x2,y2);
x2=x1;
y2=y1;
JFrame and adds a custom panel (this custom panel being the second class file, extending JPanel).In my first try, I tried using
Timer and an ActionListener with repaint() but that seemed very very laggy (I could not draw straight lines/curves, they would always be laggy when I dragged my mouse fast.)Then I switched to using a different approach (stored previous x and y locations and drew a line between the current position and the previous location). Here are the two class files:
The custom
JPanel class:```
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Panel extends JPanel
{
// PROPERTIES
private final int DEFAULT_WIDTH = 800;
private final int DEFAULT_HEIGHT = 800;
private final Color BACK_COLOR = Color.WHITE;
private int x1, y1, x2, y2;
private MyMouseHandler handler;
private Graphics g;
// CONSTRUCTOR
public Panel()
{
setBackground( BACK_COLOR );
setPreferredSize( new Dimension( DEFAULT_WIDTH, DEFAULT_HEIGHT ) );
handler = new MyMouseHandler();
this.addMouseListener( handler );
this.addMouseMotionListener( handler );
}
// METHOD
public void paintComponent(Graphics g)
{
super.paintComponent(g);
}
private void setUpDrawingGraphics()
{
g = getGraphics();
}
// INNER CLASS
private class MyMouseHandler extends MouseAdapter
{
public void mousePressed( MouseEvent e )
{
x1 = e.getX();
y1 = e.getY();
System.out.println("Mouse is being pressed at X: " + x1 + " Y: " + y1);
setUpDrawingGraphics();
x2=x1;
y2=y1;
}
public void mouseDragged( MouseEvent e )
{
x1 = e.getX();
y1 = e.getY();
System.out.println("Mouse is being dragged at X: " + x1 + " Y: " + y1);
g.drawLine(x1,y1,x2,y2);
x2=x1;
y2=y1;
Solution
Your code formatting deviates from the default Java code formatting, just saying.
Wildcard imports are kinda bad. If you can avoid them, please do so.
I'm sure you can find a more descriptive name for this class, like
Personally, I don't like such "structuring" comments, they do nothing and only clutter up the code and need to be manually moved when refactoring automatically.
Personally I also don't like this column based alignment, it puts my brain into some sort of "column reading mode" which makes it hard for me to associate the value with the variable.
You should not declare multiple variables on the same line, it makes it easy to miss the declaration of variables.
This method seems to override a super method, yet misses the
Why are you doing this on every mouse press?
This is another bad name. What does it run? And it is not a panel.
Any reason why you're not using the constants defined in
The program works fine, I can draw normal curves at low mouse speed. But again, as I increase the mouse speed the lines/curves start the get jaggy and ugly.
That is how it is.
I have gathered the two classes into one class file and the program stoped the lag. What is causing this?
I see this behavior in both programs. Combining them in the same file has for sure nothing to do with it.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;Wildcard imports are kinda bad. If you can avoid them, please do so.
public class Panel extends JPanelI'm sure you can find a more descriptive name for this class, like
PaintablePanel or MousePaintablePanel.// PROPERTIESPersonally, I don't like such "structuring" comments, they do nothing and only clutter up the code and need to be manually moved when refactoring automatically.
private final int DEFAULT_WIDTH = 800;
private final int DEFAULT_HEIGHT = 800;
private final Color BACK_COLOR = Color.WHITE;Personally I also don't like this column based alignment, it puts my brain into some sort of "column reading mode" which makes it hard for me to associate the value with the variable.
private int x1, y1, x2, y2;You should not declare multiple variables on the same line, it makes it easy to miss the declaration of variables.
private MyMouseHandler handler;MyMouseHandler is not a very good name.public void paintComponent(Graphics g)
{
super.paintComponent(g);
}This method seems to override a super method, yet misses the
@Override annotation. Also, if it does not do anything, why override it?public void mousePressed( MouseEvent e )
{
...
SetUpDrawingGraphics();Why are you doing this on every mouse press?
public class RunPanelThis is another bad name. What does it run? And it is not a panel.
frame.setDefaultCloseOperation(3);Any reason why you're not using the constants defined in
JFrame?The program works fine, I can draw normal curves at low mouse speed. But again, as I increase the mouse speed the lines/curves start the get jaggy and ugly.
That is how it is.
I have gathered the two classes into one class file and the program stoped the lag. What is causing this?
I see this behavior in both programs. Combining them in the same file has for sure nothing to do with it.
Code Snippets
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class Panel extends JPanel// PROPERTIESprivate final int DEFAULT_WIDTH = 800;
private final int DEFAULT_HEIGHT = 800;
private final Color BACK_COLOR = Color.WHITE;private int x1, y1, x2, y2;Context
StackExchange Code Review Q#123947, answer score: 4
Revisions (0)
No revisions yet.