Java Swing – Draw Station Names using GPS Coordinates on JPanel

I am working on an assignment and got stuck at a certain point. I am given a .txt file with a series of stations, and I am supposed to draw the station names using the GPS coordinates given to me, but I can’t translate those coordinates to XY coordinates that a JPanel uses. Also, GPS … Continue reading “Java Swing – Draw Station Names using GPS Coordinates on JPanel”

How do I stop previous graphics from being redrawn in Java?

I’ve been working on a game but messed something up and I can’t figure what it is. I’ve simplified the code as much as possible in this example and it retains the same issue. In this example, instead of the red square going upwards without leaving a trace, it leaves behind a red trace meaning … Continue reading “How do I stop previous graphics from being redrawn in Java?”

Java Swing – Draw line between for loop drawn components

I am working on an assignment and I’m almost done, however, there is a bullet that tells me to draw a line between the adjacent components to approximate the distance between them. I searched for the answer everywhere, and none of them worked. Here’s the code, I cut out the unimportant things, thanks in advance! … Continue reading “Java Swing – Draw line between for loop drawn components”

JPanel Object Oriented Graphics

In my computer science class we are learning Java and we have come to the point in the course where we are learning about object orientation and how it is useful. We started a project a couple days ago, however instead of being tasked with basic object orientation, my teacher decided to challenge me and … Continue reading “JPanel Object Oriented Graphics”

Having trouble with pen transparency in paint program

I’m relatively new to Java, but I’ve made a simple paint program that allows the user to draw, change the color and size of the pen, and clear the drawing area. The user selects a color from the JColorChooser object ‘c’ and this is handed off to the Color variable ‘selected’ through c.getColor(). I want … Continue reading “Having trouble with pen transparency in paint program”

ACM Graphics, Moving an image across the screen

I can’t get my code to compile. I’m Trying to get an Image to move across the screen by using a timer. Can I get some help? It there is a background gif that i have working, and a png of an turtle is supposed to move across the screen but I can’t get it … Continue reading “ACM Graphics, Moving an image across the screen”

How do I decrease letter spacing in java.awt.font?

I’m trying to make a graph with a numbered axis. However, I can’t seem to decrease the spacing between these numbers. Here is the current code I am using to decrease the spacing between the numbers in my paintComponent method, but it doesn’t seem to work: super.paintComponent(g); Map<TextAttribute, Object> attributes = new HashMap<TextAttribute, Object>(); attributes.put(TextAttribute.TRACKING, … Continue reading “How do I decrease letter spacing in java.awt.font?”

Why my image does not save

I have a simple program with JFrame and Jpanel on it and I want to draw a line on the JPanel and save the result of the drawing to an image. But it completely does not work. Where was I wrong? import javax.swing.JFrame; import javax.swing.JPanel; import java.awt.*; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.util.logging.Level; … Continue reading “Why my image does not save”

How do I initialize a variable in the main method, and then use it to paint a graphic on screen AFTER it has been initialized

My problem is that I need to declare all the variables in my main method so that I can pass them to other classes in the future. The problem is that I can’t call the paint method from my main method, so I have to declare all the variables inside my painting method. What I … Continue reading “How do I initialize a variable in the main method, and then use it to paint a graphic on screen AFTER it has been initialized”