I have a buffered image of a 4x4 checker Board (resolution 400x400) rendered on half a JPanel. Is it possible to find the coordinates of each square corner without doing it manually? I'm using absolute positioning on the JPanel and it is the only container besides the Frame
If you have a 4 x 4 checkerboard that's 400 x 400, then each square is 100 x 100 pixels.
When you construct a BufferedImage like this, you save a Rectangle for each square as you're doing the construction.
That way, when you do a mouse click later, you can use the contains method of Rectangle to determine which square was clicked.
You shouldn't use absolute positioning. If your checker board takes up half the JPanel, FlowLayout or BoxLayout works well.
Related
How can I scroll by JScrollPannel left-down to the negative coordinates area? I am developing an editor with drag and drop. For example, we have a small circle at the center of JPanel wich has a coordinates (50, 50). This circle is only drawing on the JPanel, not a java.awt.Component, thats why it can get any values of coordinates. We drag this circle left-down and its coordinates becomes (-30, 50). How can I see this circle after moving?
After dragging the circle to the right I can increase a size of my JPanel if the location of the circle will be outside of the bounds of JPannel. But what need I do to the first case?
Well, in addition I tried to draw a picture.
Check out the Drag Layout. It is a layout manager designed to allow you to drag components.
if a component is dragged to a negative location then the location of all components are translated by the negative amount to make sure the location of all components is positive
the preferred size is recalculated after each component is dragged.
I am drawing a coordinate system with weight, height =10000*10000 on a frame. It too big, my graph can't fully display. I try to draw on scroll but it's lag. I want to display my graph on a frame 10000*10000, fit with my screen computer to print screen. What should I do?
You have to scale you graph. You know, Earth is very big but it fits map that can be shown on a screen of your smart phone :)
This means that if for example you want to show rectangle of 10000*10000 on screen that has (for example) 1000 pixels you have to scale your picture 10 times.
Option 1: Set the size of the JFrame on which you are displaying the graph to the desired size.
setSize(WIDTH, HEIGHT);
where WIDTH and HEIGHT are in pixels.
OR
Option 2: Scale the drawing. That is, simply divide each dimension in the drawing by 2 to make it half the size.
I am having to draw recursively a sierpinski triangle by drawing three one-pixel squares on top of each other to make a triangle. He tells us, and im assuming this will be my recursive base case, that "The draw algorithm will take the coordinates of a square area of the screen as input. If that square is the size of a single pizel, it should call drawRect() on the object passed into paintComponent, drawing a one-pixel square somehwhere on the screen."
How do i find out if the square area of the screen is the size of a single pixel? Do i just send in the width of the area when i call the method and do width^2. If the area is 1, does that mean its 1 pixel?
The square (or probably the Rectangle) has size of one Pixel if:
java.awt.Rectangle rect;
// ....
boolean isOnePixel = (rect.width == 1 && rect.height == 1);
To simplify things we asume the witdh and height cannot be negative.
Altough Java rectangle classes permits that.
I have a JScrollPane that contains a JPanel of size 5000x5000 pixels. I want to limit the JScrollPane to only be able to show a subpart of this JPanel.
For example the rectangle defined by the two points (X,Y):
(500,500) (upper left corner)
(3000,3000) (lower right corner).
I have tried
myJScrollPane.getHorizontalScrollBar().setMinimum(500);
and
myJScrollPane.getHorizontalScrollBar().setMaximum(3000);
But it doesn't work. The setMinimum function only sets the position of the viewport on the scrollpane, setMaximum doesn't do anything.
Change your JPanel instead to have size (2500, 2500) and render desired fragment of the image. Use translate() of the Graphics in paintComponent() for this.
I'm new to android programming and have some questions. I'm trying to create a tile puzzle game where I have to move squared tiles into a grid and the tiles would automatically clip onto place. For example, I have a 3x3 squared grid with 9 position. Each position measures 100x100 pixel. If I drag a tile(also 100x100 pixel) to any of the 9 positions inside the grid, the tile would automatically clip in place inside the 100x100 pixel area even if it's a bit off. How do I do this?
x=((int)(x/100))*100
y=((int)(y/100))*100
for example if you have x=140 y=260 then
((int)(140/100))*100=100
((int)(260/100))*100=200
Because of the (int) the ((int)(140/100)) is 1 not 1.4 so the point of this is to round the number