graphics2d

How can I rotate an onscreen component in Java?

本秂侑毒 提交于 2021-02-19 06:48:25
问题 import javax.swing.*; import java.awt.*; public class JFrameAnimationTest extends JFrame { public static void main(String[] args) throws Exception{ AnimationPanel animation = new AnimationPanel(); JFrameAnimationTest frame = new JFrameAnimationTest(); frame.setSize(600, 480); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(animation); frame.setVisible(true); for(int i = 0; i < 100; i++) { animation.incX(1); //animation.incY(1); animation

how to alternate colors in a circle, so that circle looks like rotating?

回眸只為那壹抹淺笑 提交于 2021-02-11 16:39:41
问题 The expected output should be like this with the colors changing their position as well: Expected output-: the colors should change their positions in a circle so that it looks like they are moving without changing the position of circle. though my code is written in codeblocks in c/c++, i will be happy to get answers in any other programming languages. my present code #include<graphics.h> #include<stdlib.h> #include<stdio.h> #include<conio.h> #include<math.h> #include<string.h> #include

Making margins smaller - Java Printing

倖福魔咒の 提交于 2021-02-07 18:36:15
问题 I am using this code to print on paper: //Overriden from printable interface public int print(Graphics g, PageFormat pageFormat, int pageIndex) throws PrinterException { if (pageIndex != 0) { return Printable.NO_SUCH_PAGE; } Paper a4 = new Paper(); a4.setImageableArea(0, 0, a4.getWidth(), a4.getHeight()); pageFormat.setPaper(a4); pageFormat.setOrientation(PageFormat.PORTRAIT); Graphics2D g2d = (Graphics2D)g; //g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY()); this

Draw Graphics2D to another Graphics2D

…衆ロ難τιáo~ 提交于 2021-02-07 07:26:20
问题 It is possible to draw from one Graphics2D to another Graphics2D ? Let me explain. I have printing issues, when i display a JTextArea or JTextPanel in screen, internaly its used sun.java2d.SunGraphics2D , but when im printing its used sun.print.PeekGraphics and sun.awt.windows.WPathGraphics . The problem is with some kind of Fonts, like Arial. In some sizes lines are cut. I have tryed a lot of ways to render the text in printing, Graphics2D.drawString , SwingUtilities2.drawString , TextLayout

Reference of graphics 2d object doesn't work in orphan Thread

回眸只為那壹抹淺笑 提交于 2021-01-28 06:23:51
问题 I am trying to design a simple game using Graphics2D in a JPanel. I am able to draw normal objects by overriding the paintComponent() method. But when I reference the Graphics2D object inside a orphan Thread, it does not work. Where am I going wrong? public void paintComponent(Graphics g) { super.paintComponent(g); g2d = (Graphics2D) g; g2d.drawString("sample",60,100); //Works fine if(<Certain Condition>){ new Thread(new Runnable(){ //Some Code Here public void run() { try{ g2d.drawString(

Graying out a BufferedImage

吃可爱长大的小学妹 提交于 2021-01-27 13:21:55
问题 I'm trying to gray out a BufferedImage (not convert it to gray scale, just add a gray tint on top). Right now, I'm doing this by using another image, make it translucent, and then overlay it on top of my original image. This is the code I have for now: package com.mypkg; import java.awt.AlphaComposite; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.File; import java.net.URL; import javax.imageio.ImageIO; import org.imgscalr.Scalr; public class Overlay { public

Drawing an image using sub-pixel level accuracy using Graphics2D

て烟熏妆下的殇ゞ 提交于 2020-08-22 11:48:11
问题 I am currently attempting to draw images on the screen at a regular rate like in a video game. Unfortunately, because of the rate at which the image is moving, some frames are identical because the image has not yet moved a full pixel. Is there a way to provide float values to Graphics2D for on-screen position to draw the image, rather than int values? Initially here is what I had done: BufferedImage srcImage = sprite.getImage ( ); Position imagePosition = ... ; //Defined elsewhere g

Reset Graphics2D object in Java

混江龙づ霸主 提交于 2020-05-11 05:16:36
问题 I was experimenting with Graphics2D in Java. But as usual, I am stuck. :P The problem is: Suppose i have this code, Graphics2D g=(Graphics2D)(this.getGraphics()); //Inside a JFrame g.rotate(Math.PI/8); g.drawLine(10, 20, 65, 80); //I want this one and all following lines to be drawn without any rotation g.drawLine(120, 220, 625, 180); Is it possible??? I know there must be some way but I am not able to figure it out. Please help. 回答1: What you'll want to do is restore the transform. Try

Reset Graphics2D object in Java

半城伤御伤魂 提交于 2020-05-11 05:13:47
问题 I was experimenting with Graphics2D in Java. But as usual, I am stuck. :P The problem is: Suppose i have this code, Graphics2D g=(Graphics2D)(this.getGraphics()); //Inside a JFrame g.rotate(Math.PI/8); g.drawLine(10, 20, 65, 80); //I want this one and all following lines to be drawn without any rotation g.drawLine(120, 220, 625, 180); Is it possible??? I know there must be some way but I am not able to figure it out. Please help. 回答1: What you'll want to do is restore the transform. Try