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));
}
Related
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.
I am using an Icon with Java (Swing) JButton. Is it possible to change the icon when I take my mouse arrow over it?
I saw somewhere on Youtube that it is possible, but am unable to recall it.
You can take advantage of the JButton API which provides this kind of support.
Take a look at JButton#setRolloverIcon and JButton#setRolloverSelectedIcon
You will need to implement MouseListener like this:
public class YourClass extends JFrame implements MouseListener {
#Override
public void mouseEntered(MouseEvent e) { }
#Override
public void mouseExited(MouseEvent e) { }
#Override
public void mouseClicked(MouseEvent e) { }
#Override
public void mousePressed(MouseEvent e) { }
#Override
public void mouseReleased(MouseEvent e) { }
}
Add your function where needed.
You can override the mouseEntered() function by implementing a MouseListener and add the code to change the icon in that function.
If you're using an abstract button, you can just use setRolloverIcon() to set an image which will appear on rollOver.
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
}
}
});
}
}
I am trying to make a border appear when a user hovers their mouse over something, but when I use the paint Method. It says
Syntax Error on Token "(" ; expected and
Syntax Error on Token ")" ; expected
My code is:
JLabel lblAllOrNothing = new JLabel("All Or Nothing (4 BP)");
lblAllOrNothing.addMouseListener(new MouseAdapter() {
#Override
public void mouseEntered(MouseEvent arg0) {
public void paint(Graphics g) { //Error is this line
g.drawRect(0, 72, 256, 72);
}
}
});
I just don't where else I can put a semi-colen. I am new to GUI programming, so I hope I did not make too bad of a mistake. Thanks!
You cannot nest two method in java:
public void mouseEntered(MouseEvent arg0) {
public void paint(Graphics g) { //Method inside a method is not allowed
you can't to call paint() from AWT/Swing Listener
this methods is automatically called by override this method for Container
override paintComponent for JPanel instead of paint()
You have a syntax problem.
JLabel lblAllOrNothing = new JLabel("All Or Nothing (4 BP)");
lblAllOrNothing.addMouseListener(new MouseAdapter() {
#Override
public void mouseEntered(MouseEvent arg0) {
// You can't define a method inside a method!!
}
});
By the way to make custom painting in Swing you should override paintComponent instead of paint. Read more Painting in AWT and Swing
You can't nest a paint method in your mouseEntered method; perhaps you just want
JLabel lblAllOrNothing = new JLabel("All Or Nothing (4 BP)");
lblAllOrNothing.addMouseListener(new java.awt.event.MouseAdapter() {
#Override
public void mouseEntered(MouseEvent e) {
// public void paint(Graphics g) {
// g.drawRect(0, 72, 256, 72); }
java.awt.Component c = e.getComponent();
c.getGraphics().drawRect(0, 72, 256, 72);
}
});
you cant put a method into another one in java make the draw method out the listener
what you should do is just call the draw method in the listener method:
this
new MouseAdapter() {
#Override
public void mouseEntered(MouseEvent arg0) {
// You can't define a method inside a method!!
}
};
is an anonymous class.
now when you use it as an statement it will be used as };, and return an object, while when you use it as pass the object as a parameter the will use it as
fun(new MouseAdapter() {
#Override
public void mouseEntered(MouseEvent arg0) {
// You can't define a method inside a method!!
}
});
You can't have a method inside of a method in Java.
Instead, do this
JLabel lblAllOrNothing = new JLabel("All Or Nothing (4 BP)");
lblAllOrNothing.addMouseListener(new MouseAdapter() {
#Override
public void mouseEntered(MouseEvent arg0) {
//call the paint method here
}
//Move this method here
public void paint(Graphics g) { //Error is this line
g.drawRect(0, 72, 256, 72);
}
});
Note that this will not actually work functionally, but it illustrates why you're getting a syntax error
I have class
public class OwnKeyboardView extends KeyboardView {
...
}
Its have override method OnDraw:
#Override
public void onDraw(final Canvas canvas) {
super.onDraw(canvas);
mCanvas = draw_bitmap(canvas);
Draw_Bitmap(mCanvas);
}
Class KeyboardView Inherited Methods
From class android.view.View
From class java.lang.Object
From interface android.graphics.drawable.Drawable.Callback
Method OnDraw working at press any key.
When i do next:
public Canvas draw_bitmap(Canvas canvas) {
if (mCanvas != null) {
Log.i(TAG, "Copy Canvas");
}
return canvas;
}
Why in method OnDraw i can draw with mCanvas, but other don't can? I want cycle drawing on Keyboard, but OnDraw work only when you press on keyboard
Yoy may create Thread and in the Thread manually call postInvalidate, like this.
Note: It will drain the battery much
boolean flag=true;
private void Thread_Draw(){
new Thread(new Runnable(){
public void run(){
while(flag)
{
postInvalidate();
try{
Thread.sleep(45);
} catch(InterruptedException e)
{
e.printStackTrace();
}
}
}
}
}
Stop thread you can from method
public void stopThread(boolean bool)
{
flag=bool;
}
read about PostInvalidate yom can this: http://developer.android.com/reference/android/view/View.html#postInvalidate()