graphics2d

How can I draw on JPanel using another quadrant for the coordinates?

别来无恙 提交于 2019-12-08 10:56:20
问题 I would like to draw some shapes on a JPanel by overriding paintComponent . I would like to be able to pan and zoom. Panning and zooming is easy to do with AffineTransform and the setTransform method on the Graphics2D object. After doing that I can easyli draw the shapes with g2.draw(myShape) The shapes are defined with the "world coordinates" so it works fine when panning and I have to translate them to the canvas/JPanel coordinates before drawing. Now I would like to change the quadrant of

How to get pixel color from Graphics g [duplicate]

谁都会走 提交于 2019-12-08 10:13:42
问题 This question already has answers here : How to color a pixel? (4 answers) Closed 5 months ago . On my study I have graphics lessons. We have Bresenham for line drawing and Circle drawing. On next lesson I will be learning flood fill. For flood fill I need to get pixel color to check whether I need to fill or not to fill. This is my code now from all lessons. package lab1; import javax.swing.*; import java.awt.*; import java.util.Random; public class Lab1 extends JPanel{ private Random random

Moving a ball on the screen with buttons. Can't program the initial position.

雨燕双飞 提交于 2019-12-08 08:14:41
问题 So I'm doing this exercise, where I need to create a program that will be moving a little ball on the screen by pressing one of four buttons. I've completed it, but then I wanted to make the initial position to be in the center of the screen, so I assigned the values getWidth()/2 to xCoord and getHeight()/2 to yCoord (first I did it without the constructor, then when it didn't work I added the constructor and added repaint(), so the paintComponent() would be called) but the ball is still in

Java anti fillRect (fill everything outside of said rectangle)

≯℡__Kan透↙ 提交于 2019-12-08 06:46:23
问题 In Java, there is the Graphics2D.fillRect(x, y, width, height) function. In my program, I am looking for something similar, yet completely opposite. I need to fill everything on the screen except this certain x, y, width, height, sort of like an anti-fillRect. Is there a built in function that I am overlooking, or can you point me in the right direction to create one? Not required, but it would be a nice bonus if it could work with other Java 2D shapes. 回答1: There are a few ways that might be

Java Write Text Image with Specific Fonts

删除回忆录丶 提交于 2019-12-08 04:05:40
问题 I'm writing some text on top of an existing image and the font isn't very sharp. Is there some settings either with the Graphics2D or Font classes that help make fonts look nicer when writing text on top of images? The Dante font doesn't come out as Dante when I write it. I've tried to use antialiasing but it had no effect (see setRenderingHint). The images came out the same with or without the RenderingHint set. Any suggestions? public class ImageCreator{ public void createImage(String text)

Draw ring with given thickness, position, and radius. (Java2D)

帅比萌擦擦* 提交于 2019-12-08 03:07:25
I need to draw a ring, with given thickness, that looks something like this: The center must be transparent, so that it doesn't cover previously drawn shapes. (or other rings) I've tried something like this: //g is a Graphics2D object g.setColor(Color.RED); g.drawOval(x,y,width,height); g.setColor(Color.WHITE); g.drawOval(x+thickness,y+thickness,width-2*thickness,height-2*thickness); which draws a satisfactory ring, but it covers other shapes; the interior is white, not transparent. How can I modify/rewrite my code so that it doesn't do that? You can create an Area from an Ellipse2D that

Drawing multiple graphic2d components into JPanel

ぃ、小莉子 提交于 2019-12-07 22:19:56
问题 I've read a lot of tutorials on drawing Graphics2D components and adding to JPanel/JFrame but I can't find how to add multiple these components into one JPanel simply. My code below adds only 1 component (line) and I can't find why it isn't possible to add more. What am I doing wrong? Desired behaviour: there should be 3 red lines. My whole code: package Examples; import java.awt.BasicStroke; import java.awt.Color; import java.awt.Dimension; import java.awt.EventQueue; import java.awt

Java Printing Font Stretch

核能气质少年 提交于 2019-12-07 11:41:33
问题 I just got the printer to work in java how I need it too, but there's one last problem I need to solve. When it prints, the font's width is rather stretched, and not crisp and clear like it should be. Here is my code my the actual drawing to the paper: FontMetrics metrics = graphics.getFontMetrics(font); int lineHeight = metrics.getHeight(); arrangePage(graphics, pageFormat, lineHeight); if (page > pageBreaks.length){ return NO_SUCH_PAGE; } Graphics2D g = (Graphics2D) graphics; g.translate

How to draw thin line with no gap while dragging the cursor?

不羁岁月 提交于 2019-12-07 07:23:42
问题 I have this following class, which refresh a jpeg file in layer 0 and layer 1 is used to draw/paint/sketch up anything related to smash things. But in my drawing when I want to do a thin line, it breaks. Because the mouse cursor movement needs to be slower. How to resolve on fast mouse move, that the line remains joined? Annotation.java package test; import java.awt.*; import java.awt.event.*; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax

Java BufferedImage saves with unwanted background color

放肆的年华 提交于 2019-12-07 03:13:15
问题 Thanks ahead of time for the help Description: The program draws, displays, and saves an image. It works as following: the object itself extends Frame. In the constructor, the object creates a BufferedImage, and calls a method that draw onto that image. Then, it displays the image onto the Frame. Finally, it saves the image into a file (I don't care what format it uses). The main program creates the object, which does the rest. Problem: The saved file always has a colored background! This is