graphics2d

Java - Change the color of some squares created with Graphics2D

你说的曾经没有我的故事 提交于 2019-12-12 17:20:43
问题 I just want to create a simple game with 100 x 100 square, each square is 5 pixels. I created a class: public class Draw extends JComponent{ private List<Graphics2D> recList = new ArrayList<Graphics2D>(); public void paint(Graphics g) { //THIS TO SET (0,0) PANEL START AT BOTTOM LEFT Graphics2D g2 = (Graphics2D)g; AffineTransform at = g2.getTransform(); at.translate(0, getHeight()); at.scale(1, -1); g2.setTransform(at); //THIS TO DRAW ALL THE SQUARES for (int i = 0;i<100;i++){ for (int j=0;j

Creating a JLabel with a Gradient

江枫思渺然 提交于 2019-12-12 15:01:16
问题 I'm new to Java and I'm trying to create a heading using a JLabel and for its fill to be a gradient. I cannot get it to work and I've been trying for a while. I've been grabbing bits of come from here and other websites and cannot seem to make this work, nor make sense of other peoples more complex code that does work. Here are my two classes so far: import java.awt.Color; import java.awt.Component; import java.awt.Dimension; import java.awt.Font; import java.awt.Graphics; import java.awt

Java Graphics2D - draw an image with gradient opacity

限于喜欢 提交于 2019-12-12 10:54:27
问题 Using Graphics2d , I am trying to draw a BufferedImage on top of a background image. At an arbitrary point in this image, I would like to "cut a circular hole" in the drawn image to let the background show through. I would like the hole not be a solid shape, but rather a gradient. In other words, every pixel in the BufferedImage should have an alpha/opacity proportional to its distance from the center of the hole. I am somewhat familiar with Graphics2d gradients and with AlphaComposite , but

Thread not Updating Progress Bar on GlassPane

半腔热情 提交于 2019-12-12 09:26:06
问题 In the following application I have put a button ,clicking on which makes the GlassPane visible and a Thread starts which updates the Progress bar value.Below is the code:- import java.awt.Color; import java.awt.FlowLayout; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.LinearGradientPaint; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing

I'm rotating image in java but want to save rotated image [duplicate]

荒凉一梦 提交于 2019-12-12 04:19:52
问题 This question already has answers here : Java2D - How to rotate an image and save the result (3 answers) Closed 2 years ago . I'm using AffineTransform to rotate the image and I had tried to save the rotated image by ImageIo.write it is saving the image but not the rotated image it's saving the original image. so please tell me how to correct it Thanks in advance. This is my code for rotation and saving the image public class Image { public static void main(String[] args) { new Image(); }

How to successfully draw background JPanel once and update foreground JPanel constantly?

天涯浪子 提交于 2019-12-12 03:47:38
问题 I have a custom JLayeredPane, and I am repainting it in my game loop. There are two custom JPanels added into the JLayeredPane. These are foreground and background JPanels. How do I successfully only draw my background JPanel once, (And repaint when window is re-sized or any other reason) to reduce impact on system resources, while continuing to update my foreground JPanel constantly. To re-iterate, I dont want to constantly repaint the background JPanel in a loop. I want to repaint it only

Java - Open JScrollPane with JScrollBar set to the right

╄→гoц情女王★ 提交于 2019-12-12 02:48:17
问题 I'm looking to try and load up a JScrollPane with the horizontal JScrollBar set all the way to the right, i.e. like in this image: http://i.imgur.com/VeKYPa6.png However, no matter what I do, I end up with this image: http://i.imgur.com/c9KzRqZ.png This is the relevant snippet of my code JComponent graph = new TrendsForSuccLarge(wccScores, wccScoresTimes, graphStrings, maxScore, bgColour, iPadWidth); JScrollPane graphScrollPane = new JScrollPane(graph, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED

Transparent dragging component image as the drag icon in Swing

一曲冷凌霜 提交于 2019-12-12 01:14:27
问题 How can I set a quite large drag icon made by transparent dragging component image. So the user sees what has selected for dragging while dragging. It'd better that the image has a graded transparent level so the bottom right of the component image will be 99% transparent and the top left of the componenet image will have 50% transparency. What I need is generating the image and converting it to an ImageIcon. The component can be any thing. Even a complex one. For example a JPanel with some

How to draw portions of circles based on percentages in Graphics2D?

纵然是瞬间 提交于 2019-12-11 22:31:54
问题 I am working on a circular health bar for a top-down 2d shooter and would like some opinions of the best way to do this in only graphics2D. Wondering if drawing many arcs or just using images would be best. 回答1: The answer depends on what you want to achieve... This is a very basic example... You can go backwards Even start at a different angle. With a little bit of work you could reduce the extended so you could have an "open" progress, for example, starting at 245 degrees with a total

Drawing: centering objects of different size

自古美人都是妖i 提交于 2019-12-11 22:19:56
问题 This program draws two circles of the inputted size on a jPanel, one on top of another. The problem is, the two circles are not centered. How can i solve this? Code: private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { jPanel.Repaint(); try{ jLabel6.setText(""); int a=Integer.parseInt(jTextField1.getText()); Graphics2D gfx=(Graphics2D)jPanel1.getGraphics(); gfx.clearRect(0, 0, getWidth(), getHeight()); gfx.setColor(Color.red); gfx.fillOval(100,50,a,a); gfx.fillOval(100,50,a