java-2d

ArrayIndexOutOfBoundsException error while drawing a polygon

假装没事ソ 提交于 2019-12-14 04:00:31
问题 hi there i'm having a trouble while i'm trying to draw a polygon. first of all, when i try to draw a polygon with using addPoint(int x, int y) method and giving coordinates one by one there is no problem, polygon could be drawed perfectly. however, if i give the coordinates as an array (an integer array for x coordinates and y coordinates) compiler gives error. this is the working code as you can see, @Override public void paint(Graphics g) { Graphics2D g2 = (Graphics2D) g; Polygon poly = new

Code is producing a gray screen

纵饮孤独 提交于 2019-12-14 03:50:05
问题 I can't seem to work out what the problem is here. import java.awt.Color; import java.awt.Font; import java.awt.FontMetrics; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.event.MouseEvent; import java.awt.event.MouseMotionListener; import java.awt.image.BufferedImage; import javax.swing.JFrame; import javax.swing.JPanel; public class GamePanel extends JPanel implements Runnable, MouseMotionListener { private static final int SCREEN_WIDTH = 640; private static final int

How to blur an image under the mouse

自闭症网瘾萝莉.ら 提交于 2019-12-14 03:36:13
问题 I am working in java, and have an image being generated. When the mouse passes over the generated image, I need the image to use a blur or pixelation filter. What methods should I use to accomplish this? 回答1: Look in here: http://www.java2s.com/Code/Java/2D-Graphics-GUI/ImageEffectSharpenblur.htm You need define a Kernel with an array of whatever values you want, instantiate the ConvolveOp with the Kernal as an argument, and then filter the desired image. 回答2: public void test(int x, int y){

Java 2D API: Null Pointer Exception

一个人想着一个人 提交于 2019-12-14 03:10:05
问题 Please have a look at the following code package java2D; import java.awt.BasicStroke; import java.awt.FlowLayout; import java.awt.Graphics; import java.awt.Graphics2D; import javax.swing.*; import java.awt.*; public class Joins extends JFrame { private JPanel centerPanel; public Joins() { this.setLayout(new BorderLayout()); this.add(createCenterPanel(),"Center"); this.setSize(200,250); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } private JPanel

Java Swing Not Rendering Correctly

两盒软妹~` 提交于 2019-12-13 19:19:28
问题 I have been working on a project. In one part of the project I needed to draw on JPanel. Graphics2D object is passed to the model part of the framework and model will draw whatever it needs to draw at that instance. Problem is JFrame is not rendered correctly. It looks half white shaded and not painting correctly. It works fine on my laptop (Sony Vaio, Windows7) but it's not rendering correctly in all other systems I tested (2 Dell laptops,windows8;1 Thoshiba,windows7;1 samsung, windows8). It

“Zoom” text to be as big as possible within constraints/box

拈花ヽ惹草 提交于 2019-12-13 13:25:35
问题 First problem: You have 400 pixels width to go on, and need to fit some text within that constraint as large as possible (thus, the text shall use that amount of space). Throw in a new constraint: If the text is just "A", then it shall not zoom this above 100 pixels height (or some specific font size). Then, a final situation: Linebreaks. Fit some text in the largest possible way within e.g. 400 x 150 pixels. An obvious way is to simply start with point 1, and then increase until you can't

Calculating the scale factor for “bar rectangles” of Chart app

风流意气都作罢 提交于 2019-12-13 06:14:48
问题 I have an array of integer values String[] values={3100,7500,8000,4200,88000,71000,32000}; that need to be scaled into a known height of my JComponent , the question is how to scale these values into e.g. h=600px? Here is a pic just for more clarification of what i want to achieve: Thanks 回答1: bar_height = chart_height*(value/max_value) To determine bar_height , you scale (multiply) chart_height by (value/max_value) , where: bar_height is the height of the a bar in pixels. value is the value

Java2D removing old pixels after moving image?

房东的猫 提交于 2019-12-13 01:27:32
问题 So I'm moving an image in Java2D and it bounces aswell. For some reason, it always leaves behind a trail of old images. How could I fix this? Main class: package org.main.graphics; import java.awt.Graphics; import java.io.IOException; import java.util.ArrayList; import java.util.List; import javax.swing.JFrame; import org.main.entity.Entity; import org.main.entity.Loael; @SuppressWarnings("serial") public class GameWindow extends JFrame implements Runnable { private List<Entity> entities =

how to rotate a rectangle in Java2D

别来无恙 提交于 2019-12-12 17:21:55
问题 I want to rotate a rectangle in a method but do not understand how to do it and tried as follows: private void setBoundaryRotate(Rectangle b, int radio) { AffineTransform transform = new AffineTransform(); transform.rotate(Math.toRadians(45), b.getX() + b.width/2, b.getY() + b.height/2);} Thanks for all. 回答1: You need to call the transform() method on your transform object, passing in the co-ordinates of your rectangle in an array. 回答2: This is a little subjective, it all depends on what it

Aligning Shape's Center to JPanel's Center

送分小仙女□ 提交于 2019-12-12 16:50:26
问题 I have been trying to align my java2d shape's center to the JPanel's center with no success. I was able to do it for an image and many 2D shapes like parallelogram using getBounds method but not for rhombus though all of them follow the same pattern. Drastically, when I prepared an SSCCE out of the actual project I could align none of them correctly. I've written a drawShape method for drawing shapes on center. I didn't understand where I'm going wrong. This is SSCCE: import java.awt.*;