graphics2d

Java: Rotating Images

回眸只為那壹抹淺笑 提交于 2019-11-26 12:29:02
I need to be able to rotate images individually(in java). The only thing I have found so far is g2d.drawImage(image, affinetransform, ImageObserver ). Unfortunately, I need to draw the image at a specific point, and there is no method with an argument that 1.rotates the image separately and 2. allows me to set the x and y. any help is appreciated AlanFoster This is how you can do it. This code assumes the existance of a buffered image called 'image' (like your comment says) // The required drawing location int drawLocationX = 300; int drawLocationY = 300; // Rotation information double

How to use AffineTransform.quadrantRotate to rotate a bitmap?

て烟熏妆下的殇ゞ 提交于 2019-11-26 11:33:08
问题 I want to rotate a bitmap about its center point , and then draw it into a larger graphics context . The bitmap is 40x40 pixels . The graphics context is 500x500 pixels . This is what I\'m doing: BufferedImage bi = new BufferedImage(500, 500, BufferedImage.TYPE_INT_ARGB); Graphics2D g = bi.createGraphics(); AffineTransform at = new AffineTransform(); at.quadrantRotate(1, -20, -20); // rotate 90 degrees around center point. at.translate(100, 40); // I want to put its top-left corner at 100,40.

Infinite background for game

蹲街弑〆低调 提交于 2019-11-26 11:28:04
问题 I am working on a Java project to simulate the flight of a helicopter in a frame. The helicopter moves on the screen using the arrow keys. I want the helicopter to be able to move infinitely, that is, when the helicopter reaches the edge of the frame, the background should move in the opposite direction to have the effect of endless terrain. Here is the code I have so far: import java.awt.Graphics; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.image

Stretch a JLabel text

偶尔善良 提交于 2019-11-26 10:57:19
Is there a way to make a JLabel's text stretch to 100% height? I need the text to update when the component's size changes as well. I saw some solution that could work; It involved calculating and setting the font size so it appears the right height. I would have also have to add listeners for when the height changed to make it respond and I do not know exactly where I should do that. I am hoping for a better solution with layout managers, but couldn't find anything. Any ideas? trashgod In the approach shown below, the desired text is imaged using TextLayout using a suitably large Font size

Using a matrix to rotate rectangles individually

时间秒杀一切 提交于 2019-11-26 09:38:06
问题 Having a bit of a drawing complication you would call it. My math is a bit rusty when it comes to Matrices and drawing rotations on shapes. Here is a bit of code: private void Form1_Paint(object sender, PaintEventArgs e) { g = e.Graphics; g.SmoothingMode = SmoothingMode.HighQuality; DoRotation(e); g.DrawRectangle(new Pen(Color.Black), r1); g.DrawRectangle(new Pen(Color.Black), r2); // draw a line (PEN, CenterOfObject(X, Y), endpoint(X,Y) ) g.DrawLine(new Pen(Color.Black), new Point((r1.X + 50

Rotate a Java Graphics2D Rectangle?

陌路散爱 提交于 2019-11-26 08:24:10
问题 I have searched everywhere and I just cant find the answer. How do I rotate a Rectangle in java? Here is some of my code: package net.chrypthic.Space; import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Space extends JPanel implements ActionListener{ Timer time; public Space() { setVisible(true); setFocusable(true); addMouseMotionListener(new ML()); addMouseListener(new ML()); addKeyListener(new AL()); time=new Timer(5, this); time.start(); } public void paint

Fit/Scale JComponent to page being printed

眉间皱痕 提交于 2019-11-26 06:09:30
问题 I\'m trying to scale my component so it can fit on a single printed page (portrait or landscape) gDiagram.getComponent() is the component (JPanel) I\'m trying to print. Here\'s what I\'ve got so far based on How can I print a single JPanel's contents? /** * Prints the diagram. */ public void printDiagram() { PrinterJob pj = PrinterJob.getPrinterJob(); pj.setJobName(\" Print Component \"); pj.setPrintable(new Printable() { @Override public int print(Graphics g, PageFormat pf, int pageNumber)

How to make line animation smoother?

最后都变了- 提交于 2019-11-26 04:53:58
问题 I am making a simple animation in Java and I am trying to make it as smooth as possible. I use only *.Double inner classes of each Shape object and I set antialiasing in the Graphics2D objects on. It all works as long as I use only the fill() method but if I also use draw() method to draw lines around the same Shape the animation of these lines is choppy - pixel by pixel. Each of my rectangles on the canvas has this method to paint itself. It is moved every 20ms and the whole canvas is

Problems with newline in Graphics2D.drawString

。_饼干妹妹 提交于 2019-11-26 04:48:01
问题 g2 is an instance of the class Graphics2D . I\'d like to be able to draw multi-line text, but that requires a newline character. The following code renders in one line. String newline = System.getProperty(\"line.separator\"); g2.drawString(\"part1\\r\\n\" + newline + \"part2\", x, y); 回答1: The drawString method does not handle new-lines. You'll have to split the string on new-line characters yourself and draw the lines one by one with a proper vertical offset: void drawString(Graphics g,

Rotating BufferedImage instances

心不动则不痛 提交于 2019-11-26 04:26:57
问题 I am having trouble getting a rotated BufferedImage to display. I think the rotation is working just fine, but I can\'t actually draw it to the screen. My code: Class extends JPanel { BufferedImage img; int rotation = 0; public void paintComponent(Graphics g) { g.clearRect(0, 0, getWidth(), getHeight()); img2d = img.createGraphics(); img2d.rotate(Math.toRadians(rotation), img.getWidth() / 2, img.getHeight() / 2); g.drawImage(img, imgx, imgy, null); this.repaint(); } } This is not working for