Plotting points in java - java

Hey I am supposed to take in a list of cities and x and y coordinates and plot them. I have all the cities in as Vertexes with x and y coordinates. Now I am trying to plot them but I can't seem to see what I am doing wrong and I am not getting an error. This is my first time using GUI so it could be a dumb mistake.
import javax.swing.JTextField;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.geom.*;
import java.util.Arrays;
public class GraphMaker{
public GraphMaker(Vertex[] a )
{
JFrame frame = new JFrame();
String start = "Start";
int columns=20;
String end = "End";
JTextField startCity = new JTextField(start,columns);
JTextField endCity = new JTextField(end,columns);
JButton button = new JButton("Find Path");
//button.addActionListener(button);
int length = a.length;
Vertex current = a[0];
CityComponent cityPanel = new CityComponent(current);
/*for(int i=0; i < length; i++){
Vertex current = a[i];
g2.draw(new Line2D.Double(x,y,x,y));
}*/
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());
panel.add(startCity);
panel.add(endCity);
panel.add(button);
frame.setLayout(new BorderLayout());
frame.add(cityPanel,BorderLayout.CENTER);
frame.add(panel,BorderLayout.SOUTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
return;
}
}
import javax.swing.*;
import java.awt.*;
public class CityComponent extends JComponent {
private Vertex m;
private int x = 0;
private int y = 0;
public CityComponent(Vertex m) {
this.m = m;
}
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
m.draw(g2);
}
}
import java.awt.*;
import java.awt.geom.*;
import java.util.*;
public class Vertex{
public String element;
public Double x;
public Double y;
public Vertex(String city, String a, String b){
this.element = city;
this.x = Double.parseDouble(a);
this.y = Double.parseDouble(b);
}
public void draw(Graphics2D g2){
Point2D.Double r1 = new Point2D.Double(x/10, y/10);
Line2D.Double line = new Line2D.Double(r1,r1);
g2.draw(line);
}
}

You're trying to use a Graphics object that doesn't exist. As I see it you have one of two choices to make here:
You could draw in the paintComponent(Graphics g) method of a class that extends JPanel, using a for loop to iterate through the vertices, and drawing with the Graphics object supplied by the JVM...
Or you could draw in a BufferedImage, using the Graphics object you obtain by calling getGraphics() on the BufferedImage. You could then place the image into an ImageIcon, and then the Icon into a JLabel.
Or you could do a combintation of the above by drawing the BufferedImage created above in a JPanel's paintComponent method.
Whatever you do, don't use a Graphics object obtained by calling getGraphics() on a Swing component. You've been warned.
Edit
I'm now seeing your CityComponent class which extends JComponent, and now see that you should be drawing with this. The key will be to pass the correct Vertex into it, something that I don't know if you're doing correctly, since we don't see how you construct your GraphMaker class.
You might wish to tell us which code is yours, which was given to you, and also give us your exact requirements. Some of your code still seems a bit off.

Related

Draw circles that are created as instances in Java GUI

So I've been stuck on this problem for a while now and I'm desperate for help. Please help me. I've got 3 classes:
Circle is just suppose to draw a circle in the frame created by Frame with random starting position (and defind the radius).
Frame is the mainclass with methods such as addCircle(), bounce(), start(), stop(), run() (moves the circles) and quit(). This class also creates the frame in which the circles are added to.
Interfa is just for now a inteface frame where I define the radius, number of circles and Frame size.
No matter what I try I cannot add more than two circle (one is colored and one is not):
The "recursive way":
private static void addCircle(int n){
Circle[] circles = new Circle[n+10];
if (n > 0){
circles[circleAdd] = new Circle();
frame.add(circles[circleAdd]);
circleAdd = circleAdd + 1;
addCircle(n-1);
}
}
Normal itterative way
private static void addCircles(int n){
ArrayList<Circle> circles = new ArrayList<Circle>();
for(int i = 0; i<=n;i++){
circles.add(new Circle());
frame.add(circles.get(i));
}
}
This is how I create my Frame:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public Class Frame{
private static JFrame frame;
private static int circleAdd = 0;
private static JPanel fra;
public static void mainFrame(){
frame = new JFrame();
frame.setSize(500,500);
frame.setVisible(true);
fra = new JPanel();
frame.add(fra);
...
//addCircle and addCircles
...
public static void main..
}
}
This is my circle:
import java.awt.*;
import javax.swing.*;
import java.util.Random;
public class Circle extends JPanel{
private Random random = new Random();
public void paint(Graphics g){
int randX = random.nextInt(250)+50;
int randY = random.nextInt(250)+50;
g.drawOval(randX,randY,50,50);
g.setColor(Color.ORANGE);
g.fillOval(100,100,50,50);
}
}
I would suggest that your general approach is wrong. Instead of using a JPanel as the element, you should have a JPanel capable of painting any number of "circles". The Graphics2D API is capable of drawing complex shapes (including ovals).
The main issues I can see are:
JFrame by default is using a BorderLayout, this only allows a single component to be placed in each of the five available positions
Layout managers rely on the preferred/minimum/maximumSize hints to make determinations about the size of the components. They are also responsible for deciding on where the component should be placed. In your current implementation, this would mean that it's possible for you to paint beyond the visible range of the component
Overriding paint is not recommend, and failing to call super.paint could cause a number of unexpected and difficult to diagnose issues
Painting can occur at any time, so using random values in the paint method will cause the UI to constantly change
Instead, you could define your own Circle class which takes the location and size you want and simply acts as a container
public class Circle {
private int x;
private int y;
private int radius;
private Ellipse2D shape;
public Circle(int x, int y, int radius) {
this.x = x;
this.y = y;
this.radius = radius;
this.shape = new Ellipse2D.Double(x, y, radius * 2, radius * 2);
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getRadius() {
return radius;
}
public Rectangle getBounds() {
return shape.getBounds();
}
public void paint(Graphics2D g2d) {
g2d.setColor(Color.ORANGE);
g2d.fill(shape);
}
}
This is simply a container class, it represents the information need to generate the desired outcome. It has a convince method which is capable of then painting the shape itself.
You would then need to create a List of these shapes and paint them to your component
public class TestPane extends JPanel {
private List<Circle> circles = new ArrayList<>(10);
private Dimension size;
public TestPane() {
Random random = new Random();
int maxX = 0;
int maxY = 0;
for (int index = 0; index < 10; index++) {
int randX = random.nextInt(250) + 50;
int randY = random.nextInt(250) + 50;
circles.add(new Circle(randX, randY, 25));
maxX = Math.max(maxX, randX + 50);
maxY = Math.max(maxY, randY + 50);
}
size = new Dimension(maxX, maxY);
}
#Override
public Dimension getPreferredSize() {
return size;
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
for (Circle circle : circles) {
Graphics2D g2d = (Graphics2D) g.create();
circle.paint(g2d);
g2d.dispose();
}
}
}
One of the things you seem to lack understanding in is how painting actually works in Swing.
Start by having a look at Performing Custom Painting and Painting in AWT and Swing for more details.
A deeper understanding of how layout managers and the component hierarchy work also wouldn't hurt

drawing other pixels in java

I am trying to draw each pixel on the frame but I cannot work out how to draw the next pixel, it only shows one pixel drawn, plz help.
I do not know how to achieve this would someone edit this code so that it draws the pixels in the adjacent coordinates plz.
import java.lang.*;
import java.util.*;
import java.util.List;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
public class MyDrawPixel extends Frame {
public Point mypoint = new Point();
public static void drawPixel(Graphics g, int x, int y, int size, Paint color)
{
Graphics2D ga = (Graphics2D)g;
Shape circle = new Ellipse2D.Float(x, y, size, size);
ga.setPaint(color);
ga.draw(circle);
ga.setPaint(color);
ga.fill(circle);
}
public void paint(Graphics g) {
Graphics2D ga = (Graphics2D)g;
drawPixel(g, mypoint.x, mypoint.y, 1, Color.black);
}
public static void main(String args[])
{
MyDrawPixel frame = new MyDrawPixel();
frame.mypoint.x = 43;
frame.mypoint.y = 43;
MyDrawPixel frame1 = new MyDrawPixel();
frame1.mypoint.x = 44;
frame1.mypoint.y = 44;
MyDrawPixel frame2 = new MyDrawPixel();
frame2.mypoint.x = 45;
frame2.mypoint.y = 45;
MyDrawPixel frame3 = new MyDrawPixel();
frame3.mypoint.x = 46;
frame3.mypoint.y = 46;
MyDrawPixel frame4 = new MyDrawPixel();
frame4.mypoint.x = 47;
frame4.mypoint.y = 47;
frame.addWindowListener(
new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
}
);
frame.setSize(400, 400);
frame.setVisible(true);
}
}
You're creating 5 frames and displaying only one. What you need to do is create 1 frame and display it correctly.
You can use repaint() to refresh the screen, then just change the attributes between the repaints if you want to move a single dot around.
Or even better, find a proper tutorial about custom painting, your code is quite horrible.

Jave input class

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;
public class Panel extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L;
final static int WIDTH = 800;
final static int HEIGHT = 600;
private int x = 40, y = 49, r = 20;
Dimension SIZE = new Dimension(WIDTH, HEIGHT);
public Panel() {
setLayout(new BorderLayout());
setPreferredSize(SIZE);
setMaximumSize(SIZE);
setMinimumSize(SIZE);
setBackground(Color.cyan);
setFocusable(true);
requestFocus();
new input(this);
}
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2 = (Graphics2D) g;
g2.fillOval(x, y, r, r);
repaint();
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
}
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class input extends KeyAdapter {
private Panel panel;
public input(Panel panel) {
panel = new Panel();
panel.addKeyListener(this);
}
#Override
public void keyPressed(KeyEvent e) {
int keycode = e.getKeyCode();
int x = panel.getX();
int y = panel.getY();
if (keycode == KeyEvent.VK_LEFT) {
panel.setX(x - 1);
}
if (keycode == KeyEvent.VK_RIGHT) {
panel.setX(x + 1);
}
}
}
I'm a Java newbie. I was trying to make a class specially for KeyListener
but it just doesn't work. I can't figure out what I did wrong.
It might be meaningless to make a inputhandler class, and it controls only the only one class (jpanel), but i used to put all of my code in one single class.. it looks so bad. i'm learning to make them into more separate classes or make them more object-oriented~
I just confuse when to make a new class, and when not.
please help me, could you tell me what i did wrong with my code above. Was my thought wrong or just the code?
This looks like the first reason your KeyListener doesn't work:
public input(Panel panel) {
panel = new Panel();
panel.addKeyListener(this);
}
When you do this:
new input(this);
Because you are also doing this:
panel = new Panel();
You are adding the KeyListener to new Panel(), not the panel you are passing in to the constructor with this.
Personally I don't see a problem with creating a listener object just to listen, although I do not think that is necessary here.
Although presumably this will not have an effect on your listener, one other problem I see is that you are overriding paint which you should not be doing in Swing. You should be overriding paintComponent. See Painting in AWT and Swing: http://www.oracle.com/technetwork/java/painting-140037.html#callbacks
Also as far as style goes, class names start with a capital letter in Java. Your input class should be named Input.

JTextArea Not Showing When repaint(), but Graphics not updating when revalidate()?

I am making a program that simulates an NHL Draft Lottery, where there should be a JTextField on the right of the screen, and draftballs being drawn on the left that bounce around. I made a class called Ball that implements Runnable, and runs as a Thread in my main DraftLottery class. However, when my draw method calls repaint(), the JTextArea doesn't show. I tried switching repaint() to revalidate(), but then balls weren't moving. Then I tried calling repaint() and revalidate(), but it acted the same as repaint().
Here is the code for my DraftLottery class:
package ca.WiltzSports.DraftLottery;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import ca.WiltzSports.DraftLottery.DraftBall.Ball;
public class DraftLottery extends JFrame {
public static final long serialVersionUID = 89L;
int teams;
String[] teamNames;
int[] balls;
JTextArea display;
JPanel screen;
JPanel animation;
List<String> entries;
public static List<Ball> ball;
String [] draftOrder;
Random rand;
int counter = 1;
javax.swing.Timer t;
public static int width = 1024;
public static int height = 768;
Graphics dbg;
Image dbImage;
int i = 0;
public DraftLottery(int teams, String[] teamNames, int[] balls){
t = new javax.swing.Timer(2000, new ActionListener(){
#Override
public void actionPerformed(ActionEvent arg0) {
draft();
}
});
ball = new ArrayList<Ball>();
rand = new Random();
this.teams = teams;
this.teamNames = teamNames;
this.balls = balls;
this.screen = new JPanel();
this.animation = new JPanel();
display = new JTextArea(20,50);
display.setBackground(Color.BLACK);
display.setForeground(Color.YELLOW);
draftOrder = new String[teams];
this.entries = new ArrayList<String>();
addTeamBalls();
setSize(width,height);
setVisible(true);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Draft Lottery");
setLayout(new BorderLayout());
screen.add(display);
add("East", new JScrollPane(display));
add("West", animation);
t.start();
}
#Override
public void paint(Graphics g){
dbImage = createImage(getWidth(), getHeight());
dbg = dbImage.getGraphics();
Graphics2D g2d = (Graphics2D) dbg;
draw(g2d);
g.drawImage(dbImage, 0, 0, this);
Ball.runBalls(ball);
}
public void draw(Graphics2D g2d){
for (Ball b: ball){
b.draw(g2d);
}
revalidate();
repaint();
}
public void addTeamBalls(){
for (int c = 0; c < teamNames.length; c++){
for (int j = 0; j < balls[c]; j++){
entries.add(teamNames[c]);
ball.add(new Ball(rand.nextInt(width/2 - Ball.SIZE), rand.nextInt(height - Ball.SIZE), teamNames[c], i++));
}
}
}
public void draft(){
int q = 0;
String [] e = entries.toArray(new String[entries.size()]);
String draftedTeam;
int index = rand.nextInt(entries.size());
draftedTeam = e[index];
if(!draftedTeam.equals("X")){
display.append("" + counter++ + ". " + draftedTeam + "\n");
for(int c = 0; c < e.length; c++){
if (e[c].equals(draftedTeam)){
e[c] = "X";
}
}
removeBalls();
}else {
boolean again = false;
for (int c = 0; c < e.length; c++){
if (!e[c].equals("X")){
again = true;
}
}
if(again){
}else{
t.stop();
display.append("DRAFT LOTTERY COMPLETE");
}
}
entries = Arrays.asList(e);
}
public void removeBalls(){
Ball [] bs;
int q = 0;
for (Ball b: ball){
if (b.getTeamName().equals("X")){
continue;
}else{
q++;
}
}
bs = new Ball[q];
q = 0;
for (Ball b: ball){
if (!b.getTeamName().equals("X")){
bs[q++] = b;
}
}
ball = Arrays.asList(bs);
}
}
Any help would be greaty appreciated.
Thanks a lot!
You have a number of problems:
You've overridden the paint method of a top level container. This is generally not recommend, as it's not double buffered and can produce flicker with animation and other paint updates.
You've failed to call super.paint. The paint mathod is responsible for pulling all the sub-paint methods together, including, paintComponents; but since you're not allowing paint to do it's job, it's not rendering those components for you.
Instead,
Create a custom component, extending from something like JPanel.
Add all the required logic for the animation to this component.
Override it's paintComponent method and add all your animation and custom painting to it.
Add it to the frame.
Take a look at Performing Custom Painting and Painting in AWT and Swing for more details.

Java:: Use a mouse listener object in a different class

Hello people,
I am trying to write an animated character for a multitouch screen. I want my object to have 5 eyes and each of whose pupil to be dragged and dropped differently, within the eye of course.
I have tried to do it all in a single class and the problem seems to be assigning mouse handlers to each of the five pupils! In other words, if I move one pupil, all the pupils are moving.
Then, I resorted to using a bespoke class just to the pupil. When I use it by itself, the pupil is draggable. However, when I use it as an object in the eyes class, the pupil is static! No clicks registered, no mouse activity tracked.
I have looked at tutorials, other related issues with mouse handlers and could not make any progress. I changed the code a dozen times from various tutorials and suggestions before finally posting here. Any ideas where I am missing the cue? Any pointers would be greatly appreciated.
Thanks in advance.
PS: I know I am yet to put the constraints on the pupil movement within the eye.
Code for main eyes class:
package rollEyes;
import java.awt.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class FiveEyes extends JPanel
{
private static final long serialVersionUID = 1L;
private static final int SIZE = 512;
private int a = SIZE / 2;
private int b = a;
private int r = 4 * SIZE / 5;
private int n;
int circleSize=30;
Pupil dc = new Pupil(1);
public FiveEyes(int n)
{
super(true);
this.setPreferredSize(new Dimension(SIZE, SIZE));
this.n = n;
}
#Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setColor(Color.black);
a = getWidth() / 2;
b = getHeight() / 2;
int m = Math.min(a, b);
r = 4 * m / 5;
int r2 = Math.abs(m - r) / 2;
int numOfEyes = 5;
for (int i = 0; i < numOfEyes ; i++)
{
Graphics2D g2d2 = (Graphics2D) g;
double t = 2 * Math.PI * i / n;
int x = (int) Math.round(a + r * Math.cos(t));
int y = (int) Math.round(b + r * Math.sin(t));
drawEyeSockets(g2d2, x,y, 2*r2,2*r2);
}
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
#Override
public void run()
{
create();
}
});
}
public void drawEyeSockets(final Graphics2D g2, int x, int y, int w, int h)
{
g2.drawOval(x,y,w,h);
dc.drawCircle(g2, x+12, y+12);
}
private static void create()
{
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
FiveEyes fivey = new FiveEyes(5);
f.add(fivey);
f.pack();
f.setVisible(true);
}
}
Code for the Pupil class:
package rollEyes;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Pupil extends JPanel
{
private static final long serialVersionUID = 1L;
int radius=50;
int x_after = 50;
int y_after = 50;
MouseHandler mh ;
private static int n =1;
public Pupil(int n)
{
super(true);
}
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
drawCircle(g2d,x_after,y_after);
}
public void drawCircle(final Graphics2D g2d, int x, int y)
{
g2d.setColor(Color.BLUE);
g2d.fillOval(x, y, radius/2, radius/2);
mh = new MouseHandler();
this.addMouseListener(mh);
this.addMouseMotionListener(mh);
}
private static void create()
{
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Pupil dc = new Pupil(n);
f.add(dc);
f.pack();
f.setVisible(true);
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
#Override
public void run()
{
create();
}
});
}
private class MouseHandler extends MouseAdapter
{
boolean circleClicked=false;
public void mouseReleased(MouseEvent e)
{
circleClicked = false;
}
public void mousePressed(MouseEvent me)
{
circleClicked = true;
}
public void mouseDragged(MouseEvent me)
{
if (circleClicked)
{
x_after = me.getX();
y_after = me.getY();
repaint();
}
}
}
}
You have Pupil extend JPanel, but really shouldn't be doing that. Instead, use the concepts that you've learned in your current Pupil class -- how to draw a movable circle, and extend it in the larger FiveEyes class, only this time create a List<Pupil> and draw them. My suggestions:
Make Pupil not extend JPanel. Instead give it the machinery to draw circles in certain locations and to have that location changed.
Also you will need to give it a way to recognize if its circle has been clicked by giving it a contains(Point p) method. One way to do this is to use a Shape object, or you can roll your own method.
Give FiveEyes a List<Pupil> that in reality is an ArrayList<Pupil> and fill it with Pupil objects.
In FiveEyes paintComponent(...) method, iterate through this List telling each Pupil to draw itself.
In your FiveEyes MouseAdapter's mousePressed(...) method, iterate through your Pupil List to see if a Pupil has been clicked on. If so, move it.
Alternatively, you could create a Pupil BufferedImage, put it into an ImageIcon, and put that into a JLabel, and then allow your FiveEyes class's MouseAdapter to drag the labels around.
First of all, this code
mh = new MouseHandler();
this.addMouseListener(mh);
this.addMouseMotionListener(mh);
must only be called once. You're adding millions of mouse handlers to the component!
And you need five instances of Pupil, one for each eye. Right now, you have only one, so of course the rendering will only yield one result.
Lastly, you must make sure that only one eye is "active" at a time (i.e. not all of them should receive mouse events or not all of them should process them).

Categories