graphics2d

Reset Graphics2D object in Java

点点圈 提交于 2020-05-11 05:13:08
问题 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

Trying to draw lines based on doubles but nothing shows?

落爺英雄遲暮 提交于 2020-03-06 02:38:10
问题 Here's the class in which I try to draw the lines package gps; import java.awt.*; import java.awt.geom.Line2D; import java.util.*; import javax.swing.*; public class RoadMap extends JPanel { public void paintComponent(Graphics2D g) { super.paintComponent(g); g.setColor(Color.blue); for(int i = 0; i < Graph.getEdges().length; i++) { Shape s = new Line2D.Double(Graph.vMap.get(Graph.getEdges()[i].i1).x, Graph.vMap.get(Graph.getEdges()[i].i1).y, Graph.vMap.get(Graph.getEdges()[i].i2).x, Graph

Painting balloons on Graphics2D

泄露秘密 提交于 2020-02-05 13:56:45
问题 I want to make bubble shooter game and I have problem with generate bubbles at start. When trying to compile program, I have error: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException. public class MyPanel extends JPanel { Init init; public MyPanel(){ super(); init = new Init(); } public void paint(Graphics g){ Dimension size = getSize(); Graphics2D g2d = (Graphics2D) g; g2d.setColor(Color.GRAY); g2d.fillRect(0, 0, size.width, size.height - 70); for(int j = 0; j < 10; j++)

Testing graphics generation with JUnit

淺唱寂寞╮ 提交于 2020-02-01 22:33:06
问题 I'm using Java's Graphics2D to generate a graphical representation of a graph. I'm also using ImageIO to write a PNG file. ( ImageIO.write(image, "png", out); ) I'm wondering how should I write JUnit tests to test whether the generated graphics is what is expected. I could pre-generate the PNG files but what if the font is a bit different on a different machine? 回答1: You could try testing for specific, known features of the output e.g.: It there a white pixel at (100,100)? It the border

Getting the Graphics2D?

感情迁移 提交于 2020-01-30 09:09:06
问题 public void paint(Graphics g){ Graphics2D g2 = (Graphics2D)g; // double r = 100; //the radius of the circle //draw the circle Ellipse2D.Double circle = new Ellipse2D.Double(0, 0, 2 * r, 2 * r); g2.draw(circle); This is part of a class within my program, my question lies in the Graphics2D g2 = (Graphics2D)g; Why must you include the "g" after the (Graphics2D), and what exactly does the "Graphics2D" Within the parenthesis mean, i am learning out of a book and neither of these were ever fully

How to use AffineTransform with very little coordinates?

ⅰ亾dé卋堺 提交于 2020-01-24 12:58:06
问题 I have a set of two dimensions points. Their X and Y are greater than -2 and lesser than 2. Such point could be : (-0.00012 ; 1.2334 ) . I would want to display these points on a graph, using rectangles (a rectangle illustrates a point, and has its coordinates set to its point's ones - moreover, it has a size of 10*10). Rectangles like (... ; Y) should be displayed above any rectangles like (... ; Y-1) (positive Y direction is up). Thus, I must set the graph's origin not at the top-left hand

How to rotate text with Graphics2D in Java?

寵の児 提交于 2020-01-23 04:37:33
问题 I want to rotate text on a JPanel using Graphics2D.. My code is this: double paso=d.width/numeroBarras; double alto=datos[i].valor; Font fBarras=new Font("Serif", Font.PLAIN, 15); g2.setFont(fBarras); Rectangle2D barra=new Rectangle2D.Double(x,d.height-alto,paso,alto); //g2.fill(barra); x+=paso; g2.draw(barra); g2.rotate(-Math.PI/2); g2.setColor(Color.BLACK); g2.drawString(datos[i].titulo,(float)alto,(float)paso) This method must draw a rectangle and a text over the rectangle, but when i run

How can I draw a curved line segment using QuadCurve2D.Double?

五迷三道 提交于 2020-01-21 19:18:26
问题 Here is the line of code where I declare the curve: QuadCurve2D.Double curve = new QuadCurve2D.Double(50,100,100,170,150,100); Now what code can I use to draw this curve? I tried something like: g.draw(curve); but obviously that didn't work. Any suggestions? 回答1: I've made a minimum test case of what I think your describing here. This program works but I can't really help you unless I can see the code you are working with. import java.awt.geom.*; import java.awt.*; import javax.swing.*;

How can I draw a curved line segment using QuadCurve2D.Double?

丶灬走出姿态 提交于 2020-01-21 19:18:15
问题 Here is the line of code where I declare the curve: QuadCurve2D.Double curve = new QuadCurve2D.Double(50,100,100,170,150,100); Now what code can I use to draw this curve? I tried something like: g.draw(curve); but obviously that didn't work. Any suggestions? 回答1: I've made a minimum test case of what I think your describing here. This program works but I can't really help you unless I can see the code you are working with. import java.awt.geom.*; import java.awt.*; import javax.swing.*;

Java .drawImage : How do I “unDraw” or delete a image?

拈花ヽ惹草 提交于 2020-01-14 18:06:15
问题 I need a certain image to be redrawn at different locations constantly as the program runs. So I set up a while loop that should move an image across the screen, but it just redraws the image on top of itself over and over again. What am I doing wrong? Is there a way to delete the old image before drawing it in a new location? JFrame frame = buildFrame(); final BufferedImage image = ImageIO.read(new File("BeachRoad_double_size.png")); JPanel pane = new JPanel() { @Override protected void