drag and drop image from imageview1 to imageview2 on anchorpane - javafx-8

There are two imageviews on AnchorPane in scenebuilder .one imageview1 has image
and now trying to drag and drop on imageview2.
public class FXMLDocumentController implements Initializable {
#FXML
private ImageView imageview1;
#FXML
private ImageView imageview2;
#Override
public void initialize(URL url, ResourceBundle rb) {
imageview1.setImage(new Image("File:///C:imagedragndropex6/IMG_4093.JPG"));
}
#FXML
private void handleDragDetected(MouseEvent event) {
System.out.println("DragDetectedEntered");
// ImageView im=(ImageView)event.getSource();
Image img=imageview1.getImage();
Dragboard db= imageview1.startDragAndDrop(TransferMode.COPY);
// db.setDragView(imageview1.getImage());
ClipboardContent content=new ClipboardContent();
content.putImage(img);
System.out.println(content.hasImage());
db.setContent(content);
System.out.println(db.hasImage());
System.out.println("DragDetectedExited");
}
#FXML
private void handleDragOver(DragEvent event) {
System.out.println("DragOverEntered");
Dragboard db=event.getDragboard();
System.out.println(db.hasImage());
if(db.hasImage()){
event.acceptTransferModes(TransferMode.COPY);
System.out.println(event.getAcceptedTransferMode());
}
event.consume();
System.out.println("DragOverExited");
}
#FXML
private void handleDragDropped(DragEvent event) {
System.out.println("DragDroppedrEntered");
Dragboard dragboard=event.getDragboard();
if(dragboard.hasImage()){
Image img=dragboard.getImage();
imageview2.setFitHeight(200);
imageview2.setFitWidth(200);
imageview2.setPreserveRatio(true);
imageview2.setImage(img);
event.setDropCompleted(true);
}
else
event.setDropCompleted(false);
System.out.println("DragDroppedrEntered");
}
}
output:
DragDetectedEntered
true
true
DragDetectedExited
DragOverEntered
false
DragOverExited
Plz let me know what is wrong with this trying for hours...

public class FXMLDocumentController implements Initializable {
#FXML
private ImageView imageview1;
#FXML
private AnchorPane anchorpane;
#Override
public void initialize(URL url, ResourceBundle rb) {
imageview1.setImage(new Image("File:///D:/Downloads/Earthpic.jpg"));
// TODO
}
#FXML
private void handleDragDetected(MouseEvent event) {
System.out.println("DragDetectedEntered");
Dragboard db= imageview1.startDragAndDrop(TransferMode.COPY_OR_MOVE);
Image img=imageview1.getImage();
ClipboardContent content=new ClipboardContent();
content.putImage(img);
System.out.println(content.hasImage());
db.setContent(content);
System.out.println(db.hasImage());
System.out.println("DragDetectedExited");
event.consume();
}
#FXML
private void handleDragOver(DragEvent event) {
System.out.println("DragOverEntered");
System.out.println(event.getGestureSource());
System.out.println(event.getGestureTarget());
Dragboard db=event.getDragboard();
System.out.println(db.hasImage());
if(db.hasImage()){
event.acceptTransferModes(TransferMode.COPY_OR_MOVE);
System.out.println(event.getAcceptedTransferMode());
}
event.consume();
System.out.println("DragOverExited");
}
#FXML
private void handleDragDropped(DragEvent event) {
System.out.println("DragDroppedrEntered");
Dragboard dragboard=event.getDragboard();
if(dragboard.hasImage()){
Image img=dragboard.getImage();
ImageView imageview2=new ImageView();
imageview2.setFitHeight(200);
imageview2.setFitWidth(200);
imageview2.setLayoutX(400);
imageview2.setLayoutY(150);
imageview2.setPreserveRatio(true);
imageview2.setImage(img);
anchorpane.getChildren().add(imageview2);
event.setDropCompleted(true);
}
else
event.setDropCompleted(false);
System.out.println("DragDroppedrEntered");
}
}
Finally,this works fine...handleDragOver and handleDragDropped has to be set to anchorpane.

Related

How to reset back position of a dragging object, if the dragging object dont met IDropHandler script containing object

In Unity i am using IBeginDragHandler, IDragHandler, IEndDragHandler to drag and drop object. it's working fine. My need is to reset the position of the draggable object to the starting position if it doesn't touch another object that contains IDropHandler. below is my code
Drag script:-
using UnityEngine.EventSystems;
public class Compiler_Drag : MonoBehaviour, IPointerDownHandler, IBeginDragHandler, IEndDragHandler, IDragHandler
{
[SerializeField] private Canvas canvas;
private RectTransform rectTransform;
private CanvasGroup canvasGroup;
Vector3 myPosition;
// Start is called before the first frame update
void Start()
{
myPosition = this.gameObject.transform.position;
}
private void Awake()
{
rectTransform =GetComponent<RectTransform>();
canvasGroup = GetComponent<CanvasGroup>();
}
// Update is called once per frame
void Update()
{
}
public void OnBeginDrag(PointerEventData eventData)
{
Debug.Log("OnBeginDrag");
canvasGroup.alpha = .6f;
canvasGroup.blocksRaycasts = false;
}
public void OnDrag(PointerEventData eventData)
{
Debug.Log("OnDrag");
rectTransform.anchoredPosition +=eventData.delta/ canvas.scaleFactor;
}
public void OnEndDrag(PointerEventData eventData)
{
Debug.Log("OnEndDrag");
canvasGroup.alpha = 1f;
canvasGroup.blocksRaycasts = true;
}
public void OnPointerDown(PointerEventData eventData)
{
Debug.Log("OnPointerDown");
}
}
> Drop object script:-
using UnityEngine.EventSystems;
public class Compiler_Drop_Handler : MonoBehaviour, IDropHandler
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void OnDrop(PointerEventData eventData)
{
Debug.Log("OnDrop");
if(eventData.pointerDrag !=null)
{
eventData.pointerDrag.GetComponent<RectTransform>().anchoredPosition = GetComponent<RectTransform>().anchoredPosition;
}
}
}
what I need is that I should be only able to drop the dragging object, to the object that contains/is attached to the second script.

Starting a new activity android

I am trying to learn the principles of starting a new activity in android. I am using "Android studio". I have created a MainActivity class which contains a text which tells the user to push the button, and the button. When the button is pushed the new activity is supposed to start(aktivitet2Klasse). This activity contains two textviews and a button. When the first button is pressed, the tetview should display "works fine!". When the second button is pushed it is supposed to display "Finished!". The point with the app is just to start one activity from another. However after pushing the button on the first screen (first activity) the app crashes. I have studies a lot of the same issues on these sides, but I cannot figure what is wrong.
Here is the code:
The main activity:
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView instruction;
private Button startButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
instruction = (TextView) findViewById(R.id.InstruksjonsView);
startButton = (Button) findViewById(R.id.btn1);
startButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
Intent intent = new Intent(MainActivity.this,aktivitet2Klasse.class);
startActivity(intent);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
The next activity:
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class aktivitet2Klasse extends AppCompatActivity {
private Button button2;
private TextView text;
private Button button3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
button2 = (Button) findViewById(R.id.btn2);
button3 = (Button) findViewById(R.id.btn3);
text = (TextView) findViewById(R.id.textView1);
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
text.setText("Works fine!");
}
});//End button2Listener
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
text.setText("Finished!");
}
});//End button3Listener
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
you forgot to set the content view in the second activity
public class aktivitet2Klasse extends AppCompatActivity {
private Button button2;
private TextView text;
private Button button3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//YOU NEED TO SET THE CONTENTVIEW
// like setContentView(R.layout.activity_secondary);
Intent intent = getIntent();
button2 = (Button) findViewById(R.id.btn2);
button3 = (Button) findViewById(R.id.btn3);
text = (TextView) findViewById(R.id.textView1);
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
text.setText("Works fine!");
}
});//End button2Listener
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
text.setText("Finished!");
}
});//End button3Listener
}

How to bind ngui events in StrangeIoC in unity3d

I have some problem with binding ngui events in the StrangeIoC framework.
This is an unityGUI sample:
public class TestView : View
{
private readonly Rect buttonRect = new Rect(0, 0, 200, 50);
public Signal buttonClicked = new Signal();
public void OnGUI()
{
if (GUI.Button(buttonRect, "Test"))
{
buttonClicked.Dispatch();
}
}
}
This is the NGUI version:
public class NGUIView : View
{
public UIButton Button;
public Signal buttonClicked = new Signal();
private void Start()
{
if (Button != null)
{
EventDelegate.Add(Button.onClick, buttonClicked.Dispatch);
}
}
}
In the NGUI version, the the buttonClicked is never dispatched. I noticed in the scene the Notify property on that button has an empty value.
This one works, but the buttonClicked is triggered several times :(
public class NGUIView : View
{
public UIButton Button;
public Signal buttonClicked = new Signal();
void Update()
{
if (Button.state == UIButtonColor.State.Pressed)
{
buttonClicked.Dispatch();
}
}
}
Could you kindly tell me how do you handle this NGUI-StrangeIoC situation?
Thanks!
I finally figured out. Add the delegate in Awake instead of Start.
Now here's the perfect solution(not sure enough):
public class TestView : View
{
private readonly Rect buttonRect = new Rect(0, 0, 200, 50);
public UIButton Button;
public Signal buttonClicked = new Signal();
private void OnGUI()
{
if (GUI.Button(buttonRect, "Test"))
{
buttonClicked.Dispatch();
}
}
private void Awake()
{
EventDelegate.Add(Button.onClick, buttonClicked.Dispatch);
}
}

how to jump to an anchor tag from a button clickhandler

I have a clickhandler for a button. I want the screen scrolls to an anchor like
Anchor a = new Anchor();
a.setName("stopHere");
(...)
button.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
// Do somethings
(...)
// Scroll to Anchor "stopHere"
?????
}
});
How can I do it?
This is similar when using an anchor click to go to stopHere point , but I wan do it next the button is clicked.
Thanks.
The method Element#scrollIntoView() should do what you want:
final Anchor a = new Anchor();
a.setName("stopHere");
button.addClickHandler(new ClickHandler()
{
#Override
public void onClick(ClickEvent event)
{
a.getElement().scrollIntoView();
}
}
Or use Window#scrollTo(int, int)
final Anchor a = new Anchor();
a.setName("stopHere");
button.addClickHandler(new ClickHandler()
{
#Override
public void onClick(ClickEvent event)
{
Window.scrollTo(a.getAbsoluteLeft(), a.getAbsoluteTop());
}
}

Load a Listview In a Fragment using Tabs (Actionbarsherlock)

I have a main activity (FragmentDemoActivity) which creates 2 tabs. Fragment A and Fragment B.
I have another class called CategoryList which loads a customlist from an sqlite database.
I am using this example which works great for the TAB parts.
I want to load the CategoryList into the Fragment A.
My CategoryList class is as follows
// All the imports
public class CategoryList extends BaseActivity{
ImageView imgFeedback;
ImageView imgAbout;
ListView listCategories;
ProgressBar prgLoading;
TextView txtAlert;
AdView ads;
static String[] CategoryName ;
ListAdapter la;
static int[] id;
List<String> catList;
static DBHelper dbhelper;
class ListAdapter extends BaseAdapter {
private LayoutInflater inflater;
#SuppressWarnings("unused")
private Context ctx;
public ListAdapter(Context context) {
inflater = LayoutInflater.from(context);
ctx = context;
}
#Override
public int getCount() {
return CategoryName.length;
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null){
convertView = inflater.inflate(R.layout.row, null);
holder = new ViewHolder();
holder.imgPreview = (ImageView) convertView.findViewById(R.id.imgPreview);
holder.txtRecipeName = (TextView) convertView.findViewById(R.id.txtRecipeName);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
holder.imgPreview.setImageResource(getResources().
getIdentifier(CategoryName[position], "drawable", getPackageName()));
String categoryName = CategoryName[position];
categoryName = categoryName.toLowerCase();
categoryName = Character.toString(categoryName.charAt(0)).toUpperCase()+categoryName.substring(1);
holder.txtRecipeName.setText(categoryName);
return convertView;
}
class ViewHolder {
ImageView imgPreview;
TextView txtRecipeName;
}
}
final Context context = this ;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.categories_list);
AppRater.displayAsk2Rate(this, 1, 1, false);
getSupportActionBar().setHomeButtonEnabled(true);
dbhelper = new DBHelper(this);
try {
dbhelper.createDataBase();
}catch(IOException ioe){
throw new Error("Unable to create database");
}
/** then, the database will be open to use */
try{
dbhelper.openDataBase();
}catch(SQLException sqle){
throw sqle;
}
CategoryName = dbhelper.getAllCategories();
dbhelper.close();
catList = Arrays.asList(CategoryName);
/* imgAbout = (ImageView) findViewById(R.id.imgAbout);
imgAbout.setOnClickListener(new ButtonListner(CategoryList.this));
imgFeedback = (ImageView) findViewById(R.id.imgFeedback);
imgFeedback.setOnClickListener(new ButtonListner(CategoryList.this));*/
listCategories = (ListView) findViewById(R.id.listCategories);
la = new ListAdapter(CategoryList.this);
listCategories.setAdapter(la);
txtAlert = (TextView) findViewById(R.id.txtAlert);
ads = (AdView) findViewById(R.id.ads);
Ads.loadAds(ads);
listCategories.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
String category = catList.get(position);
Intent i = new Intent(CategoryList.this, RecipesList.class);
i.putExtra("Category_Name", category);
startActivity(i);
}
});
}
}
My FragmentA is as follows
public class FragmentA extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saved)
{
View view = inflater.inflate(R.layout.categories_list, container, false);
ListView listCategories = (ListView) getActivity().findViewById(R.id.listCategories);
ListAdapter lav = new ListAdapter(CategoryList.this);
listCategories.setAdapter(lav);
return view;
}
#Override
public void onActivityCreated (Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
}
}
Any pointers ?