graphics2d

Triangle Draw Method

放肆的年华 提交于 2019-11-27 03:11:30
问题 I have trouble drawing a triangle with the draw(Graphics g) method in Java. I can draw a rectangle like so: public void draw(Graphics g) { g.setColor(colorFill); g.fillRect(p.x, p.y, width, height); g.setColor(colorBorder); g.drawRect(p.x, p.y, width, height); drawHandles(g); Where p represents "the top left corner of the shapes". How would I draw the triangle in the same manner? Could someone give me an example for a standard triangle? 回答1: There is not a drawTriangle method neither in

java draw line as the mouse is moved

自作多情 提交于 2019-11-27 03:00:27
问题 I would like to add a feature to my application which allows the user to draw a straight line by clicking the mouse at the start location and releasing it at the end location. The line should move as the mouse moves until it is finally released; similar to the way that a line can be drawn using the Microsoft Paint application. How can implement this so that the line is repainted as it moves without repainting other things that may already be drawn in that rectangular area? 回答1: Try this..

Drawing text with outline in java

空扰寡人 提交于 2019-11-27 02:32:22
问题 I'm working with graphcis2d in Java and am currently using this to draw text into a bufferedImage Font font1 = new Font("Arial", Font.PLAIN, 120); g2d.setFont(font1); FontMetrics fm1 = g2d.getFontMetrics(font1); g2d.drawString(s[1], width/2-fm1.stringWidth(s[1])/2, height/4-70); I want to draw this text with an different color outline. GlyphVector gv = font1.createGlyphVector(g2d.getFontRenderContext(), s[1]); Shape shape = gv.getOutline(); g2d.setStroke(new BasicStroke(4.0f)); g2d.translate

How can I center Graphics.drawString() in Java?

混江龙づ霸主 提交于 2019-11-27 02:03:47
问题 I'm currently working on the menu system for my Java game, and I wonder how I can center the text from Graphics.drawString() , so that if I want to draw a text whose center point is at X: 50 and Y: 50 , and the text is 30 pixels wide and 10 pixels tall, the text will start at X: 35 and Y: 45 . Can I determine the width of the text before I draw it? Then it would be easy maths. EDIT: I also wonder if I can get the height of the text, so that I can center it vertically too. Any help is

Using a matrix to rotate rectangles individually

爱⌒轻易说出口 提交于 2019-11-27 01:22:12
Having a bit of a drawing complication you would call it. My math is a bit rusty when it comes to Matrices and drawing rotations on shapes. Here is a bit of code: private void Form1_Paint(object sender, PaintEventArgs e) { g = e.Graphics; g.SmoothingMode = SmoothingMode.HighQuality; DoRotation(e); g.DrawRectangle(new Pen(Color.Black), r1); g.DrawRectangle(new Pen(Color.Black), r2); // draw a line (PEN, CenterOfObject(X, Y), endpoint(X,Y) ) g.DrawLine(new Pen(Color.Black), new Point((r1.X + 50), (r1.Y + 75)), new Point((/*r1.X + */50), (/*r1.Y - */25))); this.lblPoint.Text = "X-pos: " + r1.X +

Rotate a Java Graphics2D Rectangle?

丶灬走出姿态 提交于 2019-11-26 22:34:15
I have searched everywhere and I just cant find the answer. How do I rotate a Rectangle in java? Here is some of my code: package net.chrypthic.Space; import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Space extends JPanel implements ActionListener{ Timer time; public Space() { setVisible(true); setFocusable(true); addMouseMotionListener(new ML()); addMouseListener(new ML()); addKeyListener(new AL()); time=new Timer(5, this); time.start(); } public void paint(Graphics g) { super.paint(g); Graphics2D g2d = (Graphics2D)g; g2d.setColor(Color.WHITE); Rectangle rect2 =

Drawing rectangles on a JPanel

混江龙づ霸主 提交于 2019-11-26 22:26:48
问题 I have a JScrollPane and on top of it I have a JPanel named 'panel1'. I want some rectangles to be drawn on this JPanel. I have a class named DrawRectPanel which extends JPanel and does all the drawing stuff. The problem is that, I tried to draw the rectangles on panel1 by writing the following code : panel1.add(new DrawRectPanel()); but nothing appeared on panel1 then I tried, just as a test to the class DrawRectPanel : JFrame frame = new JFrame(); frame.setSize(1000, 500); Container

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

五迷三道 提交于 2019-11-26 22:25:51
问题 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 =

create an transparent rectangle over blurred background in jframe

十年热恋 提交于 2019-11-26 22:02:23
问题 I am having problem in creating an transparent rectangle over blurred background. I am trying to do this task on glasspane . Here is my code snippet. void createBlur() { alpha = 1.0f; JRootPane root = SwingUtilities.getRootPane(jf); blurBuffer = GraphicsUtilities.createCompatibleImage(jf.getWidth(), jf.getHeight()); Graphics2D g2d = blurBuffer.createGraphics(); root.paint(g2d); g2d.dispose(); backBuffer = blurBuffer; blurBuffer = GraphicsUtilities.createThumbnailFast(blurBuffer, jf.getWidth()

Convert a Graphics2D to an Image or BufferedImage

耗尽温柔 提交于 2019-11-26 21:12:34
问题 I have a little problem here. I have an applet, where user can "draw" inside it. To do that, I use the java.awt.Graphics2D. But, how can I do to save the user draw image as a JPEG image, or at least, convert it to a BufferedImage or something? I don't know how to do that. Thanks. 回答1: Have them draw directly in a BufferedImage by way of it's Graphics2D object which you can get via getGraphics(). Then use ImageIO.write(...) to output the image to whatever file type you desire (and that's