Sorry to bother with simple questions but as I learn JAVA I am having a few issues and the first one of those is with programing a GUI. I am first forming a JFrame and then placing JPanels within it. basically with the code below looking to make two boxes that outline JPanels, one on the top and one along the left side of the jframe. These will later be used as containers for other elements but just trying to get the outlines to show first. I did a bit of research and added a repaint line for the second panel but not the answer code below, any help would be appreciated. Thanks
import javax.swing.*;
import java.awt.*;
public class frmTest {
public static void main(String main[]){
JFrame test = new JFrame();
test.setSize(1300,800);
test.setVisible(true);
JPanel panel1 = new JPanel();
panel1.setSize(1300,100);
panel1.setLocation(0, 0);
panel1.setBorder(BorderFactory.createLineBorder(Color.black));
JPanel panel2 = new JPanel();
panel2.setSize(200,700);
panel2.setLocation(0,100);
panel2.setBorder(BorderFactory.createLineBorder(Color.black));
test.add(panel1);
test.add(panel2);
panel1.setVisible(true);
panel2.setVisible(true);
panel2.repaint();
}
}
It sounds like you need to use BorderLayout. Here's the tutorial.. Good luck with learning swing.
Related
When I was creating my first tiled map creator in libGDX, I noticed a very strange bug. I creating grid of objects like this:
private static final int GRID_WIDTH=2400;
private static final int GRID_HEIGHT=2400;
private static final int CELL_SIZE=60;
so you can see there are 2400/60x2400/60 objects or cells. I am creating my map like this:
private void createMap(){
cells = new Cell[GRID_WIDTH/CELL_SIZE][GRID_HEIGHT/CELL_SIZE];
for(int i=0;i<GRID_WIDTH/CELL_SIZE;++i){
for(int j=0;j<GRID_HEIGHT/CELL_SIZE;++j){
cells[i][j]=new Cell(textures[0],i*CELL_SIZE,j*CELL_SIZE);
}
}
}
I also have coordinates for my debug in the screen so I know where they started to disappear. Y coordinate is ok there are from 0 to 2400, but on the X they started to disappear at 1500. When I start to draw there some texture every column will be visible to that texture for example (when I start to write texture at x=2100 every disappeared column will be visible to 2100) and when I will delete that texture every column will disappear again to 1500. So the objects are there but they are not visible. It is so annoying does anyone know about this bug?
As you can see coordinates are at the bottom left this is at the beginning:
and this is when I will add there some texture
[Edited] Code with camera:
private float x=GRID_WIDTH/2,y=GRID_HEIGHT/2;
#Override
public void render(float delta) {
batch = new SpriteBatch();
camera=new OrthographicCamera(CAM_WIDTH,CAM_HEIGHT);
viewPos = new Vector3();
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
viewPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
camera.unproject(viewPos);
batch.begin();
if(Gdx.input.isKeyPressed(Input.Keys.RIGHT) || Gdx.input.isKeyPressed(Input.Keys.D))
x+=SPEED*Gdx.graphics.getDeltaTime();
if(Gdx.input.isKeyPressed(Input.Keys.LEFT) || Gdx.input.isKeyPressed(Input.Keys.A))
x-=SPEED*Gdx.graphics.getDeltaTime();
if(Gdx.input.isKeyPressed(Input.Keys.UP) || Gdx.input.isKeyPressed(Input.Keys.W))
y+=SPEED*Gdx.graphics.getDeltaTime();
if(Gdx.input.isKeyPressed(Input.Keys.DOWN) || Gdx.input.isKeyPressed(Input.Keys.S))
y-=SPEED*Gdx.graphics.getDeltaTime();
stage.act(Gdx.graphics.getDeltaTime());
stage.draw();
camera.position.set(x,y,0);
camera.update();
batch.setProjectionMatrix(camera.combined);
batch.end();
}
The camera is correct. The problem is the batch.begin() and batch.end(). As you might know you cannot do batch.begin() and then shaperenderer.begin() directly after each others without closing one of them. Reason for this I am not 100% about. stage works similar. This means we have to close the batch before drawing the stage
batch.end();
stage.draw();
batch.begin();
// draw your batch stuff here
Also it's terrible to do this
batch = new SpriteBatch();
camera=new OrthographicCamera(CAM_WIDTH,CAM_HEIGHT);
inside the render method. Instead, put it into the create() method or some of your own initialize method. The important thing is to not create a new SpriteBatch every frame as the batch isn't collected by the GC. So you have to manually dispose it using batch.dispose() or it will leak so much memory your RAM will be gone in no time.
I hope this helped you out, good luck.
I have an animated Sprite that is drawn on the screen when I press the button. However, I want to the animation to start after 5 seconds. Technically, the ver first PNG in the "animation set" is shown and the animation starts after 5 seconds.
I have tried to used the DelayModifier as follows, but without luck:
mySprite.registerEntityModifier(new DelayModifier(500)); //doesn't work
I would appreciate your input.
I found the solution to my problem from this tutorial: http://www.andengine.org/forums/tutorials/using-timer-s-sprite-spawn-example-t463.html
The idea was to use a TimeHandler and not a DelayModifier.
actually , you can do it with a delaymodifier too , like this
DelayModifier dMod = new DelayModifier(5f){
#override
public void onModifierFinished(IModifier arg0, IEntity arg1) {
mySprite.animate(300);
}
}
mySprite.registerEntityModifier(dMod);
I changed image of cursor. I am trying to change size of cursor image but i couldnt. How can i change it's size? Thanks in advance.
You can change the cursor by using one of the predefined cursor types or supplying an image to have your own cursor, but the system will automatically resize your supplied image based upon the cursor size support of the underlying platform. See the documentation for ImageCursor.getBestSize() and ImageCursor.chooseBestCursor() for details of the algorithm used to choose determine the actual cursor size used.
import javafx.application.Application;
import javafx.scene.*;
import javafx.scene.layout.*;
import javafx.stage.*;
public class ResizableCursor extends Application {
public static void main(String[] args) { launch(args); }
#Override public void start(Stage stage) {
StackPane layout = new StackPane();
layout.setCursor(Cursor.cursor("http://icons.iconarchive.com/icons/artua/star-wars/256/Death-Star-icon.png"));
stage.setScene(new Scene(layout, 200, 200));
stage.show();
}
}
If you want to have an arbitrarily sized node which moves with the mouse and looks and acts like a cursor (but is not a cursor), you can set the cursor to Cursor.NONE, then translate a node to follow the mouse location (as is done in this example when you click on a circle to move it around). Using this strategy it will seem as though the user has a custom cursor of arbitrary size, even though they don't.
Copy of relevant documentation for ImageCursor.getBestSize():
Gets the supported cursor size that is closest to the specified
preferred size. A value of (0,0) is returned if the platform does not
support custom cursors.
Note: if an image is used whose dimensions don't match a supported
size (as returned by this method), the implementation will resize the
image to a supported size. This may result in a loss of quality.
Note: These values can vary between operating systems, graphics cards
and screen resolution, but at the time of this writing, a sample
Windows Vista machine returned 32x32 for all requested sizes, while
sample Mac and Linux machines returned the requested size up to a
maximum of 64x64. Applications should provide a 32x32 cursor, which
will work well on all platforms, and may optionally wish to provide a
64x64 cursor for those platforms on which it is supported.
VBox vBoxMainLayout = new VBox();
Scene scene = new Scene(vBoxMainLayout);
Image img = newImage(getClass().getResourceAsStream("image.png"));
ImageCursor cursor = new ImageCursor(img, 30, 30);
scene.setCursor(cursor);
import java.awt.Toolkit;
import javafx.scene.Cursor;
import javafx.scene.image.Image;
public class ColorChooser extends Cursor
{
public override function impl_getAWTCursor(): java.awt.Cursor {
def url = "{__DIR__}eyedropper.png";
var toolkit = Toolkit.getDefaultToolkit();
var image = Image{width:32 height:32 url:url}.bufferedImage;
var hotspot = new java.awt.Point(0,15);
var cursor = toolkit.createCustomCursor(image, hotspot "colorpicker");
return cursor;
}
}
I am facing an issue with editor and its contents layout. Its following: there is an editor with print preview functionality, main part of the editor is taken by paper clips PrintPreview component, on the left side there is a composite with some buttons (see screenshot 1).
As you can see PrintPreview is located inside ScrolledComposite.
The problem is: when I am resizing window or views under editor, the bottom scrollbar moves under views and thus disappears. There is no way to scroll horizontally afterwards (no editor scrollbar appears). see screenshot2 (bottom scrollbar is getting hidden).
It starting to get hidden, when editor height is less then height of the right composite ( there are more widgets below the last button, I've just erased them)
If I am shrinking the window, then right composite never loses its width and always visible (see screenshot 3 - It does not allow to resize more than that).
The composites are created in the following way:
#Override
public void createPartControl(final Composite parent) {
GridLayout gridLayout = new GridLayout(2, false);
parent.setLayout(gridLayout);
// Scroll area for the print area
scroll = new ScrolledComposite(parent, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
final GridData gridData = new GridData(GridData.FILL_BOTH);
gridData.grabExcessHorizontalSpace = true;
gridData.grabExcessVerticalSpace = true;
gridData.widthHint = 1;
gridData.heightHint = 1;
scroll.setLayoutData(gridData);
scroll.setExpandHorizontal(true);
scroll.setExpandVertical(true);
// Button area
final Composite buttonArea = new Composite(parent, SWT.FLAT);
final GridData layoutData = new GridData(SWT.FILL, SWT.TOP, false, false);
buttonArea.setLayoutData(layoutData);
gridLayout = new GridLayout(1, false);
buttonArea.setLayout(gridLayout);
The question is: how not to prevent disappearing of the bottom scrollbar? Can I have the same behavior as width the width of the right composite - It not possible to resize less then right composite height? Or how can I show scrollbar on the whole editor if some minimum height limit is exceeded?
I've tried different things on different composites, like setting minimumHeight and heightHint, putting buttonsArea into the scrolledcomposite (just for fun) etc. But nothing have helped me.
Does anybody knows fast solution? Thank you.
See this SO post: Prevent SWT scrolledComposite from eating part of it's children
You need to listen for width changes on the content composite
(mParent), compute the minimum height again given the new content
width, and call setMinHeight() on the scrolled composite with new
height.
If the above post is not as useful as it seems then share some minimal compiling and executable code to replicate the problem. Also, mention the eclipse/swt version, OS and JDK version, it helps in analyzing the problem.
Demo code for resize event and output !!
Code :
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
public class ScrolledCompositeTest
{
public static void main (String [] args)
{
Display display = new Display ();
Shell shell = new Shell (display);
shell.setLayout(new FillLayout());
GridLayout layout = new GridLayout();
layout.numColumns = 4;
// set the minimum width and height of the scrolled content - method 2
final ScrolledComposite sc2 = new ScrolledComposite(shell, SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
sc2.setExpandHorizontal(true);
sc2.setExpandVertical(true);
final Composite c2 = new Composite(sc2, SWT.NONE);
sc2.setContent(c2);
layout = new GridLayout();
layout.numColumns = 7;
c2.setLayout(layout);
Button b2 = new Button (c2, SWT.PUSH);
b2.setText("first button");
sc2.setMinSize(c2.computeSize(SWT.DEFAULT, SWT.DEFAULT));
Button add = new Button (shell, SWT.PUSH);
add.setText("add children");
final int[] index = new int[]{0};
add.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
index[0]++;
Button button = new Button(c2, SWT.PUSH);
button.setText("button "+index[0]);
sc2.setMinSize(c2.computeSize(SWT.DEFAULT, SWT.DEFAULT));
c2.layout();
}
});
shell.open ();
while (!shell.isDisposed ()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}
}
Output:
Actual Size
Vertical Resize
Horizontal Resize
I want to make speech bubbles but there is a problem adjusting size of the bubbles in UITextView.
is there a way to increase the size of the Textview automatically depending on the length of the
text by using UITextView?
Okay I found a better way of doing what I was suggesting. Rather then deal with the UIView and resizing depending on the text inside it. I just made the UITextView have a rounded edge which now looks like a panel. Perfect. Hope this helps someone!
If your interested in the code
- (void)drawRect:(CGRect)rect
{
CGRect frame = self.frame;
frame.size.height = self.contentSize.height;
self.frame = frame; // Drawing code
self.clipsToBounds = YES;
self.layer.cornerRadius = 10.0f;
}
I think I understand what your getting at. I had this problem the other day. This bit of code will adjust the UITextView contents. Create a new class and call this into it via a new class. If you want to adjust the View well I'm still working on that :-P
Hope this helps
- (void)drawRect:(CGRect)rect
{
CGRect frame = self.frame;
frame.size.height = self.contentSize.height;
self.frame = frame; // Drawing code
}