I'm fairly new to java (been coding for about 8 months) and I'm having a few issues when drawing shapes on JPanel. I can already draw shapes by clicking and dragging my mouse. The issues I have are:
When I try to pass a color to a class where the shape is being drawn, the color won't update and stays as default color (black).
When I try to change the pointSize of my shapes (using setStroke method), the first shape I draw correctly updates the pointSize, but the next shape I draw has the default pointSize. (it appears as if the pointSize resets every time I draw a new shape)
Long story short; I think there is something wrong with the way I instantiate new objects and how I pass values to other classes. I have been looking for answers from other people's questions, but I just can't figure out whats wrong.
Here is my code:
my inputHandler class:
package colors;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.util.Random;
import javax.swing.JLabel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.MouseInputListener;
public class InputHandler implements ActionListener, MouseInputListener, ChangeListener
{
private DrawPanel dp;
public int pos = 0;
public String mode = "";
public String shapeColor = "";
private double startx;
private double starty;
ButtonPanel bp;
myRectangle rectangle = new myRectangle();
myLine line = new myLine();
myEllipse ellipse = new myEllipse();
JLabel label;
public InputHandler(DrawPanel dpanel, ButtonPanel bpanel)
{
this.dp = dpanel;
this.bp = bpanel;
dp.addMouseListener(this);
dp.addMouseMotionListener(this);
}
public void actionPerformed (ActionEvent e)
{
switch (buttonPressed(e)) //handles the pressed button
{
case "Paint":
mode = "paint";
myRectangle.paint = true;
myEllipse.paint = true;
break;
case "Add Line":
mode = "place line";
break;
case "Add Ellipse":
mode = "place ellipse";
break;
case "Add Rectangle":
mode = "place rectangle";
break;
case "Delete":
for(int i = dp.shapesList.size()-1; i>=0; i--)
{
dp.shapesList.remove(i);
}
System.out.println("Canvas cleared.");
dp.repaint();
break;
case "Image":
mode = "image";
dp.repaint();
break;
case "Text":
mode = "text";
dp.repaint();
break;
//colors
case "red":
shapeColor = "red";
rectangle.setColor(Color.red);
ellipse.setColor(Color.red);
line.setColor(Color.red);
System.out.println("red");
break;
case "blue":
shapeColor = "blue";
rectangle.color = Color.blue;
ellipse.color = Color.blue;
line.color = Color.blue;
System.out.println("blue");
break;
case "green":
shapeColor = "green";
rectangle.color = Color.green;
ellipse.color = Color.green;
line.color = Color.green;
System.out.println("green");
break;
case "yellow":
shapeColor = "yellow";
rectangle.color = Color.yellow;
ellipse.color = Color.yellow;
line.color = Color.yellow;
System.out.println("yellow");
break;
case "black":
shapeColor = "black";
rectangle.color = Color.black;
ellipse.color = Color.black;
line.color = Color.black;
System.out.println("black");
break;
case "purple":
shapeColor = "purple";
rectangle.color = Color.magenta;
ellipse.color = Color.magenta;
line.color = Color.magenta;
System.out.println("magenta");
break;
default:
break;
}
}
public String buttonPressed (ActionEvent e)
{
String buttonPressed = e.getActionCommand();
return buttonPressed;
}
@Override
public void mouseClicked(MouseEvent me)
{
if(mode.equals("paint"))
{
double x = me.getX();
double y = me.getY();
for(int i = 0; i<dp.shapesList.size(); i++)
{
if(dp.shapesList.get(i).contains(x, y))
{
}
}
}
if(mode.equals("text"))
{
System.out.println("test");
double x = me.getX();
double y = me.getY();
}
}
@Override
public void mouseEntered(MouseEvent me)
{
}
@Override
public void mouseExited(MouseEvent me)
{
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent me)
{
double x = me.getX();
double y = me.getY();
startx = me.getX();
starty = me.getY();
if(mode.equals("place line"))
{
line = new myLine(startx,starty,x,y);
dp.shapesList.add(line);
dp.repaint();
}
if(mode.equals("place rectangle"))
{
rectangle = new myRectangle(startx,starty,x,y);
dp.shapesList.add(rectangle);
dp.repaint();
}
if(mode.equals("place ellipse"))
{
ellipse = new myEllipse(startx,starty,x,y);
dp.shapesList.add(ellipse);
dp.repaint();
}
}
@Override
public void mouseReleased(MouseEvent me)
{
startx = 0;
starty = 0;
}
@Override
public void mouseDragged(MouseEvent me)
{
double x = me.getX();
double y = me.getY();
if(mode.equals("place line"))
{
line = new myLine(startx,starty,x,y);
dp.shapesList.remove(dp.shapesList.size()-1);
dp.shapesList.add(line);
dp.repaint();
}
if(mode.equals("place rectangle"))
{
rectangle = new myRectangle(startx,starty,x,y);
dp.shapesList.remove(dp.shapesList.size()-1);
dp.shapesList.add(rectangle);
dp.repaint();
}
if(mode.equals("place ellipse"))
{
ellipse = new myEllipse(startx,starty,x,y);
dp.shapesList.remove(dp.shapesList.size()-1);
dp.shapesList.add(ellipse);
dp.repaint();
}
}
@Override
public void mouseMoved(MouseEvent me)
{
}
@Override
public void stateChanged(ChangeEvent ce)
{
if(ce.getSource() == bp.pointSize)
{
rectangle.setStroke(bp.pointSize.getValue());
ellipse.setStroke(bp.pointSize.getValue());
line.setStroke(bp.pointSize.getValue());
}
}
}
the myRectangle class (class where the rectangle is being drawn): ` package colors;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
public class myRectangle implements Drawable {
private double x1, y1, x2, y2;
DrawPanel dp;
public Color color;
public static boolean paint;
public int pointSize;
public myRectangle() {
}
public myRectangle(double x1, double y1, double x2, double y2) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}
@Override
public void draw(Graphics2D g) {
double x = getStartX();
double y = getStartY();
double width = getWidth();
double height = getHeight();
Rectangle2D r = new Rectangle2D.Double(x, y, width, height);
g.setColor(color);
g.draw(r);
g.setStroke(new BasicStroke(pointSize));
if(paint)
{
g.fillRect((int)x, (int)y, (int)width, (int)height);
}
System.out.println("rectangle called");
}
public boolean contains(double x, double y) {
return x >= getStartX() && x <= getEndX() && y >= getStartY() && y <= getEndY();
}
public double getWidth() {
return Math.abs(x1 - x2);
}
public double getHeight() {
return Math.abs(y1 - y2);
}
public double getStartX() {
return Math.min(x1, x2);
}
public double getStartY() {
return Math.min(y1, y2);
}
public double getEndX() {
return Math.max(x1, x2);
}
public double getEndY() {
return Math.max(y1, y2);
}
public String getShape()
{
return "Rectangle";
}
public void setStroke(int stroke)
{
pointSize = stroke;
}
public void setColor(Color color)
{
this.color = color;
}
}`
my DrawPanel class (the class that draws shapes onto the JPanel):` package colors;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.ArrayList;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class DrawPanel extends JPanel
{
public ArrayList<Drawable> shapesList = new ArrayList<Drawable>();
public static int pointSize;
public DrawPanel()
{
super();
}
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
// Cast the graphics object to type Graphics2D
Graphics2D g2d = (Graphics2D) g;
// Loop through the shapesList and draw every shape
for(Drawable s : shapesList)
{
s.draw(g2d);
}
}
}`
And finally here is an image to get a basic understanding of what my program looks like:image of my program
I know this is a lot of code, but I have been struggeling with these problems for a while now and I just can't figure out whats wrong so I finally decided to make a post.
Thanks in advance for any help.
-Mick
been coding for about 8 months- then you should know that class names should start with an upper case character. Follow Java conventions. - camickrrepainton the instance ofDrawPanelto trigger a new paint cycle - MadProgrammer