graphics2d

How to Undo the splash screen painting in java

ぐ巨炮叔叔 提交于 2019-11-28 10:23:34
问题 I am a beginner in Java GUI, The problem is that I am using a splash screen which has a gradient as a background and I want to write the text , erase it and then write the new one. I Managed to write the text and even I can write the new one but the problem is that the new text overwrites the old text, and the old text is not erased, Since I have a gradient as background so I cannot use .clearRect() or .fillRect() of Graphics2D Class since they fill it with a solid color, here is my code

Java image rotation with AffineTransform outputs black image, but works well when resized

随声附和 提交于 2019-11-28 10:23:28
I am just trying to rotate a JPG file by 90 degrees. However my code outputs image ( BufferedImage ) that is completely black. Here's the way to reproduce: (Download 3.jpg here ) private static BufferedImage transform(BufferedImage originalImage) { BufferedImage newImage = null; AffineTransform tx = new AffineTransform(); tx.rotate(Math.PI / 2, originalImage.getWidth() / 2, originalImage.getHeight() / 2); AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BICUBIC); newImage = op.filter(originalImage, newImage); return newImage; } public static void main(String[] args)

Triangle Draw Method

ぃ、小莉子 提交于 2019-11-28 09:45:56
I have trouble drawing a triangle with the draw(Graphics g) method in Java. I can draw a rectangle like so: public void draw(Graphics g) { g.setColor(colorFill); g.fillRect(p.x, p.y, width, height); g.setColor(colorBorder); g.drawRect(p.x, p.y, width, height); drawHandles(g); Where p represents "the top left corner of the shapes". How would I draw the triangle in the same manner? Could someone give me an example for a standard triangle? davidbuzatto There is not a drawTriangle method neither in Graphics nor Graphics2D. You need to do it by yourself. You can draw three lines using the drawLine

How can I center Graphics.drawString() in Java?

僤鯓⒐⒋嵵緔 提交于 2019-11-28 09:37:53
I'm currently working on the menu system for my Java game, and I wonder how I can center the text from Graphics.drawString() , so that if I want to draw a text whose center point is at X: 50 and Y: 50 , and the text is 30 pixels wide and 10 pixels tall, the text will start at X: 35 and Y: 45 . Can I determine the width of the text before I draw it? Then it would be easy maths. EDIT: I also wonder if I can get the height of the text, so that I can center it vertically too. Any help is appreciated! Daniel Kvist I used the answer on this question . The code I used looks something like this: /** *

java draw line as the mouse is moved

◇◆丶佛笑我妖孽 提交于 2019-11-28 09:33:56
I would like to add a feature to my application which allows the user to draw a straight line by clicking the mouse at the start location and releasing it at the end location. The line should move as the mouse moves until it is finally released; similar to the way that a line can be drawn using the Microsoft Paint application. How can implement this so that the line is repainted as it moves without repainting other things that may already be drawn in that rectangular area? Try this...Draw a red line on the screen as the mouse is moved (dragged). public static void main(String args[]) throws

Java rotating an ImageBuffer fails

不羁岁月 提交于 2019-11-28 09:32:38
问题 I am trying to rotate an instance of a BufferImage named pic when I try this it resizes and skews and crops the image, any advice to get it to work properly public void rotate(double rads){ AffineTransform tx = new AffineTransform(); tx.rotate(rads,pic.getWidth()/2,pic.getHeight()/2); AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR); pic = op.filter(pic, null); } When I have it rotate 90˚ it works fine so I'm wondering if the problem is that it is the shape of

Flipping shape (not image)

这一生的挚爱 提交于 2019-11-28 09:29:38
问题 Solved: Thanks @MadProgrammer I replaced g2.rotate(Math.toRadians(180.0)); by g2.scale(1, -1); thanks^^ I wrote program to show Digital Clock with mirror (Vertical Flip) This is my code import java.awt.*; import java.awt.font.GlyphVector; import javax.swing.*; import java.util.*; public class DigitalClock extends JFrame implements Runnable { /** * @author HASSAN */ Thread runner; // declare global objects Font clockFont; Shape mirror; public DigitalClock() { super("Digital Clock - Hassan

Drawing text with outline in java

大城市里の小女人 提交于 2019-11-28 08:48:30
I'm working with graphcis2d in Java and am currently using this to draw text into a bufferedImage Font font1 = new Font("Arial", Font.PLAIN, 120); g2d.setFont(font1); FontMetrics fm1 = g2d.getFontMetrics(font1); g2d.drawString(s[1], width/2-fm1.stringWidth(s[1])/2, height/4-70); I want to draw this text with an different color outline. GlyphVector gv = font1.createGlyphVector(g2d.getFontRenderContext(), s[1]); Shape shape = gv.getOutline(); g2d.setStroke(new BasicStroke(4.0f)); g2d.translate(width/2-fm1.stringWidth(s[1])/2, height/4-70); g2d.draw(shape); The problem with using this method,

Incremental graphics in Swing

纵然是瞬间 提交于 2019-11-28 08:46:41
问题 I'm trying to get the hang of doing graphics stuff (drawing lines, etc.) in Swing. So far, all the tutorials I've seen declare a class that overrides paintComponent , and all the paintComponent methods do some set, specific thing, like drawing a red square (although maybe they draw it at a different location every time). Or maybe they draw a number of lines and shapes, but the paintComponent method does everything all at once. I'm trying to figure out: suppose I want to draw one thing in a

Drawing an image at a point of another image

≡放荡痞女 提交于 2019-11-28 07:52:04
问题 So I'm creating a side scroller and I'm trying to draw an image at the point of another image. I have my background image which is 5000 x 500 and lets say I want to draw an image that's 25x25 at 500, 50 of the background image. How would I do that? So far I've tried: Coins c = new Coins(img.getWidth(this) - 4500, img.getHeight(this) - 250); But this just draws it at 500, 50 of the frame so it "moves" across the image as I scroll to the right. I want, after scroll once to the right, the coin