graphics2d

Why can't I access my panel's getWidth() and getHeight() functions?

Deadly 提交于 2019-11-27 15:40:55
I'm writing a simple program to test out basic GUI. The program prints a letter in the middle of the screen and allows the user to move it with the arrow keys. Everything works fine, but when I try to center the letter at the start of the program, it seems that the getWidth and getHeight functions aren't returning the proper numbers. Here's the snippet containing my Panel class static class LinePanel extends JPanel{ int xCenter = getWidth() /2; int yCenter = getHeight() /2; private int x = xCenter; private int y = yCenter; private char keyChar = 'A'; public LinePanel(){ addKeyListener(new

How to smoothen scrolling of JFrame in Java

我是研究僧i 提交于 2019-11-27 14:53:54
I have a JFrame in my Java application that contains a JPanel where I have some drawing objects created at run-time. The problem is while scrolling the JFrame for large figures the scrolling slows up and scroll bar does not move smoothly. Please note I am using Graphics 2D object and doing repaint on scroll action. Is there any way of smoothing the scrolling action of JFrame . Here is some part of the code public class DiagramPanel implements MouseListener{ int click=0; Point p1; Point p2; private Dimension panelDimension; .... // variables public void go() { p1 = new Point(); p2 = new Point()

Drawing Transparent Images In Java Graphics2D

限于喜欢 提交于 2019-11-27 11:17:07
问题 I want to draw a PARTIALLY transparent image on top of another (Making shadows over things). I am currently using java's Graphics2D class to render, I've been told to set the composite to AlphaComposite, but that only sets it completely transparent. Can I do this with my current setup? What do I have to do to fix this? This is the code I was told that would make it partially transparent: AlphaComposite ac = java.awt.AlphaComposite.getInstance(AlphaComposite.CLEAR, 0.5F); g.setComposite(ac);

Java how to paint rectangles on mouseclick

感情迁移 提交于 2019-11-27 09:52:09
As the title says, I'm having a hard time trying to draw some rectangles (filled) in JApplet. The exact goal is to have a 50x50 table and when you click on a targeted cell, to make it filled (possibly done by drawing a filled rectangle). I have done the maths about the coordinates of the starting point, but for some reason I can't draw the new rectangle in the MouseClicked method. Any suggestions? public class Main extends JApplet { public static final int DIMX = 800; public static final int DIMY = 800; public static final int ratio = 16; Graphics g; boolean drawing; public int cX; public int

paintComponent draws other components on top of my drawing

人走茶凉 提交于 2019-11-27 09:42:08
I'm trying to build a simple paint tool. The mouseDrag events creates a new ellipse and causes my JPanel to repaint() . This works fine so far. However, if I press any button (or any other UI component) before firing the mouseDrag event for the first time, the button is painted in the upper left corner of my panel. I have isolated the code into this test application: import java.awt.BasicStroke; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import javax.swing

Rotate an image in java by the specified angle

瘦欲@ 提交于 2019-11-27 09:36:47
Here's the function which draws a shape at the given coordinates: public void drawTank(int x,int y){ int h = 50; int w = 50; graphic.setColor(Color.darkGray); graphic.drawRect(x, y, h, w); graphic.fillRect(x, y, h, w); graphic.setColor(Color.GRAY); graphic.drawRect(x+50, y+20, 35, 10); graphic.fillRect(x+50, y+20, 35, 10); } I want to add one more variable to the above function called 'angle', so that the image is also rotated by the angle specified (drawTank(int x,int y,int angle). Updated with example What I tried to do is that I initialized Graphics2D and changed my code respectively: g2D

Infinite background for game

梦想与她 提交于 2019-11-27 09:22:16
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.BufferedImage; import java.net.URL; import java.io.IOException; import javax.imageio.ImageIO; import javax

How to draw images on transparent window?

一个人想着一个人 提交于 2019-11-27 07:10:51
问题 I'm trying to draw Images with Graphics2D on JFrame. But this code only displays blank background. How to do that? Java Version: SE-1.6 IDE: Eclipse My code looks like this: import java.awt.BasicStroke; import java.awt.Color; import java.awt.Graphics2D; import java.awt.image.BufferStrategy; import java.awt.geom.Line2D; import java.util.TimerTask; import javax.swing.JFrame; public class GraphicTest extends JFrame{ public static void main(String[] args) { GraphicTest gt = new GraphicTest(); gt

How to use AffineTransform.quadrantRotate to rotate a bitmap?

坚强是说给别人听的谎言 提交于 2019-11-27 05:22:51
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. g.drawImage(smallerBitmap, at, null); ... I'm probably using quadrantRotate() incorrectly - if I remove

How to rotate a rectangle after reaching specified position?

自古美人都是妖i 提交于 2019-11-27 04:52:40
问题 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