Making the views(images) inside a linear layout dragable(android) - view

I have added Views(images) in a linear Layout and displayed it in a linear layout in this way:
public void displayCard(ArrayList<Integer> aContent) {
Point pnt = new Point();
for(int i=0;i<13;i++)
{
pnt = Suffle.getCoOrdinate(aContent.get(i));
imge=new ImageView(getApplicationContext());
LinearLayout bottomLinearLayout=(LinearLayout) findViewById(R.id.bottomlinear);
Bitmap orgbmp=BitmapFactory.decodeResource(getResources(), R.drawable.cards);
Bitmap corp=Bitmap.createBitmap(orgbmp,Integer.valueOf(1+(2 + 144) *pnt.x),1+Integer.valueOf((2 + 194) * pnt.y), 144, 194);
imge.setImageBitmap(corp);
bottomLinearLayout.addView(imge, 60, 80);
//bottomLinearLayout.addView(imge,i));
}
Now i have to make each images added in the Linear layout draggable;how can i achieve this??

Related

How do I delete or move 3D prefab clones of the same name and tag on collision?

What I am trying to achieve is when my prefabs instantiate in my spawn area when they collide then one either moves from another or is destroyed and another spawn replaces it. I tried to use tags to get the other prefab touching. I tried getting the names' prefabs. I don't know what I am doing wrong. I did just drag my game object into the prefabs folder to get prefabs. Heres my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpawnArea : MonoBehaviour
{
int counter = 0;
public int max;
//public GameObject objects;
public GameObject[] objects;
public GameObject[] objectsPrefabs;
public Vector3 size;
public Vector3 center;
int xRotation = 0;
// Start is called before the first frame update
void Start()
{
Spawn();
}
// Update is called once per frame
void Update()
{
if (counter < max)
{
Spawn();
}
}
//void Spawn()
//{
// Vector3 pos = center + new Vector3(Random.Range(-size.x / 2, size.x / 2), Random.Range(-size.y / 2, size.y / 2), Random.Range(-size.z / 2, size.z / 2));
// Instantiate(objects, pos, Quaternion.Euler(0, xRotation, 0));
// xRotation += 45;
// counter++;
//}
void Spawn()
{
objects = new GameObject[objectsPrefabs.Length];
Vector3 pos = center + new Vector3(Random.Range(-size.x / 2, size.x / 2), Random.Range(-size.y / 2, size.y / 2), Random.Range(-size.z / 2, size.z / 2));
for (int i = 0; i < objectsPrefabs.Length; i++)
{
objects[i] = Instantiate(objectsPrefabs[i], pos, Quaternion.Euler(0, xRotation, 0)) as GameObject;
}
xRotation += 45;
counter++;
}
void OnCollisionEnter(Collision collision)
{
for (int i = 0; i < objectsPrefabs.Length; i++)
{
if (collision.transform.CompareTag("spawnwall"))
{
//if (objects[i].gameObject)
//{
Destroy(objectsPrefabs[i]);
counter--;
Spawn();
//}
}
}
}
private void OnDrawGizmosSelected()
{
Gizmos.color = new Color(1, 0, 0, 0.5f);
Gizmos.DrawCube(center, size);
}
}
According to the docs on collision, you must refer to collision.gameObject in order to refer to the other object the collision is referring to. Right now you are comparing the spawnwall tag against the tag of the game object this component belongs to.
When posing the questions, it would also be nice to know the following:
1. The desired output
2. The current symptoms
I also have the following concerns:
1. Are you sure your class members are initialized with the correct data? It seems to me that they are all either starting with C# default values.
2. Do you want the objects inside the area to do what you described when they (a) touch each other or (b) when they touch the area boundaries.
The way your current code is written makes me thinks that you have a bunch of objects inside a rectangular area. Then, when the objects collide with the boundaries of the rectangular area, they should be destroyed.

Is RadioButton with cross instead of bullet possible? - iText 7.1.0 for Java

Does anybody know, if it's possible to have a cross (checkbox like) instead of the usual bullet? Did'nt find anything. Thanks a lot! Dirk
To produce this result, I adopted an iText sample
package jumpstart;
import com.itextpdf.forms.PdfAcroForm;
import com.itextpdf.forms.fields.PdfButtonFormField;
import com.itextpdf.forms.fields.PdfFormField;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.io.font.PdfEncodings;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.property.TextAlignment;
import java.awt.Desktop;
import java.io.File;
public class Problem8 {
public static void main(String[] args) throws Exception {
PdfWriter writer = new PdfWriter("problem8.pdf");
PdfDocument pdf = new PdfDocument(writer);
Document doc = new Document(pdf);
PdfFont font = PdfFontFactory.createFont("src/main/resources/fonts/arialuni.ttf", PdfEncodings.IDENTITY_H, true);
PdfAcroForm form = PdfAcroForm.getAcroForm(pdf, true);
String[] languages = { "Dutch", "English", "French" };
Rectangle rect;
Paragraph para;
PdfButtonFormField radioGroup = PdfFormField.createRadioGroup(pdf, "Language", "");
pdf.addNewPage();
for (int i = 1; i <= languages.length; i++) {
rect = new Rectangle(40, 800 - i * 30, 20, 20);
para = new Paragraph(languages[i - 1]);
para.setFont(font);
para.setFontSize(18);
PdfFormField.createRadioButton(pdf, rect, radioGroup, languages[i - 1]);
doc.showTextAligned(para, 70, 800 - i * 30, TextAlignment.LEFT);
}
form.addField(radioGroup);
doc.close();
Desktop.getDesktop().open(new File("problem8.pdf"));
}
}
That's perfectly possible. And one of the FAQ apparently.
https://developers.itextpdf.com/de/node/3095
If you want to replace the appearance, then you have to replace the stream that draws the rectangle and the cross. In IText 7 we added some popular appearances, so you can easily use them while creating elements like:
createCheckBox(PdfDocument doc, Rectangle rect, String name, String value, int checkType)
Where checkType can be: TYPE_CHECK, TYPE_CIRCLE, TYPE_CROSS, TYPE_DIAMOND, TYPE_SQUARE, TYPE_STAR. Or you can also change the appearance of existing element using:
setCheckType(int checkType).
Since the example linked by Joris doesn't immediatly apply to radiobuttons (looks like their on-appearance is hard-coded to be a circle by default), I wrote a simple example that shows you how to override that appearance after creating the button-object bu before adding the entire radio group to the form fields:
public void createPdf(String dest) throws IOException, java.io.IOException{
float width = 20;
float height = 20;
List<PdfFormField> radiobuttons = new ArrayList<>();
PdfWriter writer = new PdfWriter(dest);
PdfDocument pdf = new PdfDocument(writer);
Document doc = new Document(pdf);
//PdfFont font = PdfFontFactory.createFont("src/main/resources/fonts/arialuni.ttf", PdfEncodings.IDENTITY_H, true);
PdfAcroForm form = PdfAcroForm.getAcroForm(pdf, true);
String[] languages = { "Dutch", "English", "French" };
Rectangle rect;
Paragraph para;
PdfButtonFormField radioGroup = PdfFormField.createRadioGroup(pdf, "Language", "");
pdf.addNewPage();
for (int i = 1; i <= languages.length; i++) {
rect = new Rectangle(40, 800 - i * 30, width, height);
para = new Paragraph(languages[i - 1]);
//para.setFont(font);
para.setFontSize(18);
PdfFormField radioButton = PdfFormField.createRadioButton(pdf, rect, radioGroup, languages[i - 1]);
createAndSetCircleGraphicForOn(radioButton,pdf,width,height, languages[i-1]);
radiobuttons.add(radioButton);
doc.showTextAligned(para, 70, 800 - i * 30, TextAlignment.LEFT);
}
form.addField(radioGroup);
doc.close();
}
private static void createAndSetCircleGraphicForOn(PdfFormField radiobutton, PdfDocument pdfDoc, float width, float height, String value){
PdfStream streamOn = (PdfStream) new PdfStream().makeIndirect(pdfDoc);
PdfCanvas canvasOn = new PdfCanvas(streamOn, new PdfResources(), pdfDoc);
Rectangle rect = new Rectangle(0, 0, width, height);
PdfFormXObject xObjectOn = new PdfFormXObject(rect);
drawRadioFieldOnWithCross(canvasOn, ColorConstants.BLACK,1f, width, height, true);
PdfStream streamOff = (PdfStream) new PdfStream().makeIndirect(pdfDoc);
PdfWidgetAnnotation widget = radiobutton.getWidgets().get(0);
xObjectOn.getPdfObject().getOutputStream().writeBytes(streamOn.getBytes());
widget.setNormalAppearance(new PdfDictionary());
widget.getNormalAppearanceObject().put(new PdfName(value), xObjectOn.getPdfObject());
}
private static void drawRadioFieldOnWithCross(PdfCanvas canvas,Color strokeColor, float strokeWidth, float width, float height, boolean on) {
canvas.saveState();
if (on) {
canvas.
setStrokeColor(strokeColor)
.setLineWidth(strokeWidth)
//bottom left to top right
.moveTo(0,0)
.lineTo(width,height)
.stroke()
//Top left to bottom right
.moveTo(0,height)
.lineTo(width,0)
.stroke();
}
canvas.restoreState();
}
It works by replacing the appearance stream for the value-state with a PdfStreamcontaining the drawing instructions for a cross instead of a circle.
It's not possible to change the bullet for a Radio Button completely.The solution of Samuel Huylebroeck is working only when interacting with the Radio Button Group:Then it looks like this after clicking on "Dutch":
But after leaving the Radio Button Group, for example by clicking on another part of the page it looks like this:
This is the standard appearance of a Radio Button which cannot be altered. That's a pity and it would be very interesting to know why that is so.It's confusing that print preview is showing the cross again:

Toggle function not working in Processing (ControlP5)

I just made my image generator work with PNG files. For now, it's divided into 3 categories (backgrounds, objects & texts). These are now all combined, and with every mouse click it randomises these PNGs.
I made three toggles, where you could to choose to show either the background and the objects on top, all of them, or all separate. Whenever I run the sketch, it shows the "grey" background, but when I use the toggles, it doesn't show anything, or shows a flickering image, where the mouse-click can't be used to go to the next image. I can't seem to find the problem. Hopefully, you can help. :)
import controlP5.*;
boolean showBackground = false;
boolean showObjects = false;
boolean showGrids = false;
ControlP5 cp5;
PImage[] myImageArray = new PImage[8];
PImage[] myImageArray2 = new PImage[15];
PImage[] myImageArray3 = new PImage[15];
void setup() {
size(1436, 847);
background(211, 211, 211);
for (int i=0; i<myImageArray.length; i++) {
myImageArray[i] = loadImage ( "o" + i + ".png");
myImageArray2[i] = loadImage ( "g" + i + ".png");
myImageArray3[i] = loadImage( "b" + i + ".jpg");
cp5 = new ControlP5(this);
// create a toggle and change the default look to a (on/off) switch look
cp5.addToggle("showBackground")
.setPosition(40, 250)
.setSize(50, 20)
.setValue(true)
.setMode(ControlP5.SWITCH);
cp5.addToggle("showObjects")
.setPosition(40, 400)
.setSize(50, 20)
.setValue(true)
.setMode(ControlP5.SWITCH);
cp5.addToggle("showGrid")
.setPosition(40, 600)
.setSize(50, 20)
.setValue(true)
.setMode(ControlP5.SWITCH);
}
display();
}
void display() {
image(myImageArray3[(int)random(myImageArray.length)], 0, 0, 1436, 847); // b
image(myImageArray2[(int)random(myImageArray.length)], 0, 0, 1436, 847); // g
image(myImageArray[(int)random(myImageArray.length)], 0, 0, 1436, 847); // o
}
void mousePressed() {
display();
}
void draw() {
pushMatrix();
if (showBackground==false) {
image(myImageArray3[(int)random(myImageArray.length)], 0, 0, 1436, 847); // b
} else {
background(211, 211, 211);
}
if (showGrids==false) {
image(myImageArray2[(int)random(myImageArray.length)], 0, 0, 1436, 847); // g
} else {
background(211, 211, 211);
}
if (showObjects==false) {
image(myImageArray[(int)random(myImageArray.length)], 0, 0, 1436, 847); // o
} else {
background(211, 211, 211);
}
popMatrix();
}
Here are a couple of things where the logic your wrote in your code might not match what you had in mind:
When you call display() on mouse it renders those 3 images once (also it will be different images within those since it's using a randomised index). Similarly in draw(), when an does get picked to be rendered, frames will be flickering fast as a random index is generated multiple times per second(each frame). You may want to randomise indices in a different event (e.g. mouse or key press) and store this value in a variable you can re-use.
the conditions you use in draw(): you probably meant to check if the values are true(toggled enabled/turned on in controlP5) ? (e.g. e.g. if (showBackground==true) and initialise the toggles with false, instead of true?)
a big one: in draw() , after each condition(showBackground,showGrids,showObjects), if it's false, you're clearing the the whole frame (so a previous image would be erased)
you have 3 arrays, but you use the size of the first(myImageArray.length) only, which means, even though you may have more images for myImageArray2 and myImageArray3, you're not loading, nor displaying them.
The third grid is labeled "showGrid" when it should be "showGrids": if you aren't consistent with the toggle labels and variable names, toggles won't update the variable names.
you should use more descriptive names for the arrays: it will make it easier to scan/follow your code on the long run.
there's no need to add toggles multiple times in the for loop where you load images: once will do.
Here's what I mean:
import controlP5.*;
boolean showBackground = false;
boolean showObjects = false;
boolean showGrids = false;
ControlP5 cp5;
PImage[] objects = new PImage[8];
PImage[] grids = new PImage[15];
PImage[] backgrounds = new PImage[15];
int currentImage = 0;
void setup() {
size(1436, 847);
//load objects
for (int i=0; i<objects.length; i++) {
objects[i] = loadImage ( "o" + i + ".png");
}
//load grids
for(int i = 0 ; i < grids.length; i++){
grids[i] = loadImage ( "g" + i + ".png");
}
//load backgrounds
for(int i = 0 ; i < grids.length; i++){
backgrounds[i] = loadImage( "b" + i + ".jpg");
}
//setup UI
cp5 = new ControlP5(this);
// create a toggle and change the default look to a (on/off) switch look
cp5.addToggle("showBackground")
.setPosition(40, 250)
.setSize(50, 20)
.setValue(false)
.setMode(ControlP5.SWITCH);
cp5.addToggle("showObjects")
.setPosition(40, 400)
.setSize(50, 20)
.setValue(false)
.setMode(ControlP5.SWITCH);
cp5.addToggle("showGrids")
.setPosition(40, 600)
.setSize(50, 20)
.setValue(false)
.setMode(ControlP5.SWITCH);
}
void mousePressed() {
//go to next image index
currentImage = currentImage + 1;
//check if the incremented index is still valid, otherwise, reset it to 0 (so it doesn't go out of bounds)
if (currentImage >= objects.length) {
currentImage = 0;
}
}
void draw() {
//clear current frame
background(211);//for gray scale value you can just use one value: the brightness level :)
if (showBackground==true) {
image(backgrounds[currentImage], 0, 0, 1436, 847); // b
}
if (showGrids==true) {
image(grids[currentImage], 0, 0, 1436, 847); // g
}
if (showObjects==true) {
image(objects[currentImage], 0, 0, 1436, 847); // o
}
}
Note that currently the same index is used for all 3 arrays.
You may want to add a separate index variable for each array (e.g. currentObjectIndex, currentBackgroundIndex, currentGridIndex) that you can increment independently of each other.
I recommend having a bit more patience and double checking your code first.
Visualise what each line of code will do, then check if it actually does what you expect it to do. Either you will learn something new or improve your logic.
Also, if mentally joggling 3 arrays is tricky (and it can be), go one step back: try your logic with one array only until you get the hang of it, then move on.
A step backwards is sometimes a step forward when you're going in the wrong direction.
As creative as you'd like to be with Processing, bare in mind, the interface to plugging your ideas to it is still a series of one instruction at a time, each precisely tuned to do exactly what you want it to do. There's room for fun, but unfortunately you need to get past the boring parts first

Position of GUI.Label in Unity3D

I have a huge (but probably simple) problem. I want to display score. I'm using GUI.Label. When I scale GUI.Label. I want to set position of text. I work on Android, so there are many resolutions and aspect ratios, and I have problem with giving it a precise position.
The "GAME OVER" text is sprite. I tried to set position as half of screen height. It works on some devices, but it doesn't work on devices with highest resolution (1280x720). When I set 400px from top it works, but it doesn't make any sense. Is there any tip for this? I want to display score like here.
My code:
private string labelText;
public Font fontxd;
public Vector2 sizexde;
/*[HideInInspector]*/public GUIStyle styl;
public int p,hp;
static GameObject g1;
static punkt playerScript;
static float virtualWidth = 640.0f;
static float virtualHeight = 400.0f;
static Rect rece;
static Vector3 v3;
public Matrix4x4 matrixs;
static float guiRatio=Screen.width/640;
static float XD=0;
void Start() //1280x720
{
if (Screen.width > Screen.height) {
XD=Screen.width/2;
}
else{//if( Screen.height>Screen.width ){
XD=Screen.height/2;
}
rece=new Rect(10,XD, Screen.width,1);
v3 = new Vector3 (Screen.width / virtualWidth, Screen.width / virtualWidth , 1.0f);
matrixs = Matrix4x4.TRS (Vector3.zero, Quaternion.identity, v3);
g1 = GameObject.Find("XDXDX2");
playerScript = g1.GetComponent<punkt>();
}
void OnGUI()
{
p = playerScript.punkty;
hp = playerScript.hpunkty;
labelText = "Height: " + Screen.height+"-"+Screen.height/2 + "\nWidth: " + Screen.width + "-"+Screen.width/2 ;
GUI.matrix = matrixs;
styl.normal.textColor = Color.black;
styl.font = fontxd;
styl.fontSize = 70;
GUI.Label (rece, labelText, styl);
}
GameObject > CreateOther > GUIText
Edit settings in inspect as shown below:
Now your label's position should be resolution independent.

Create more than one window of a single sketch in Processing

How to create more than one window of a single sketch in Processing?
Actually I want to detect and track a particular color (through webcam) in one window and display the detected co-ordinates as a point in another window.Till now I'm able to display the points in the same window where detecting it.But I want to split it into two different windows.
You need to create a new frame and a new PApplet... here's a sample sketch:
import javax.swing.*;
SecondApplet s;
void setup() {
size(640, 480);
PFrame f = new PFrame(width, height);
frame.setTitle("first window");
f.setTitle("second window");
fill(0);
}
void draw() {
background(255);
ellipse(mouseX, mouseY, 10, 10);
s.setGhostCursor(mouseX, mouseY);
}
public class PFrame extends JFrame {
public PFrame(int width, int height) {
setBounds(100, 100, width, height);
s = new SecondApplet();
add(s);
s.init();
show();
}
}
public class SecondApplet extends PApplet {
int ghostX, ghostY;
public void setup() {
background(0);
noStroke();
}
public void draw() {
background(50);
fill(255);
ellipse(mouseX, mouseY, 10, 10);
fill(0);
ellipse(ghostX, ghostY, 10, 10);
}
public void setGhostCursor(int ghostX, int ghostY) {
this.ghostX = ghostX;
this.ghostY = ghostY;
}
}
One option might be to create a sketch twice the size of your original window and just offset the detected coordinates by half the sketch's size.
Here's a very rough code snippet (assumming blob will be a detected color blob):
int camWidth = 320;
int camHeight = 240;
Capture cam;
void setup(){
size(camWidth * 2,camHeight);
//init cam/opencv/etc.
}
void draw(){
//update cam and get data
image(cam,0,0);
//draw
rect(camWidth+blob.x,blob.y,blob.width,blob.height);
}
To be honest, it might be easier to overlay the tracked information. For example, if you're doing color tracking, just display the outlines of the bounding box of the tracked area.
If you really really want to display another window, you can use a JPanel.
Have a look at this answer for a running code example.
I would recommend using G4P, a GUI library for Processing that has some functionality built in for handling multiple windows. I have used this before with a webcam and it worked well. It comes with a GWindow object that will spawn a window easily. There is a short tutorial on the website that explains the basics.
I've included some old code that I have that will show you the basic idea. What is happening in the code is that I make two GWindows and send them each a PImage to display: one gets a webcam image and the other an effected image. The way you do this is to augment the GWinData object to also include the data you would like to pass to the windows. Instead of making one specific object for each window I just made one object with the two PImages in it. Each GWindow gets its own draw loop (at the bottom of the example) where it loads the PImage from the overridden GWinData object and displays it. In the main draw loop I read the webcam and then process it to create the two images and then store them into the GWinData object.
Hopefully that gives you enough to get started.
import guicomponents.*;
import processing.video.*;
private GWindow window;
private GWindow window2;
Capture video;
PImage sorted;
PImage imgdif; // image with pixel thresholding
MyWinData data;
void setup(){
size(640, 480,P2D); // Change size to 320 x 240 if too slow at 640 x 480
// Uses the default video input, see the reference if this causes an error
video = new Capture(this, 640, 480, 24);
numPixels = video.width * video.height;
data = new MyWinData();
window = new GWindow(this, "TEST", 0,0, 640,480, true, P2D);
window.isAlwaysOnTop();
window.addData(data);
window.addDrawHandler(this, "Window1draw");
window2 = new GWindow(this, "TEST", 640,0 , 640,480, true, P2D);
window2.isAlwaysOnTop();
window2.addData(data);
window2.addDrawHandler(this, "Window2draw");
loadColors("64rev.csv");
colorlength = mycolors.length;
distances = new float[colorlength];
noCursor();
}
void draw()
{
if (video.available())
{
background(0);
video.read();
image(video,0,0);
loadPixels();
imgdif = get(); // clones the last image drawn to the screen v1.1
sorted = get();
/// Removed a lot of code here that did the processing
// hand data to our data class to pass to other windows
data.sortedimage = sorted;
data.difimage = imgdif;
}
}
class MyWinData extends GWinData {
public PImage sortedimage;
public PImage difimage;
MyWinData(){
sortedimage = createImage(640,480,RGB);
difimage = createImage(640,480,RGB);
}
}
public void Window1draw(GWinApplet a, GWinData d){
MyWinData data = (MyWinData) d;
a.image(data.sortedimage, 0,0);
}
public void Window2draw(GWinApplet a, GWinData d){
MyWinData data = (MyWinData) d;
a.image(data.difimage,0,0);
}

Resources