Drawing circles: separation of code into classes - java

I'm trying to draw circles with random colors and random diameters on mousePressed event but I'm having some issues when I tried to organize my code a little bit "separate my code into classes".
Controller class
public class Controller implements MouseListener {
private HashSet<Circle> circleSet = new HashSet<>();
private int r,g,b,d;
#Override
public void mousePressed(MouseEvent e) {
r = new Random().nextInt(256);
g = new Random().nextInt(256);
b = new Random().nextInt(256);
d = 10+new Random().nextInt(100);
circleSet.add(new Circle(e.getX()-d/2,e.getY()-d/2,d,d,new
Color(r,g,b),0,0));
}
public HashSet<Circle> getCircleSet() {
return circleSet;
}
#Override
public void mouseClicked(MouseEvent e) {}
#Override
public void mouseReleased(MouseEvent e) {}
#Override
public void mouseEntered(MouseEvent e) {}
#Override
public void mouseExited(MouseEvent e) {}
}
View class
public class View extends JPanel{
Controller controller;
HashSet<Circle> circleHashSet;
public View() {
repaint();
controller = new Controller();
circleHashSet = controller.getCircleSet();
this.addMouseListener(controller);
listen();
System.out.println(circleHashSet.size());
}
public void listen() {
new javax.swing.Timer(100, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
circleHashSet = controller.getCircleSet();
System.out.println(circleHashSet.size());
}
});
}
#Override
public void paintComponents(Graphics g) {
super.paintComponents(g);
for (Circle circle:this.circleHashSet) {
paintCircle(g,circle);
}
System.out.println(circleHashSet);
}
public void paintCircle(Graphics graphics, Circle circle) {
graphics.setColor(circle.color);
graphics.fillOval(circle.x,circle.getY(),circle.a,circle.b);
}
circleHashSet still empty even though I did:
//circleHashSet = controller.getCircleSet();
Circles are added into circleSet after pressing the mouse but I can't get them into the View class.

First of all, the problem is that you are calling the paintComponents(Graphics g) method instead of the paintComponent(Graphics g) method.
If you are done changing this, you should create a separate Circle POJO class for the circles, because the only avaliable Circle avaliable in the basic Java API is from the JavaFX package.
After setting the constructor for the circle class, you can add to the HashSet more easily. Like so: circleSet.add(new Circle(e.getX() - d / 2, e.getY() - d / 2, d, new Color(r, g, b)));
Another problem is that the Timer object you created is never used for anything. First, you should make a Timer object and after initializing it, call the start() method on it to start the checking of the circles.

put this line of code
circleHashSet = controller.getCircleSet();
within paintComponents() method. The reason is that you only get the value of HashSet when you start the program while the hashset is empty at this time. You should renew the value of circleHashSet after painting one with mouse.

Related

Implementing the observer pattern with Java and MouseListener

I have three classes, Main, DrawingPanel, and ToolboxPanel. ToolboxPanel contains all my buttons, including an Undo button. DrawingPanel is where I draw objects. I want the undo button to become enabled when an object is drawn on the screen, and disabled when there are no more objects left on the screen. Main creates an instance of DrawingPanel and of ToolboxPanel. I can get my undo button to work correctly if I use static methods and call, say, Main.setUndoStatus(false); from drawingPanel. The setUndoStatus then calls a setter in toolboxPanel. However, I've been reading about the Observer pattern and listeners and think I'm probably not doing it in a best-practice way.
How should I go about this using the observer pattern and/or mouse listeners correctly? (Or any "better" way of doing it).
Here's some code somewhat similar to what I'm doing.
public class Main
{
DrawingPanel drawingPanel;
ToolboxPanel toolboxPanel;
public Main()
{
drawingPanel = new DrawingPanel();
toolboxPanel = new ToolboxPanel(drawingPanel);
}
}
//A static method here to setUndoStatus, but I feel like I shouldn't have it
public static void setUndoStatus(boolean b)
{
{
toolboxPanel.setUndoStatus(b);
}
}
public class ToolboxPanel
{
JButton undoButton;
public ToolboxPanel(DrawingPanel drawingPanel)
{
undoButton = new JButton("Undo");
undoButton.setEnabled(false);
undoButton.addActionListener
(
new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
drawingPanel.undo();
undoButton.setEnabled(drawingPanel.getUndoStatus());
}
}
);
}
public void setUndoStatus(boolean status)
{
undoButton.setEnabled(status);
}
}
public class DrawingPanel
{
public DrawingPanel()
{
addMouseListener(new MouseAdapter()
{
public void mouseReleased(MouseEvent e)
{
//Some code here that's unrelated
if(objectsExist == true) //If something gets drawn, whatever
{
Main.setUndoStatus(true); //Don't like this
}
}
});
}
}

Drawing a weighted tree using MouseListener

I'm new at Java. I'm trying to use GUI to input and display a weighted binary tree. To create root node, user clicks on screen. Subsequently, he drags from one point to another to create an edge and a new node. Once he releases the mouse, he should be prompted to enter the weight, and then edge (with weight displayed) and new node should be displayed.
In the input, while I'm able to create the root, when I try to drag to create the next node, the program goes into a loop.
Code is given below.
class myPanel extends JPanel implements MouseListener, MouseMotionListener
{
int node_is_present[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; //1 if node exists, 0 otherwise
int no_of_nodes=0;
int node_coords[][]=new int[15][2]; //stores co-ordinates of each node
int weight[]=new int[15]; //stores weight of line in the order entered
int x1,x2,y1,y2;
int tracker=-1;
int SIZE=20;
public void clear()
{
x1=0;
y1=0;
x2=0;
y2=0;
}
#Override
public void mouseClicked(MouseEvent me) {}
#Override
public void mousePressed(MouseEvent me) //will store intial x,y
{
tracker=1;
x1=me.getX();
y1=me.getY();
repaint();
}
#Override
public void mouseReleased(MouseEvent me) //will store final x,y and update arrays
{
x2=me.getX();
y2=me.getY();
if(x1!=x2)
tracker=2;
node_is_present[no_of_nodes]=1;
node_coords[no_of_nodes][0]=me.getX();
node_coords[no_of_nodes][1]=me.getY();
no_of_nodes++;
repaint();
}
#Override
public void mouseEntered(MouseEvent me){}
#Override
public void mouseExited(MouseEvent me) {}
#Override
public void mouseDragged(MouseEvent me){}
#Override
public void mouseMoved(MouseEvent me) {}
public void paintComponent(Graphics g)
{
if(tracker==1 && no_of_nodes==0) //for root node
{
g.drawOval(x1-(SIZE/2),y1-(SIZE/2),SIZE,SIZE);
g.drawString(String.valueOf(no_of_nodes+1),x1-(SIZE/2),y1-(SIZE/2));
}
else if(tracker==2 && no_of_nodes>0)
{
g.drawLine(x1 -(SIZE/2) , y1 -(SIZE/2) , x2 -(SIZE/2) , y2 -(SIZE/2));
g.drawOval(x2-(SIZE/2),y2-(SIZE/2),SIZE,SIZE);
g.drawString(String.valueOf(no_of_nodes+1),x2-(SIZE/2),y2-(SIZE/2));
String str=JOptionPane.showInputDialog("Enter the weight of the edge: ");
int w=Integer.parseInt(str);
weight[no_of_nodes-1]=w;
g.drawString(String.valueOf(w),Math.round((x1+x2)/2),Math.round((y1+y2)/2));
clear();
}
}
myPanel()
{
this.setSize(1500, 500);
addMouseListener(this);
addMouseMotionListener(this);
clear();
}
}
public class tree1
{
public static void main(String[] args) {
JFrame frame=new JFrame();
myPanel panel=new myPanel();
frame.setSize(1500, 800);
frame.setLocation(0, 0);
frame.setTitle("Tree");
frame.setBounds(100,100,1500,800);
frame.add(panel);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Painting in Swing may occur at any time for any reason. As #HoverCraftFullOfEels has already pointed out, you should have NO application within any paint method, these should contain ONLY the functionality required to paint your component.
Start by taking a look at Performing Custom Painting and Painting in AWT and Swing for more details about the paint sub system
Basically, within your paintComponent, call super.paintComponent first and remove, in particular, the else block.
Make yourself some kind of model, which holds the required data and then use the paintComponent method to paint that model.

How to pass a variable to a inner class

I want to use canvas inside mousePressed. How can I do that?
public DragManager(Canvas canvas)
{
canvas.addMouseListener(new MouseAdapter() {
#Override public void mousePressed(MouseEvent e)
{
canvas.something(); // does not work.
}
});
}
As many of the guys over here already said you have to make function parameter final.
public DragManager(final Canvas canvas)
{
canvas.addMouseListener(new MouseAdapter() {
#Override public void mousePressed(MouseEvent e)
{
canvas.something();
}
});
}
That means that this variable cannot point to any other object. E.g. you cannot do this inside function:
canvas = SomeOtherCanvas
If you create an object using a local class definition, that object can keep "living" after local variables have been discarded from the stack (after DragManager constructor completion). It has to have a copy of the local values. If you make this parameter final (so it's guaranteed that reference inside constructor wouldn't point to some other place) it's really easy to have a copy: just copy a reference. If there was no such rule you (well, not you personally, but Java language) would need to constantly sync those values and that would be much more complex and slow solution.
Make the parameter final:
public DragManager(final Canvas canvas)
you Cannot refer to non final variable inside an inner class defined. mark your canvas as final.
public void DragManager(final Canvas canvas)
{
canvas.addMouseListener(new MouseAdapter() {
#Override public void mousePressed(MouseEvent e)
{
System.out.println(canvas);;// does not work.
}
});
public DragManager(final Canvas canvas)
{
canvas.addMouseListener(new MouseAdapter() {
#Override public void mousePressed(MouseEvent e)
{
canvas.something(); // does work.
}
});
}
since you can modify canvas variable, you should define it as final(constant reference).
without using final keyword. you can add Init method that return this and add private variable.
pass canvas by Call Init method.
public DragManager(Canvas canvas)
{
canvas.addMouseListener(new MouseAdapter() {
Canvas _canvas;
#Override public void mousePressed(MouseEvent e)
{
_canvas.something(); // does not work.
}
public MouseAdapter Init(Canvas canvas){
_canvas = canvas;
return this;
}
}.Init(canvas));
}

JME: How to get the complete screen in WHITE without buttons, etc etc

Please have a look at the following code
First, Please note I am a 100% newbie to Java Mobile.
In here, I am making the light on and vibrate on when user click the button. However, I really wanted to create a SOS application which turn the whole screen into white, and go to black, like that, in the thread. I guess I didn't achieve that by this app because even the lights are on, the buttons are still there. I tried to turn the "Form" color to "white" but it seems like JME has no "Color" class.
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class Midlet extends MIDlet{
private Form f;
private Display d;
private Command start,stop;
private Thread t;
public Midlet()
{
t = new Thread(new TurnLightOn());
}
public void startApp()
{
f = new Form("Back Light On");
d = Display.getDisplay(this);
d.setCurrent(f);
start = new Command("Turn On",Command.OK,0);
stop = new Command("Turn Off",Command.OK,1);
f.addCommand(start);
f.setCommandListener(new Action());
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional)
{
this.notifyDestroyed();
}
private class Action implements CommandListener
{
public void commandAction(Command c, Displayable dis)
{
f.append("Light is Turnning On");
t.start();
}
}
private class ActionOff implements CommandListener
{
public void commandAction(Command c, Displayable dis)
{
}
}
private class TurnLightOn implements Runnable
{
public void run()
{
f.append("Working");
for(int i=0;i<100;i++)
{
try
{
d.flashBacklight(200);
d.vibrate(200);
Thread.sleep(1000);
}
catch (InterruptedException ex)
{
ex.printStackTrace();
}
}
}
}
}
Use the javax.microedition.lcdui.Canvas instead of Form. This example can get you started
public void startApp()
{
f = new Form("Back Light On");
d = Display.getDisplay(this);
start = new Command("Turn On",Command.OK,0);
stop = new Command("Turn Off",Command.OK,1);
f.addCommand(start);
f.setCommandListener(new Action());
myCanvas = new MyCanvas();
d.setCurrent(myCanvas);
myCanvas.repaint();
}
Now create a canvas and implement paint method like this:
class MyCanvas extends Canvas {
public void paint(Graphics g) {
// create a 20x20 black square in the center
// clear the screen first
g.setColor(0xffffff);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(0xffffff); // make sure it is white color
// draw the square, <b>changed to rely on instance variables</b>
<b>g.fillRect(x, y, getWidth(), getHeight());</b>
}
}

Boolean Value Change Listener Java

I need a listener that will constantly check if a static boolean value has been changed so that I can repaint a component on a frame. Can someone please help me I really don't know much about listeners and haven't worked with them much? Help will be greatly appreciated.
edit(more clarity): I have two separate classes in which on class is the "main frame" the second class is an extension of JLabel and implements MouseListner for a "clickable photo". The "main frame" creates instances of the photo and when the photo is clicked the "main frame" is supposed to paint on the panel a description of the photo. This is "main frame"
MenuBar menuBar;
static AnnotationVisual a;
Picture pic;
Picture pic2;
GalleryScreen(int rows, int columns){
this.setBorder(BorderFactory.createEmptyBorder(500,500,0,0));
pic = new Picture("pic1", "Z:/My Documents/Downloads/Ball.jpg", new Coordinate(0,0));
pic2 = new Picture("pic2", "Z:/My Documents/Downloads/hoop.jpg" , new Coordinate(1,0));
this.add(pic);
this.add(pic2);
a = new AnnotationVisual();
}
public void paintComponent(Graphics g){
super.paintComponent(g);
if(a.shouldAnnotate()){
FontMetrics size= g.getFontMetrics();
if(getWidth()>=(a.dispX()+size.stringWidth(a.annotationText()))){
g.setColor(Color.white);
g.fillRect(a.dispX()-3,a.dispY()-12,size.stringWidth(a.annotationText())+5,15);
g.setColor(Color.black);
g.drawRect(a.dispX()-3,a.dispY()-12,size.stringWidth(a.annotationText())+5,15);
g.drawString(a.annotationText(), a.dispX(), a.dispY());
}else{
String sub="";
int letters=0;
g.setColor(Color.white);
g.fillRect(a.dispX()-3,a.dispY()-12,getWidth(),15);
g.setColor(Color.black);
for(int i=0;i<a.annotationText().length();i++){
if(a.dispX()+letters+16<=getWidth()){
sub+=a.annotationText().substring(i,i+1);
letters=size.stringWidth(sub);
}else{
sub=sub+"...";
i=a.annotationText().length();
}
}
g.drawRect(a.dispX()-3,a.dispY()-12,size.stringWidth(sub)+3,15);
g.drawString(sub,a.dispX(),a.dispY());
}
}
}
public static AnnotationVisual getA()
{
return a;
}
This is "clickable photo"
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.*;
import javax.swing.*;
public class Picture extends JLabel implements MouseListener
{
String myAnnotation;
String filePath;
Coordinate imageCoord;
private boolean wasDoubleClick;
private Timer timer;
EditAnnotation newEdit;
AnnotationVisual newVisual;
public Picture(String annotation, String filePath, Coordinate coord)
{
super(new ImageIcon(filePath));
this.addMouseListener(this);
myAnnotation=annotation;
this.filePath = filePath;
imageCoord = coord;
newEdit = new EditAnnotation(annotation);
newVisual = new AnnotationVisual();
}
public Picture(String filePath)
{
super(new ImageIcon(filePath));
this.addMouseListener(this);
this.filePath = filePath;
newEdit = new EditAnnotation();
newVisual = new AnnotationVisual();
}
public String getAnnotation()
{
return myAnnotation;
}
public AnnotationVisual getAnnotationVisual()
{
return newVisual;
}
public void setAnnotation(String annotation)
{
myAnnotation=annotation;
}
public Coordinate getCoordinate()
{
return imageCoord;
}
public void setCoordinate(Coordinate coord)
{
imageCoord = coord;
}
public Dimension getSize()
{
return new Dimension(super.getIcon().getIconWidth(), super.getIcon().getIconHeight());
}
public void mouseClicked(MouseEvent e)
{
final int scrLocX = (int)e.getLocationOnScreen().getX();
final int scrLocY = (int)e.getLocationOnScreen().getY();
if (e.getClickCount() == 2)
{
wasDoubleClick = true;
}
else if(e.getClickCount() == 1)
{
Integer timerinterval = (Integer) Toolkit.getDefaultToolkit().getDesktopProperty("awt.multiClickInterval");
timer = new Timer(timerinterval.intValue(), new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
if (wasDoubleClick)
{
GalleryScreen.getA().deleteAnnotation();
myAnnotation = newEdit.getAnnotation();
newEdit.show(myAnnotation);
wasDoubleClick = false;
}
else
{
GalleryScreen.getA().deleteAnnotation();
GalleryScreen.getA().showAnnotation(scrLocX, scrLocY , myAnnotation);
}
}
});
timer.setRepeats(false);
timer.start();
}
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}
public void mousePressed(MouseEvent e)
{
}
public void mouseReleased(MouseEvent e)
{
}
}
AnnotationVisual is the thing that supposed to pop up when single clicked
You're probably better off making the boolean private, and only allowing it to be changed through a setter method. The setter method, when called, should then repaint the component.
The point of listeners is to invert the logic. You don't constantly check if a value is changed. You notify the listener when you change the value.
So, instead of Foo.bar = 5, you invoke Foo.setBar(5), where in addition to the assignment, you call barListener.valueChanged(value)
As a sidenote - avoid storing state in static variables.
You don't set a listener on a field in Java, you set it on a property. While properties (according to the JavaBeans spec) can be fields, they're usually done as pairs of methods (one getter, one setter; the latter being not needed for read-only fields) as that lets you hook extra logic in to be called when the property is accessed. Such as firing a listener callback to say that the value has changed. (You could use a thread to monitor for that sort of thing, but that's really nasty and error-prone. Wasteful too.)
One thing to be aware of though: you don't know what thread the value will have been modified from. Take care when invoking back into Swing…

Categories

Resources