graphics2d

Java Graphics2D Translate and Scale

丶灬走出姿态 提交于 2019-11-28 06:18:29
问题 I have a problem. I want to be able to zoom into my Graphics2D screen using the mouse wheel but I want to be able to translate the Graphics2D as well so that it's right on the spot that I scaled. Here's what's happening so far: "http://cdn.makeagif.com/media/6-11-2015/E0kYGY.gif" It's translating to the player's position correctly but I want it to be zoomable in and out with the scrolling of the mouse wheel. Here's the code I have so far to do it: private AffineTransform getCamTransform() {

Why does one have to use the paintComponent method to draw in Java?

我怕爱的太早我们不能终老 提交于 2019-11-28 04:48:04
问题 I hope this question is not going to be regarded as too general. I understand that to draw on a JPanel you override the paintComponent method and place all your drawing code within that method. My question is why! Why does Java not seem to allow/provide for drawing with methods such as panel.drawLine(x1,y1,x2,y2) or panel.drawText(text,x,y) ? It was all so much easier in delphi. There must be a reason I just can't figure it out. 回答1: That's because that's the way it works. It was designed

Resize drawing to match frame size

别说谁变了你拦得住时间么 提交于 2019-11-28 03:54:36
问题 I've written an app that custom draws everything inside paint() based on fixed pixel positions. Then I disabled resize of the frame so its always visible. However, now I would like to be able to resize it but I dont want to change my drawling code. I was hoping I could grab the 300x300 square of the Graphics g object and resize it to the JFrame current size after all of my drawling code, but I have no idea what I'm doing. Here sample code. In this I want the 100x100 square to remain in the

Font.createFont leaves files in temp directory

送分小仙女□ 提交于 2019-11-28 03:27:21
问题 The code below does its work but leaves copies of the font file in the temp directory each time it is run. These files are named +~JF7154903081130224445.tmp where the number seems random for each created file. InputStream fontStream = this.getClass().getResourceAsStream("handsean.ttf"); Font baseFont = Font.createFont(Font.TRUETYPE_FONT, fontStream); fontStream.close(); I have found years-old discussions in forums at sun.com and other resources on the web where this is recognized as a bug in

How to rotate a rectangle after reaching specified position?

落爺英雄遲暮 提交于 2019-11-28 02:14:05
I would like to rotate a rectangle when e.g. y position achieve specified position. I would like to behave a rectangle as a car on a junction - just turn e.g. right. I prefer just rotate and continue. A draft code looks like that: Graphics2D g2d = (Graphics2D) g.create(); g2d.setPaint(new Color(150, 150, 0)); //1. when rectangle achieve 500 on y dimension just rotate left/right if(y==500) { _rotate = true; g2d.rotate(Math.toRadians(90.)); } if(_rotate) { //if rotate, continue way on x dimension ++x ; g2d.fillRect(x, y, 20, 40); } else { //else go to the north --y; g2d.fillRect(x, y, 20, 40); }

create an transparent rectangle over blurred background in jframe

半腔热情 提交于 2019-11-28 01:46:11
I am having problem in creating an transparent rectangle over blurred background. I am trying to do this task on glasspane . Here is my code snippet. void createBlur() { alpha = 1.0f; JRootPane root = SwingUtilities.getRootPane(jf); blurBuffer = GraphicsUtilities.createCompatibleImage(jf.getWidth(), jf.getHeight()); Graphics2D g2d = blurBuffer.createGraphics(); root.paint(g2d); g2d.dispose(); backBuffer = blurBuffer; blurBuffer = GraphicsUtilities.createThumbnailFast(blurBuffer, jf.getWidth() / 2); blurBuffer = new GaussianBlurFilter(5).filter(blurBuffer, null); } where, backBuffer and

how to save panel as image in swing?

别来无恙 提交于 2019-11-28 00:50:13
Hi i want to convert panel which contains components like label and buttons to image file. I have done the following code. The image was saved. but the content of the panel not visible or saved. Can anyone tell me how to save the panel with its components. Code: package PanelToImage; import java.awt.Color; import java.awt.image.BufferedImage; import java.io.File; import javax.imageio.ImageIO; import javax.swing.*; public class sample extends JPanel { public JPanel firstpanel; public JPanel secondpanel; JLabel label1, label2; JButton button1, button2; public sample() { firstpanel = new JPanel()

plotting a curve around a set of points

落花浮王杯 提交于 2019-11-27 20:08:11
问题 I have a set of points on a plane. They are partitioned into subsets. I want to plot a closed curve around points that belong to the same subset, so that points that belong to a subset will be inside the curve, and those that aren't will be outside. Therefore simple circles, or a convex hull might not work. For a starter, let's say I just want to have a smooth curve around a set of point (without the requirement that it excludes other points) Any ideas how to do that in R? ---added later---

Drawing rectangles on a JPanel

喜夏-厌秋 提交于 2019-11-27 16:22:25
I have a JScrollPane and on top of it I have a JPanel named 'panel1'. I want some rectangles to be drawn on this JPanel. I have a class named DrawRectPanel which extends JPanel and does all the drawing stuff. The problem is that, I tried to draw the rectangles on panel1 by writing the following code : panel1.add(new DrawRectPanel()); but nothing appeared on panel1 then I tried, just as a test to the class DrawRectPanel : JFrame frame = new JFrame(); frame.setSize(1000, 500); Container contentPane = frame.getContentPane(); contentPane.add(new DrawRectPanel()); frame.show(); This worked, and

Line2D decoration tips needed - Graphics2D

偶尔善良 提交于 2019-11-27 16:17:51
I have Line2D and Arc2D objects laid out on my JPanel by Graphics2D drawing. You can have a look a part of it on this question " How to make pixel perfect Line2D in - Graphics2D ". Now what I want to achieve is, I want to create two parallel lines and arcs for all Line2D and Arc2D objects. Visually, Normal Line2D and Arc2D drawn currently, Want to decorate it like this, My Thoughts so far, I might be able to achieve this by creating two different line and give an offset +gap and -gap from my normal line position. However This will make lots of objects which I don't want to. Now, is it possible