java-2d

How to get an instance of java.awt.Graphics in a java.applet.Applet?

可紊 提交于 2019-12-12 06:36:21
问题 I am currently coding the game of Snake for my computer science class. However, I ran into a lot of difficulties (since I am the only one in my block that is working alone) and I had to restart. Right now I am just trying to make an array of objects (named Snake) from the second class (Segment) I made. I get a NullPointerException on line 26: g.fillRect(xValue, yValue, size, size); Where should I get an instance of Graphics to call this method on? import java.awt.*; import java.applet.*;

Java2D Image Rotation Issue

て烟熏妆下的殇ゞ 提交于 2019-12-12 06:25:09
问题 I am using following code to rotate an given image. public int getImageWidth(BufferedImage img) { if (rotate == Rotate.UPSIDE_DOWN || rotate == Rotate.ABOUT_CENTER) return img.getWidth(); else return img.getHeight(); } public int getImageHeight(BufferedImage img) { if (rotate == Rotate.UPSIDE_DOWN || rotate == Rotate.ABOUT_CENTER) return img.getHeight(); else return img.getWidth(); } This is the Rotation enums public enum Rotate { DOWN, UP, UPSIDE_DOWN, ABOUT_CENTER; } Actual rotation method

How to move JFrame shape

冷暖自知 提交于 2019-12-12 02:58:28
问题 I'm trying to develop a simple game. The games is about the shapes. Shapes will move and we'll catch by mouse. I have already created a oval and given size of oval graphic. But I cannot move this shape repeatedly. I think I need to use timer class. I have been trying 2 hours myself but I didnt do yet. The code; import java.awt.Color; import java.awt.Container; import java.awt.Dimension; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import

Collision detection with complex shapes

别来无恙 提交于 2019-12-12 02:57:18
问题 I am wanting to make a game that has each level loaded from an image. I want to draw up the whole level in Photoshop, and then set it as the background and allow the player to walk over it. I want another invisible image to go over top which will be black in all places that I want to collide with. The reason I don't want to use tiles, which are much easier with rectangle collision and such, is because there will be complex corners and not everything will be rectangle. Is this a good idea, and

Java Change shape of JPanel

冷暖自知 提交于 2019-12-12 02:05:56
问题 I'm trying to do something which I'd think would be fairly easy but can't find a straight forward answer to. Basically I want to change a JPanel's default shape to a circular shape (or any other shape other than a rectangle). 回答1: You will need to provide your own custom painting routines. The other problem you will have is getting the layout managers to work with it, but you can supply your own insets to provided an area within the panel that can safely used You'll also want to make the

How to find custom shape speicific area?

*爱你&永不变心* 提交于 2019-12-11 19:45:18
问题 please , see following image, here you can see blue rectangle is custom shape bounds and custom shape is shoe , i want to find area of a portion written in image and i want that area in form of rectangle do is there any path iterator concept ? Note custom shape i derived from image of the same size. 回答1: I would do it like this: 1.create table for all bounding box-rect perimeter lines each value in it will represent the empty space length form border line to shape something like this: the

Draw arrow on top left of scrollpane

て烟熏妆下的殇ゞ 提交于 2019-12-11 15:24:43
问题 I have a JPanel within a JScrollPane. In my program, the JPanel shows an image of map and it shows the current location of a vehicle. My program receives the location and current direction of the vehicle. I need to show this on the map. I'm having a problem showing the direction of the vehicle. I want to show an arrow on the top left of the map's visible area to show the direction. The following is the code that I have tried. But it throws an exception when I scroll down. Exception in thread

Java How to add String below an image and resizing it?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-11 13:55:55
问题 I am trying to add a title string below a png image. The problem is that there is no space for the text and I have to add some white space. Im not sure how to do this. This is what I have done so far: BufferedImage image = ImageIO.read(new File("output.png")); Graphics g = image.getGraphics(); g.setFont(g.getFont().deriveFont(30f)); g.setColor(Color.BLACK); g.drawString("Hello World!", 100, 350); g.dispose(); ImageIO.write(image, "png", new File("test.png")); 回答1: Why not simply add the

How to Rotate AWT rectangle in JAVA?

我的梦境 提交于 2019-12-11 12:03:17
问题 I am creating a small java 2D game and i want to know if there is any way to rotate AWT rectangle AffineTransform origXform = g2d.getTransform(); AffineTransform newXform = (AffineTransform) origXform.clone(); newXform.rotate(angle, pivotX, pivotY); // pivotX ,pivotY is the starting point of the hand image g2d.setTransform(newXform); Rectangle arm = new Rectangle(bowX + 5, bowY + 55, 60, 5); g2d.drawImage(playerBowReadyImg, bowX, bowY, null); //hand image on the above code i simply draw the

How to draw a butterfly curve as accurate as possible?

六月ゝ 毕业季﹏ 提交于 2019-12-11 10:57:31
问题 I am trying to draw a butterfly curve using Java . Here's the parametric equation for the mentioned curve: From what I remember from the college, the way to draw a parametric equation with Java is the next: public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; g2.translate(300,300); int x1,y1; int x0 = 0; int y0 = (int)(Math.E-2); //for x = 0, we get y = Math.E - 2 int nPoints = 1000; g2.scale(30,-30); for(int i=0;i<nPoints;i++) { double t= 12*i*Math