FAQ Database Discussion Community


I added a rectangle to a shape Arraylist but the shape will not show up on the panel

java,arraylist,jframe,jpanel,graphics2d
import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.ArrayList; @SuppressWarnings("serial") public class GUI extends JFrame implements ActionListener, MouseListener { private boolean drawLine = false; private boolean drawRec = false; private boolean drawOval = false; private final JButton line; private final JButton oval; private final JButton rectangle; private final JPanel buttonPanel; public...

How to load and use ttf font from jar [duplicate]

java,fonts,graphics2d,true-type-fonts
This question already has an answer here: Setting custom font 2 answers Trying to write a class: private void gameLevel(Graphics g) { try { InputStream fnt_stream = getClass().getResourceAsStream("resources/iomanoid.ttf"); Font myFont = Font.createFont(Font.TRUETYPE_FONT, fnt_stream); Font Iomanoid = new Font("Iomanoid", Font.BOLD, 40); String msg = "Level"; g.setColor(Color.black); g.setFont(Iomanoid); g.drawString(msg, 111,111); }...

How to export graphics2d To PDF using PDFCLown library

graphics2d,pdfclown
I am new to pdfClown. I have a Graphics2D data from my JAVA JCanvas i need to export this graphics2d data to PDF. How can i do that by using pdfclown? Please Help me.

Is it truly never okay to call Component.getGraphics?

java,swing,graphics2d,repaint,java-2d
MadProgrammer made a comment here stating that you should NEVER EVER use Component.getGraphics, which I generally agree with: In virtually all situations, it is wrong. But take the case of this class I created years ago. I'm posting what I think is the relevant code. You can find the full...

How to create custom high-def images in Java

java,bufferedimage,graphics2d
I am creating images in Java that have fonts written on them with a transparent background. I make the fonts different colors and also different types of font styles so I need the program to be dynamic. The issue is that I am using Graphics2D and writing on a Buffered...

Can i add other shapes than ellipse2D rectangle2D to “Shape” in Java?

java,arraylist,geometry,shape,graphics2d
I have an Arraylist that saves all shapes. ArrayList<Shape> shapes = new ArrayList<Shape>(); The ArrayList is of type Shape and hence has ellipse, rectangle, line, point. But now I want to draw a triangle and save it into that same ArrayList. Is it possible to do this? I mean can...

Java Graphics2D Translate and Scale

java,graphics2d
I have a problem. I want to be able to zoom into my Graphics2D screen using the mouse wheel but I want to be able to translate the Graphics2D as well so that it's right on the spot that I scaled. Here's what's happening so far: "http://cdn.makeagif.com/media/6-11-2015/E0kYGY.gif" It's translating to...

Why does my Image not rotate?

java,rotation,applet,bufferedimage,graphics2d
This code displays the logo, but there's no rotation. Am I missing something? I've been trying to rotate for a long time. Everything displays fine, and the calculations are correct, but the image displays as a normal image import javax.swing.JApplet; import java.awt.Graphics2D; import java.awt.Graphics; import java.applet.Applet; import java.awt.Font; import java.awt.image.*;...

Can't render unicode characters with Graphics2D even though they are supported by the font

java,unicode,fonts,awt,graphics2d
I try to render text to a BufferedImage and that works quite great but I can't render any characters that are not ASCII (at least as far as I can see). All I could find was that it's because of the font so I downloaded Google's "Noto" fonts that seem...

Painting balloons on Graphics2D

java,graphics2d
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...

Drawing a 8x8 rectangle becomes a 9x9 rectangle on screen?

java,swing,graphics2d
g2.fill(new Rectangle2D.Double(0, 0, 8, 8)); This nicely fills a 8x8 rectangle. But strange things happen when I try to draw a 8x8 rectangle border: g2.draw(new Rectangle2D.Double(0, 0, 8,8)); This draws a rectangle 9x9 rectangle. But I specified that it should be 8 width and 8 height. I have a default...

Java drawing program

java,swing,graphics2d
I am trying to create a simple drawing program which contains a toolbar and a drawing area. The program's main window is a JFrame. I have added a JToolBar and a JPanel (drawingPanel) on which to draw. However, the line is not drawn on drawingPanel but behind it (I can...

how to refresh JFrame after after adding java 2d components

java,swing,graphics,graphics2d
in this code i added two components to the jframe and also used revalidate and repaint but only one of the components is being viewed. i need a way to refresh the jframe class A extends JPanel{ int i ,j; A(int i,int j){ this.i = i; this.j = j; }...

Jfreechart: Drawing line arrow in gantt chart

java,jfreechart,graphics2d,gantt-chart
I want to draw some vertical lines to track the tasks as shown by the red lines in the figure below. I believe the lines can be drawn using the Gantt Renderer. What I want to is store the coordinates of the end points of the lines during drawing of...

Trying to draw lines based on doubles but nothing shows?

java,swing,jpanel,double,graphics2d
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,...

Why is java.awt.Graphics.drawLine exceptionally slow?

java,swing,graphics2d
I am trying to achieve the following 'grid' layout. The class is extending java.awt.Canvas, and drawing these shapes (or lines) in the paint function. Why Canvas? Check here, trying to do something similar inititally. Updated MCVE Code for getting the above 'layout': import java.awt.BasicStroke; import java.awt.BorderLayout; import java.awt.Canvas; import java.awt.Color;...

Why is my brushstroke thickness changing?

java,variable-assignment,graphics2d
My assignment is to draw horizontal lines random in width and random line thickness. When I run my program the thickness changes part way through. I don't understand how that is possible since I am setting the width then drawing the line. I don't update so I am not redrawing...

Graphics not displaying on JFrame

java,swing,graphics,graphics2d
I'm am new to graphics in java and for some reason the graphics are not displaying on the jframe. I am confused of how to set up and instantiate the graphics. There could also be a stupid error in the code that im just not seeing. Thanks for any feedback!...

Java Graphics2D scale doubling

java,scale,graphics2d
I'm having trouble scaling objects with Graphics2D in Java. In my paintComponent I create two graphics2D objects for easier management. I then try to scale them. The problem is that the output is scaled x4 (the scale commands affect each other). Otherwise everything renders as it should. How is this...

Height parameter in Graphics2D.drawArc

java,swing,parameters,graphics2d
I'm trying to create an arc, but when I use the same width and height parameters, the arc is almost flat, shouldn't it be higher? I would have expected that the height would appear to be the same length as the width on the screen. public void paintComponent(Graphics g) {...

MouseListener on a drawString() Method

java,graphics2d,mouselistener
How can I detect if the text ("Resume", "Restart", "Quit") that I drew with a drawString() method is being clicked? My code so far: public class Pause { public Pause() { } public void draw(Graphics2D g) { g.setFont(new Font("Arial", Font.BOLD, 14)); int intValue = Integer.parseInt( "ff5030",16); g.setColor(new Color(intValue)); g.drawString("Resume", 200,...

Unable to draw over a JPanel

java,swing,awt,graphics2d
I am trying to draw on a JPanel. When the function drawField is called, it draws nothing. What could be the reason for it? private void drawField() { try { Graphics2D field = (Graphics2D) jPanel2.getGraphics(); field.drawLine(0, 0, 100 , 100); }catch(Exception exc) { exc.printStackTrace();} } It is called from the...

Am I getting Java and JavaScript (ECMA-262) mixed up?

java,javascript,methods,casting,graphics2d
I'm new to Java and JavaScript. While working though some Java Tutorials, I got to the part about casting: public void paint (Graphics g) { Graphics2D g2 = (Graphics2D) g; ... } Is the component method paint casting a Graphics object g onto an Object class {} or am I...

How to draw a diamond shape in java?

java,arraylist,geometry,shape,graphics2d
So i have to draw a diamond shape. Not a Static diamond but a diamond that i will myself drag and draw. I've used General Path to do it but it is drawing a diamond that is not straight; the diamond is bend to the left and it's not being...