java - Repaint() not being called -



java - Repaint() not being called -

recently i've been working on programme paints area empty, colored squares. locations on screen based off of values 1 , 2 in text file. 1s supposed create reddish boxes, , 2s supposed create greenish boxes. however, when run program, reddish boxes painted. did testing , found out repaint method beingness called twice(once reason), though there close 300 values in file, , repaint() should called 1 time every value. here code:

public class map extends jframe { public static void main(string[] args) throws ioexception { map map = new map(); } shape shape; int x = -32; int y = 0; arraylist<shape> shapes = new arraylist<shape>(); graphics2d g2; color coulor = null; private class paintsurface extends jcomponent { public paintsurface() { } public void paint(graphics g) { g2 = (graphics2d) g; g2.setcolor(coulor); (shape s : shapes) { g2.draw(s); } } } public map() throws filenotfoundexception, ioexception { jframe frame = new jframe(); jpanel panel = new jpanel(); frame.add(panel); frame.settitle("grid maker"); frame.setsize(400, 200); frame.setdefaultcloseoperation(exit_on_close); frame.add(new paintsurface(), borderlayout.center); frame.setvisible(true); readnextline(); } private void readnextline() throws ioexception { file file = new file("map.txt"); bufferedreader in = new bufferedreader(new filereader(file)); string line = in.readline(); while (line != null) { (int = 0; < line.length(); i++) { char c = line.charat(i); if (c == '1') { coulor = color.red; x += 32; int smallx = x / 32; int smally = y / 32; shape = new rectangle2d.float(x, y, 32, 32); shapes.add(shape); repaint(); } else if (c == '2') { coulor = color.green; x += 32; int smallx = x / 32; int smally = y / 32; shape = new rectangle2d.float(x, y, 32, 32); shapes.add(shape); repaint(); } } line = in.readline(); x = -32; y += 32; } } }

why isn't code working properly?

just add together other answers, here piece of code (based on yours) looks lot improve (yet there still issues, not there yet):

import java.awt.borderlayout; import java.awt.color; import java.awt.graphics; import java.awt.graphics2d; import java.awt.shape; import java.awt.geom.rectangle2d; import java.io.bufferedreader; import java.io.filenotfoundexception; import java.io.ioexception; import java.io.stringreader; import java.util.arraylist; import java.util.list; import javax.swing.jcomponent; import javax.swing.jframe; public class map extends jframe { public static void main(string[] args) throws ioexception { map map = new map(); } public static class coloredshape { private shape shape; private color color; public coloredshape(shape shape, color color) { super(); this.shape = shape; this.color = color; } public shape getshape() { homecoming shape; } public color getcolor() { homecoming color; } } int x = -32; int y = 0; list<coloredshape> shapes = new arraylist<coloredshape>(); graphics2d g2; private class paintsurface extends jcomponent { public paintsurface() { } @override protected void paintcomponent(graphics g) { super.paintcomponent(g); g2 = (graphics2d) g; (coloredshape s : shapes) { g2.setcolor(s.getcolor()); g2.draw(s.getshape()); } } } public map() throws filenotfoundexception, ioexception { jframe frame = new jframe(); frame.settitle("grid maker"); frame.setsize(400, 400); frame.setdefaultcloseoperation(exit_on_close); frame.add(new paintsurface(), borderlayout.center); frame.setvisible(true); readnextline(); } private void readnextline() throws ioexception { bufferedreader in = new bufferedreader(new stringreader("11121\n1221\n2212\n221121\n111221\n11221\n222\n2222\n")); string line = in.readline(); while (line != null) { (int = 0; < line.length(); i++) { char c = line.charat(i); color color = null; if (c == '1') { color = color.red; } else if (c == '2') { color = color.green; } if (color != null) { shapes.add(new coloredshape(new rectangle2d.float(x, y, 32, 32), color)); x += 32; repaint(); } } line = in.readline(); x = -32; y += 32; } } }

java swing paint repaint

Comments

Popular posts from this blog

javascript - mongodb won't find my schema method in nested container -

Hibernate criteria by a list of natural ids -

ios - Lagging ScrollView with UIWebview inside -