graphics2d

Line2D decoration tips needed - Graphics2D

谁说胖子不能爱 提交于 2019-11-26 18:35:05
问题 I have Line2D and Arc2D objects laid out on my JPanel by Graphics2D drawing. You can have a look a part of it on this question " How to make pixel perfect Line2D in - Graphics2D ". Now what I want to achieve is, I want to create two parallel lines and arcs for all Line2D and Arc2D objects. Visually, Normal Line2D and Arc2D drawn currently, Want to decorate it like this, My Thoughts so far, I might be able to achieve this by creating two different line and give an offset +gap and -gap from my

Fit/Scale JComponent to page being printed

北慕城南 提交于 2019-11-26 17:57:23
I'm trying to scale my component so it can fit on a single printed page (portrait or landscape) gDiagram.getComponent() is the component (JPanel) I'm trying to print. Here's what I've got so far based on How can I print a single JPanel's contents? /** * Prints the diagram. */ public void printDiagram() { PrinterJob pj = PrinterJob.getPrinterJob(); pj.setJobName(" Print Component "); pj.setPrintable(new Printable() { @Override public int print(Graphics g, PageFormat pf, int pageNumber) throws PrinterException { // TODO Auto-generated method stub if (pageNumber > 0) { return Printable.NO_SUCH

How to do 2D shadow casting in Java?

大憨熊 提交于 2019-11-26 17:20:45
问题 I'm currently trying to implement a 2D shadow casting method in Java by following this tutorial: http://ncase.me/sight-and-light/ I want to stick to Line2D and Polygon objects. Here is the main part of my code so far: for (Polygon p : Quads.polygons) { for (int i = 0; i < p.npoints; i++) { osgCtx.setStroke(new BasicStroke(0.1f)); Line2D line = new Line2D.Double(mousePos.getX(), mousePos.getY(), p.xpoints[i], p.ypoints[i]); osgCtx.draw(line); } osgCtx.setStroke(new BasicStroke(1.0f)); osgCtx

How to smoothen scrolling of JFrame in Java

╄→гoц情女王★ 提交于 2019-11-26 16:57:20
问题 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;

How to make line animation smoother?

笑着哭i 提交于 2019-11-26 16:41:47
I am making a simple animation in Java and I am trying to make it as smooth as possible. I use only *.Double inner classes of each Shape object and I set antialiasing in the Graphics2D objects on. It all works as long as I use only the fill() method but if I also use draw() method to draw lines around the same Shape the animation of these lines is choppy - pixel by pixel. Each of my rectangles on the canvas has this method to paint itself. It is moved every 20ms and the whole canvas is repainted using Timer and TimerListener. import javax.swing.*; import java.awt.*; import java.awt.event

Problems with newline in Graphics2D.drawString

依然范特西╮ 提交于 2019-11-26 16:32:14
g2 is an instance of the class Graphics2D . I'd like to be able to draw multi-line text, but that requires a newline character. The following code renders in one line. String newline = System.getProperty("line.separator"); g2.drawString("part1\r\n" + newline + "part2", x, y); The drawString method does not handle new-lines. You'll have to split the string on new-line characters yourself and draw the lines one by one with a proper vertical offset: void drawString(Graphics g, String text, int x, int y) { for (String line : text.split("\n")) g.drawString(line, x, y += g.getFontMetrics().getHeight

paintComponent draws other components on top of my drawing

主宰稳场 提交于 2019-11-26 14:48:54
问题 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

Rotate an image in java by the specified angle

十年热恋 提交于 2019-11-26 14:47:13
问题 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

Drawing a simple line graph in Java

只谈情不闲聊 提交于 2019-11-26 13:02:43
In my program I want to draw a simple score line graph. I have a text file and on each line is an integer score, which I read in and want to pass as argument to my graph class. I'm having some trouble implementing the graph class and all the examples I've seen have their methods in the same class as their main, which I won't have. I want to be able to pass my array to the object and generate a graph, but when calling my paint method it is asking me for a Graphics g... This is what I have so far: public class Graph extends JPanel { public void paintGraph (Graphics g){ ArrayList<Integer> scores

Rotating Image with AffineTransform

只愿长相守 提交于 2019-11-26 12:32:36
I have got class called Airplane . Inside this class i have got variable img which is a BufferedImage type. What is more i have got class WorldMap which overrides function paintComponent(Graphics g) : @Override public void paintComponent(Graphics g) { Graphics2D g2d = (Graphics2D) g; g2d.drawImage(mapa, 0, 0, getWidth(), getHeight(), null); drawAirplanes(g2d); } Function drawAirplanes() look like this: private void drawAirplane(Graphics2D g){ for(Samolot i: s){ i.rotateAirplane(); g.drawImage(i.getImg(),i.getX(),i.getY(),(int)i.getDim().getWidth(),(int)i.getDim().getHeight(), null); } } It