graphics2d

Painting pixels images in Java

耗尽温柔 提交于 2019-12-03 07:25:55
Which method is the best way to create a pixel image with java. Say, I want to create a pixel image with the dimensions 200x200 which are 40.000 pixels in total. How can I create a pixel from a random color and render it at a given position on a JFrame. I tried to create a own component which just creates pixel but it seems that this is not very performant if I create such a pixel a 250.000 times with a for-loop and add each instance to a JPanels layout. class Pixel extends JComponent { @Override protected void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(getRandomColor());

Java - Creating a 2D tile map in a panel using Graphics2D Rectangles?

只愿长相守 提交于 2019-12-03 00:52:14
I'm trying to simulate a battle in a really basic program, but since this is my first time with a big program in Java I'm pretty much clueless on how to proceed. I was thinking I'd have a big 600-by-600 panel and use Graphics2D to just draw the terrain as 20x20 rectangles... Unfortunately even with several tutorials I have no idea what to do. I have 10 different types of terrain to cycle through, and 5 different landscape profiles. Basically what I want the program to do is when I select a certain profile in a combobox, it paints the landscape and the two opposing sides in the battle (though I

Rotate a moving shape around its center

穿精又带淫゛_ 提交于 2019-12-02 16:33:24
问题 I'm making a 2D game in Java where the player guides a polygon through obstacles. The polygon moves up and down and the game world scrolls left and right. I need the polygon to rotate around its center, but since it is constantly being translated the point it is being rotated around moves. Trying to translate it back to the original center, rotate it, and translate it back doesn't work. How do I get the center of the shape? Here are my motion calculations on a 2ms timer: @Override public void

ImageIO saves back to original size

和自甴很熟 提交于 2019-12-02 13:35:02
问题 I've been searching for some solutions from the internet yet I still haven't found an answer to my problem. I've been working or doing a program that would get an image file from my PC then will be edited using Java Graphics to add some text/object/etc. After that, Java ImageIO will save the newly modified image. So far, I was able to do it nicely but I got a problem about the size of the image. The original image and the modified image didn't have the same size. The original is a 2x3 inches

Creating a space for Graphics2D drawings

百般思念 提交于 2019-12-02 13:11:47
I want to draw a simple board made of Graphics2D rectangles but I also want to have one JButton under this board. I know the exact dimensions of this board in pixels and I was trying to deal with getContentPane() method and BoxLayout, like this: frame.getContentPane().add(board); frame.getContentPane().add(Box.createRigidArea(new Dimension(bWidth, bHeight))); frame.getContentPane().add(new JButton("Start")); frame.pack(); But RigidArea isn't truly invisible and it overrides my drawings. Could you please give me some tips how to make it work properly? :( I wanted just one little button and it

How to draw a line using y = mx +b in java?

↘锁芯ラ 提交于 2019-12-02 11:41:47
So I have a program that solves a system of linear equations, but that is not relevant. So what happens is that my program pass two linear equations in the form of: y = mx +b. I do not know how I would graph this using Graphics2D, I am having some trouble figuring it out. Right now I have no idea so I have no code that I could show you, but I can tell you that: That my program correctly converts Ax + By = C into y = mx + B That it would be helpful to show an example in some code possibly using the drawLine() method When you draw a line in code, you need to draw from point A to point B . (a

Post overriding the paint method of the components in java

a 夏天 提交于 2019-12-02 10:08:44
In java awt or swing when you want to change painting of some component you usually have to override the method paint(Graphics g) (in awt) or paintComponent(Graphics g) (in swing). This is usually (maybe allways - I'm not sure) done when you are creating the component for example: JPanel jPanel = new JPanel() { @Override protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g; //... my implementation of paint, some transfromations, rotation, etc } }; Imagine that you have container of components which could for example consists of some JLabels, some

Rotating a polygon in Java

六月ゝ 毕业季﹏ 提交于 2019-12-02 09:49:38
The program I am writing draws multiple stars on the screen and gives them random directions and speeds. The stars will bounce off the edges of the panel and stay inside. I need to have the stars rotating as they are moving. I have tried so many things and I cannot figure it out. Below is the code I use to draw the stars and move them. Additional Information: -the stars are in a collection called "stars" -there are two classes that I wrote, "Star" and "MyJPanel" Set the points: (in Star class) for (double current = 0; current < nPoints; current += 1) { i = (int)current; int[] X = new int

Rotating one image under another one

笑着哭i 提交于 2019-12-02 08:53:33
问题 I am currently trying to rotate an image and then drawing an image on top which isn't rotating. But whenever I use: g2d.rotate(Math.toRadians(rot), (x+15), (y+15)); every image I draw afterwards rotates as well. Is there any way I can rotate one image and not rotate the rest (gosh its really hard to explain). Here's my paint method: public void draw(Graphics2D g2d) { move(); if(bo.px==+1)rot--; if(bo.px==-1)rot++; g2d.rotate(Math.toRadians(rot), (x+15), (y+15)); g2d.drawImage(img, x, y, null)

Rotate a moving shape around its center

孤街浪徒 提交于 2019-12-02 08:24:32
I'm making a 2D game in Java where the player guides a polygon through obstacles. The polygon moves up and down and the game world scrolls left and right. I need the polygon to rotate around its center, but since it is constantly being translated the point it is being rotated around moves. Trying to translate it back to the original center, rotate it, and translate it back doesn't work. How do I get the center of the shape? Here are my motion calculations on a 2ms timer: @Override public void actionPerformed(ActionEvent e) { double theta = angleRad+90; if (up == true) { if (accelerating ==